xref: /titanic_41/usr/src/cmd/fs.d/smbclnt/smbutil/smbutil.c (revision ba2be53024c0b999e74ba9adcd7d80fec5df8c57)
1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 
3 #include <sys/param.h>
4 #include <sys/time.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <err.h>
10 #include <sysexits.h>
11 #include <locale.h>
12 #include <libintl.h>
13 
14 #include <netsmb/smb_lib.h>
15 
16 #include "common.h"
17 
18 #ifndef EX_DATAERR
19 #define	EX_DATAERR 1
20 #endif
21 
22 static void help(void);
23 
24 
25 typedef int cmd_fn_t (int argc, char *argv[]);
26 typedef void cmd_usage_t (void);
27 
28 #define	CMDFL_NO_KMOD	0x0001
29 
30 static struct commands {
31 	const char	*name;
32 	cmd_fn_t	*fn;
33 	cmd_usage_t	*usage;
34 	int 		flags;
35 } commands[] = {
36 	{"crypt",	cmd_crypt,	NULL, CMDFL_NO_KMOD},
37 	{"help",	cmd_help,	help_usage, CMDFL_NO_KMOD},
38 	{"login",	cmd_login,	login_usage, 0},
39 	{"logout",	cmd_logout,	logout_usage, 0},
40 	{"logoutall",	cmd_logoutall,	logoutall_usage, 0},
41 	{"lookup",	cmd_lookup,	lookup_usage, CMDFL_NO_KMOD},
42 	{"status",	cmd_status,	status_usage, 0},
43 	{"view",	cmd_view,	view_usage, 0},
44 	{NULL, NULL, NULL, 0}
45 };
46 
47 static struct commands *
48 lookupcmd(const char *name)
49 {
50 	struct commands *cmd;
51 
52 	for (cmd = commands; cmd->name; cmd++) {
53 		if (strcmp(cmd->name, name) == 0)
54 			return (cmd);
55 	}
56 	return (NULL);
57 }
58 
59 int
60 cmd_crypt(int argc, char *argv[])
61 {
62 	char *cp, *psw;
63 
64 	if (argc < 2)
65 		psw = getpassphrase(gettext("Password:"));
66 	else
67 		psw = argv[1];
68 	/* XXX Better to embed malloc/free in smb_simplecrypt? */
69 	cp = malloc(4 + 2 * strlen(psw));
70 	if (cp == NULL)
71 		errx(EX_DATAERR, gettext("out of memory"));
72 	smb_simplecrypt(cp, psw);
73 	printf("%s\n", cp);
74 	free(cp);
75 	return (0);
76 }
77 
78 int
79 cmd_help(int argc, char *argv[])
80 {
81 	struct commands *cmd;
82 	char *cp;
83 
84 	if (argc < 2)
85 		help_usage();
86 	cp = argv[1];
87 	cmd = lookupcmd(cp);
88 	if (cmd == NULL)
89 		errx(EX_DATAERR, gettext("unknown command %s"), cp);
90 	if (cmd->usage == NULL)
91 		errx(EX_DATAERR,
92 		    gettext("no specific help for command %s"), cp);
93 	cmd->usage();
94 	return (0);
95 }
96 
97 int
98 main(int argc, char *argv[])
99 {
100 	struct commands *cmd;
101 	char *cp;
102 	int opt;
103 	extern void dropsuid();
104 
105 	(void) setlocale(LC_ALL, "");
106 	(void) textdomain(TEXT_DOMAIN);
107 
108 	dropsuid();
109 
110 	if (argc < 2)
111 		help();
112 
113 	while ((opt = getopt(argc, argv, "dhv")) != EOF) {
114 		switch (opt) {
115 		case 'd':
116 			smb_debug++;
117 			break;
118 		case 'h':
119 			help();
120 			/* NOTREACHED */
121 		case 'v':
122 			smb_verbose++;
123 			break;
124 		default:
125 			help();
126 			/* NOTREACHED */
127 		}
128 	}
129 	if (optind >= argc)
130 		help();
131 
132 	cp = argv[optind];
133 	cmd = lookupcmd(cp);
134 	if (cmd == NULL)
135 		errx(EX_DATAERR, gettext("unknown command %s"), cp);
136 
137 	if ((cmd->flags & CMDFL_NO_KMOD) == 0 && smb_lib_init() != 0)
138 		exit(1);
139 
140 	argc -= optind;
141 	argv += optind;
142 	optind = 1;
143 	return (cmd->fn(argc, argv));
144 }
145 
146 static void
147 help(void) {
148 	printf("\n");
149 	printf(gettext("usage: %s [-hv] subcommand [args]\n"), __progname);
150 	printf(gettext("where subcommands are:\n"
151 	" crypt		slightly obscure password\n"
152 	" help		display help on specified subcommand\n"
153 	/* " lc 		display active connections\n" */
154 	" login		login to specified host\n"
155 	" logout 	logout from specified host\n"
156 	" logoutall	logout all users (requires privilege)\n"
157 	" lookup 	resolve NetBIOS name to IP address\n"
158 	/* " print		print file to the specified remote printer\n" */
159 	" status 	resolve IP address or DNS name to NetBIOS names\n"
160 	" view		list resources on specified host\n"
161 	"\n"));
162 	exit(1);
163 }
164 
165 void
166 help_usage(void) {
167 	printf(gettext("usage: smbutil help command\n"));
168 	exit(1);
169 }
170