smbutil.c revision 256281
1245614Sandrew#include <sys/param.h>
2245614Sandrew#include <sys/time.h>
3245614Sandrew#include <stdio.h>
4245614Sandrew#include <string.h>
5245614Sandrew#include <unistd.h>
6245614Sandrew#include <stdlib.h>
7245614Sandrew#include <err.h>
8245614Sandrew#include <sysexits.h>
9245614Sandrew
10245614Sandrew#include <netsmb/smb_lib.h>
11245614Sandrew#include <netsmb/smb_conn.h>
12245614Sandrew
13296417Sdim#include "common.h"
14245614Sandrew
15245614Sandrewextern char *__progname;
16245614Sandrew
17274201Sdimstatic void help(void);
18251034Sedstatic void help_usage(void);
19276789Sdimstatic int  cmd_crypt(int argc, char *argv[]);
20245614Sandrewstatic int  cmd_help(int argc, char *argv[]);
21245614Sandrew
22245614Sandrewint verbose;
23245614Sandrew
24276789Sdimtypedef int cmd_fn_t (int argc, char *argv[]);
25276789Sdimtypedef void cmd_usage_t (void);
26276789Sdim
27296417Sdim#define	CMDFL_NO_KMOD	0x0001
28276789Sdim
29276789Sdimstatic struct commands {
30276789Sdim	const char *	name;
31276789Sdim	cmd_fn_t*	fn;
32276789Sdim	cmd_usage_t *	usage;
33276789Sdim	int 		flags;
34245614Sandrew} commands[] = {
35276789Sdim	{"crypt",	cmd_crypt,	NULL, CMDFL_NO_KMOD},
36245614Sandrew	{"help",	cmd_help,	help_usage, CMDFL_NO_KMOD},
37245614Sandrew	{"lc",		cmd_dumptree,	NULL},
38276789Sdim	{"login",	cmd_login,	login_usage},
39276789Sdim	{"logout",	cmd_logout,	logout_usage},
40276789Sdim	{"lookup",	cmd_lookup,	lookup_usage, CMDFL_NO_KMOD},
41276789Sdim	{"print",	cmd_print,	print_usage},
42245614Sandrew	{"view",	cmd_view,	view_usage},
43276789Sdim	{NULL, NULL}
44276789Sdim};
45274201Sdim
46274201Sdimstatic struct commands *
47274201Sdimlookupcmd(const char *name)
48274201Sdim{
49274201Sdim	struct commands *cmd;
50274201Sdim
51274201Sdim	for (cmd = commands; cmd->name; cmd++) {
52274201Sdim		if (strcmp(cmd->name, name) == 0)
53276789Sdim			return cmd;
54274201Sdim	}
55274201Sdim	return NULL;
56274201Sdim}
57274201Sdim
58276789Sdimint
59274201Sdimcmd_crypt(int argc, char *argv[])
60274201Sdim{
61274201Sdim	char *cp, *psw;
62274201Sdim
63274201Sdim	if (argc < 2)
64274201Sdim		psw = getpass("Password:");
65274201Sdim	else
66274201Sdim		psw = argv[1];
67274201Sdim	cp = smb_simplecrypt(NULL, psw);
68274201Sdim	if (cp == NULL)
69276789Sdim		errx(EX_DATAERR, "out of memory");
70296417Sdim	printf("%s\n", cp);
71245614Sandrew	free(cp);
72296417Sdim	exit(0);
73}
74
75int
76cmd_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
93int
94main(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
139static void
140help(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
155static void
156help_usage(void) {
157	printf("usage: smbutil help command\n");
158	exit(1);
159}
160