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