xref: /freebsd/contrib/smbfs/smbutil/smbutil.c (revision 1669d8afc64812c8d2d1d147ae1fd42ff441e1b1)
1 #include <sys/param.h>
2 #include <sys/time.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <err.h>
8 #include <sysexits.h>
9 
10 #include <netsmb/smb_lib.h>
11 #include <netsmb/smb_conn.h>
12 
13 #include "common.h"
14 
15 extern char *__progname;
16 
17 static void help(void);
18 static void help_usage(void);
19 static int  cmd_crypt(int argc, char *argv[]);
20 static int  cmd_help(int argc, char *argv[]);
21 
22 int verbose;
23 
24 typedef int cmd_fn_t (int argc, char *argv[]);
25 typedef void cmd_usage_t (void);
26 
27 #define	CMDFL_NO_KMOD	0x0001
28 
29 static struct commands {
30 	const char *	name;
31 	cmd_fn_t*	fn;
32 	cmd_usage_t *	usage;
33 	int 		flags;
34 } commands[] = {
35 	{"crypt",	cmd_crypt,	NULL, CMDFL_NO_KMOD},
36 	{"help",	cmd_help,	help_usage, CMDFL_NO_KMOD},
37 	{"lc",		cmd_dumptree,	NULL},
38 	{"login",	cmd_login,	login_usage},
39 	{"logout",	cmd_logout,	logout_usage},
40 	{"lookup",	cmd_lookup,	lookup_usage, CMDFL_NO_KMOD},
41 	{"print",	cmd_print,	print_usage},
42 	{"view",	cmd_view,	view_usage},
43 	{NULL, NULL}
44 };
45 
46 static struct commands *
47 lookupcmd(const char *name)
48 {
49 	struct commands *cmd;
50 
51 	for (cmd = commands; cmd->name; cmd++) {
52 		if (strcmp(cmd->name, name) == 0)
53 			return cmd;
54 	}
55 	return NULL;
56 }
57 
58 int
59 cmd_crypt(int argc, char *argv[])
60 {
61 	char *cp, *psw;
62 
63 	if (argc < 2)
64 		psw = getpass("Password:");
65 	else
66 		psw = argv[1];
67 	cp = smb_simplecrypt(NULL, psw);
68 	if (cp == NULL)
69 		errx(EX_DATAERR, "out of memory");
70 	printf("%s\n", cp);
71 	free(cp);
72 	exit(0);
73 }
74 
75 int
76 cmd_help(int argc, char *argv[])
77 {
78 	struct commands *cmd;
79 	char *cp;
80 
81 	if (argc < 2)
82 		help_usage();
83 	cp = argv[1];
84 	cmd = lookupcmd(cp);
85 	if (cmd == NULL)
86 		errx(EX_DATAERR, "unknown command %s", cp);
87 	if (cmd->usage == NULL)
88 		errx(EX_DATAERR, "no specific help for command %s", cp);
89 	cmd->usage();
90 	exit(0);
91 }
92 
93 int
94 main(int argc, char *argv[])
95 {
96 	struct commands *cmd;
97 	char *cp;
98 	int opt;
99 #ifdef APPLE
100         extern void dropsuid();
101 
102 	dropsuid();
103 #endif /* APPLE */
104 
105 	if (argc < 2)
106 		help();
107 
108 	while ((opt = getopt(argc, argv, "hv")) != EOF) {
109 		switch (opt) {
110 		    case 'h':
111 			help();
112 			/*NOTREACHED */
113 		    case 'v':
114 			verbose = 1;
115 			break;
116 		    default:
117 			warnx("invalid option %c", opt);
118 			help();
119 			/*NOTREACHED */
120 		}
121 	}
122 	if (optind >= argc)
123 		help();
124 
125 	cp = argv[optind];
126 	cmd = lookupcmd(cp);
127 	if (cmd == NULL)
128 		errx(EX_DATAERR, "unknown command %s", cp);
129 
130 	if ((cmd->flags & CMDFL_NO_KMOD) == 0 && smb_lib_init() != 0)
131 		exit(1);
132 
133 	argc -= optind;
134 	argv += optind;
135 	optind = optreset = 1;
136 	return cmd->fn(argc, argv);
137 }
138 
139 static void
140 help(void) {
141 	printf("\n");
142 	printf("usage: %s [-hv] command [args]\n", __progname);
143 	printf("where commands are:\n"
144 	" crypt [password]		slightly encrypt password\n"
145 	" help command			display help on \"command\"\n"
146 	" lc 				display active connections\n"
147 	" login //user@host[/share]	login to the specified host\n"
148 	" logout //user@host[/share]	logout from the specified host\n"
149 	" print //user@host/share file	print file to the specified remote printer\n"
150 	" view //user@host		list resources on the specified host\n"
151 	"\n");
152 	exit(1);
153 }
154 
155 static void
156 help_usage(void) {
157 	printf("usage: smbutil help command\n");
158 	exit(1);
159 }
160