xref: /freebsd/contrib/smbfs/smbutil/smbutil.c (revision f1b9d12761de3c33754f03e6d2a5bf9f1b40aad9)
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 = malloc(strlen(psw + 4));
68 	if (cp == NULL)
69 		errx(EX_DATAERR, "out of memory");
70 	smb_simplecrypt(cp, psw);
71 	printf("%s\n", cp);
72 	free(cp);
73 	exit(0);
74 }
75 
76 int
77 cmd_help(int argc, char *argv[])
78 {
79 	struct commands *cmd;
80 	char *cp;
81 
82 	if (argc < 2)
83 		help_usage();
84 	cp = argv[1];
85 	cmd = lookupcmd(cp);
86 	if (cmd == NULL)
87 		errx(EX_DATAERR, "unknown command %s", cp);
88 	if (cmd->usage == NULL)
89 		errx(EX_DATAERR, "no specific help for command %s", cp);
90 	cmd->usage();
91 	exit(0);
92 }
93 
94 int
95 main(int argc, char *argv[])
96 {
97 	struct commands *cmd;
98 	char *cp;
99 	int opt;
100 
101 	if (argc < 2)
102 		help();
103 
104 	while ((opt = getopt(argc, argv, "hv")) != EOF) {
105 		switch (opt) {
106 		    case 'h':
107 			help();
108 			/*NOTREACHED */
109 		    case 'v':
110 			verbose = 1;
111 			break;
112 		    default:
113 			warnx("invalid option %c", opt);
114 			help();
115 			/*NOTREACHED */
116 		}
117 	}
118 	if (optind >= argc)
119 		help();
120 
121 	cp = argv[optind];
122 	cmd = lookupcmd(cp);
123 	if (cmd == NULL)
124 		errx(EX_DATAERR, "unknown command %s", cp);
125 
126 	if ((cmd->flags & CMDFL_NO_KMOD) == 0 && smb_lib_init() != 0)
127 		exit(1);
128 
129 	argc -= optind;
130 	argv += optind;
131 	optind = optreset = 1;
132 	return cmd->fn(argc, argv);
133 }
134 
135 static void
136 help(void) {
137 	printf("\n");
138 	printf("usage: %s [-hv] command [args]\n", __progname);
139 	printf("where commands are:\n"
140 	" crypt [password]		slightly encrypt password\n"
141 	" help command			display help on \"command\"\n"
142 	" lc 				display active connections\n"
143 	" login //user@host[/share]	login to the specified host\n"
144 	" logout //user@host[/share]	logout from the specified host\n"
145 	" print //user@host/share file	print file to the specified remote printer\n"
146 	" view //user@host		list resources on the specified host\n"
147 	"\n");
148 	exit(1);
149 }
150 
151 static void
152 help_usage(void) {
153 	printf("usage: smbutil help command\n");
154 	exit(1);
155 }
156