xref: /titanic_41/usr/src/cmd/fs.d/smbclnt/smbutil/smbutil.c (revision a026686c807bfa501e774c70b45ef875ed7de130)
1 /*
2  * Copyright (c) 2000, Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
35  * Use is subject to license terms.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <stdlib.h>
44 #include <err.h>
45 #include <sysexits.h>
46 #include <locale.h>
47 #include <libintl.h>
48 
49 #include <netsmb/smb_lib.h>
50 
51 #include "common.h"
52 
53 #ifndef EX_DATAERR
54 #define	EX_DATAERR 1
55 #endif
56 
57 static void help(void);
58 
59 
60 typedef int cmd_fn_t (int argc, char *argv[]);
61 typedef void cmd_usage_t (void);
62 
63 #define	CMDFL_NO_KMOD	0x0001
64 
65 static struct commands {
66 	const char	*name;
67 	cmd_fn_t	*fn;
68 	cmd_usage_t	*usage;
69 	int 		flags;
70 } commands[] = {
71 	{"crypt",	cmd_crypt,	NULL, CMDFL_NO_KMOD},
72 	{"help",	cmd_help,	help_usage, CMDFL_NO_KMOD},
73 	{"login",	cmd_login,	login_usage, 0},
74 	{"logout",	cmd_logout,	logout_usage, 0},
75 	{"logoutall",	cmd_logoutall,	logoutall_usage, 0},
76 	{"lookup",	cmd_lookup,	lookup_usage, CMDFL_NO_KMOD},
77 	{"status",	cmd_status,	status_usage, 0},
78 	{"view",	cmd_view,	view_usage, 0},
79 	{NULL, NULL, NULL, 0}
80 };
81 
82 static struct commands *
83 lookupcmd(const char *name)
84 {
85 	struct commands *cmd;
86 
87 	for (cmd = commands; cmd->name; cmd++) {
88 		if (strcmp(cmd->name, name) == 0)
89 			return (cmd);
90 	}
91 	return (NULL);
92 }
93 
94 int
95 cmd_crypt(int argc, char *argv[])
96 {
97 	char *cp, *psw;
98 
99 	if (argc < 2)
100 		psw = getpassphrase(gettext("Password:"));
101 	else
102 		psw = argv[1];
103 	/* XXX Better to embed malloc/free in smb_simplecrypt? */
104 	cp = malloc(4 + 2 * strlen(psw));
105 	if (cp == NULL)
106 		errx(EX_DATAERR, gettext("out of memory"));
107 	smb_simplecrypt(cp, psw);
108 	printf("%s\n", cp);
109 	free(cp);
110 	return (0);
111 }
112 
113 int
114 cmd_help(int argc, char *argv[])
115 {
116 	struct commands *cmd;
117 	char *cp;
118 
119 	if (argc < 2)
120 		help_usage();
121 	cp = argv[1];
122 	cmd = lookupcmd(cp);
123 	if (cmd == NULL)
124 		errx(EX_DATAERR, gettext("unknown command %s"), cp);
125 	if (cmd->usage == NULL)
126 		errx(EX_DATAERR,
127 		    gettext("no specific help for command %s"), cp);
128 	cmd->usage();
129 	return (0);
130 }
131 
132 int
133 main(int argc, char *argv[])
134 {
135 	struct commands *cmd;
136 	char *cp;
137 	int opt;
138 
139 	(void) setlocale(LC_ALL, "");
140 	(void) textdomain(TEXT_DOMAIN);
141 
142 #ifdef APPLE
143 	dropsuid(); /* see libsmbfs */
144 #endif
145 
146 	if (argc < 2)
147 		help();
148 
149 	while ((opt = getopt(argc, argv, "dhv")) != EOF) {
150 		switch (opt) {
151 		case 'd':
152 			smb_debug++;
153 			break;
154 		case 'h':
155 			help();
156 			/* NOTREACHED */
157 		case 'v':
158 			smb_verbose++;
159 			break;
160 		default:
161 			help();
162 			/* NOTREACHED */
163 		}
164 	}
165 	if (optind >= argc)
166 		help();
167 
168 	cp = argv[optind];
169 	cmd = lookupcmd(cp);
170 	if (cmd == NULL)
171 		errx(EX_DATAERR, gettext("unknown command %s"), cp);
172 
173 	if ((cmd->flags & CMDFL_NO_KMOD) == 0 && smb_lib_init() != 0)
174 		exit(1);
175 
176 	argc -= optind;
177 	argv += optind;
178 	optind = 1;
179 	return (cmd->fn(argc, argv));
180 }
181 
182 static void
183 help(void) {
184 	printf("\n");
185 	printf(gettext("usage: %s [-hv] subcommand [args]\n"), __progname);
186 	printf(gettext("where subcommands are:\n"
187 	" crypt		slightly obscure password\n"
188 	" help		display help on specified subcommand\n"
189 	/* " lc 		display active connections\n" */
190 	" login		login to specified host\n"
191 	" logout 	logout from specified host\n"
192 	" logoutall	logout all users (requires privilege)\n"
193 	" lookup 	resolve NetBIOS name to IP address\n"
194 	/* " print		print file to the specified remote printer\n" */
195 	" status 	resolve IP address or DNS name to NetBIOS names\n"
196 	" view		list resources on specified host\n"
197 	"\n"));
198 	exit(1);
199 }
200 
201 void
202 help_usage(void) {
203 	printf(gettext("usage: smbutil help command\n"));
204 	exit(1);
205 }
206