xref: /onnv-gate/usr/src/cmd/fs.d/smbclnt/smbutil/smbutil.c (revision 10023:71bf38dba3d6)
18271SGordon.Ross@Sun.COM /*
28271SGordon.Ross@Sun.COM  * Copyright (c) 2000, Boris Popov
38271SGordon.Ross@Sun.COM  * All rights reserved.
48271SGordon.Ross@Sun.COM  *
58271SGordon.Ross@Sun.COM  * Redistribution and use in source and binary forms, with or without
68271SGordon.Ross@Sun.COM  * modification, are permitted provided that the following conditions
78271SGordon.Ross@Sun.COM  * are met:
88271SGordon.Ross@Sun.COM  * 1. Redistributions of source code must retain the above copyright
98271SGordon.Ross@Sun.COM  *    notice, this list of conditions and the following disclaimer.
108271SGordon.Ross@Sun.COM  * 2. Redistributions in binary form must reproduce the above copyright
118271SGordon.Ross@Sun.COM  *    notice, this list of conditions and the following disclaimer in the
128271SGordon.Ross@Sun.COM  *    documentation and/or other materials provided with the distribution.
138271SGordon.Ross@Sun.COM  * 3. All advertising materials mentioning features or use of this software
148271SGordon.Ross@Sun.COM  *    must display the following acknowledgement:
158271SGordon.Ross@Sun.COM  *    This product includes software developed by Boris Popov.
168271SGordon.Ross@Sun.COM  * 4. Neither the name of the author nor the names of any co-contributors
178271SGordon.Ross@Sun.COM  *    may be used to endorse or promote products derived from this software
188271SGordon.Ross@Sun.COM  *    without specific prior written permission.
198271SGordon.Ross@Sun.COM  *
208271SGordon.Ross@Sun.COM  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
218271SGordon.Ross@Sun.COM  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
228271SGordon.Ross@Sun.COM  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
238271SGordon.Ross@Sun.COM  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
248271SGordon.Ross@Sun.COM  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
258271SGordon.Ross@Sun.COM  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
268271SGordon.Ross@Sun.COM  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
278271SGordon.Ross@Sun.COM  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
288271SGordon.Ross@Sun.COM  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
298271SGordon.Ross@Sun.COM  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
308271SGordon.Ross@Sun.COM  * SUCH DAMAGE.
318271SGordon.Ross@Sun.COM  */
328271SGordon.Ross@Sun.COM 
338271SGordon.Ross@Sun.COM /*
34*10023SGordon.Ross@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
358271SGordon.Ross@Sun.COM  * Use is subject to license terms.
368271SGordon.Ross@Sun.COM  */
376007Sthurlow 
386007Sthurlow #include <sys/param.h>
396007Sthurlow #include <sys/time.h>
406007Sthurlow #include <stdio.h>
416007Sthurlow #include <string.h>
426007Sthurlow #include <unistd.h>
436007Sthurlow #include <stdlib.h>
446007Sthurlow #include <err.h>
456007Sthurlow #include <sysexits.h>
466007Sthurlow #include <locale.h>
476007Sthurlow #include <libintl.h>
486007Sthurlow 
496007Sthurlow #include <netsmb/smb_lib.h>
506007Sthurlow 
516007Sthurlow #include "common.h"
526007Sthurlow 
536007Sthurlow #ifndef EX_DATAERR
546007Sthurlow #define	EX_DATAERR 1
556007Sthurlow #endif
566007Sthurlow 
576007Sthurlow static void help(void);
586007Sthurlow 
596007Sthurlow 
606007Sthurlow typedef int cmd_fn_t (int argc, char *argv[]);
616007Sthurlow typedef void cmd_usage_t (void);
626007Sthurlow 
636007Sthurlow #define	CMDFL_NO_KMOD	0x0001
646007Sthurlow 
656007Sthurlow static struct commands {
666007Sthurlow 	const char	*name;
676007Sthurlow 	cmd_fn_t	*fn;
686007Sthurlow 	cmd_usage_t	*usage;
696007Sthurlow 	int 		flags;
706007Sthurlow } commands[] = {
716007Sthurlow 	{"crypt",	cmd_crypt,	NULL, CMDFL_NO_KMOD},
726007Sthurlow 	{"help",	cmd_help,	help_usage, CMDFL_NO_KMOD},
736007Sthurlow 	{"login",	cmd_login,	login_usage, 0},
746007Sthurlow 	{"logout",	cmd_logout,	logout_usage, 0},
756007Sthurlow 	{"logoutall",	cmd_logoutall,	logoutall_usage, 0},
766007Sthurlow 	{"lookup",	cmd_lookup,	lookup_usage, CMDFL_NO_KMOD},
77*10023SGordon.Ross@Sun.COM 	{"print",	cmd_print,	print_usage, 0},
78*10023SGordon.Ross@Sun.COM 	{"status",	cmd_status,	status_usage, CMDFL_NO_KMOD},
796007Sthurlow 	{"view",	cmd_view,	view_usage, 0},
806007Sthurlow 	{NULL, NULL, NULL, 0}
816007Sthurlow };
826007Sthurlow 
836007Sthurlow static struct commands *
lookupcmd(const char * name)846007Sthurlow lookupcmd(const char *name)
856007Sthurlow {
866007Sthurlow 	struct commands *cmd;
876007Sthurlow 
886007Sthurlow 	for (cmd = commands; cmd->name; cmd++) {
896007Sthurlow 		if (strcmp(cmd->name, name) == 0)
906007Sthurlow 			return (cmd);
916007Sthurlow 	}
926007Sthurlow 	return (NULL);
936007Sthurlow }
946007Sthurlow 
956007Sthurlow int
cmd_crypt(int argc,char * argv[])966007Sthurlow cmd_crypt(int argc, char *argv[])
976007Sthurlow {
986007Sthurlow 	char *cp, *psw;
996007Sthurlow 
1006007Sthurlow 	if (argc < 2)
1016007Sthurlow 		psw = getpassphrase(gettext("Password:"));
1026007Sthurlow 	else
1036007Sthurlow 		psw = argv[1];
1046007Sthurlow 	/* XXX Better to embed malloc/free in smb_simplecrypt? */
1056007Sthurlow 	cp = malloc(4 + 2 * strlen(psw));
1066007Sthurlow 	if (cp == NULL)
1076007Sthurlow 		errx(EX_DATAERR, gettext("out of memory"));
1086007Sthurlow 	smb_simplecrypt(cp, psw);
1096007Sthurlow 	printf("%s\n", cp);
1106007Sthurlow 	free(cp);
1116007Sthurlow 	return (0);
1126007Sthurlow }
1136007Sthurlow 
1146007Sthurlow int
cmd_help(int argc,char * argv[])1156007Sthurlow cmd_help(int argc, char *argv[])
1166007Sthurlow {
1176007Sthurlow 	struct commands *cmd;
1186007Sthurlow 	char *cp;
1196007Sthurlow 
1206007Sthurlow 	if (argc < 2)
1216007Sthurlow 		help_usage();
1226007Sthurlow 	cp = argv[1];
1236007Sthurlow 	cmd = lookupcmd(cp);
1246007Sthurlow 	if (cmd == NULL)
1256007Sthurlow 		errx(EX_DATAERR, gettext("unknown command %s"), cp);
1266007Sthurlow 	if (cmd->usage == NULL)
1276007Sthurlow 		errx(EX_DATAERR,
1286007Sthurlow 		    gettext("no specific help for command %s"), cp);
1296007Sthurlow 	cmd->usage();
1306007Sthurlow 	return (0);
1316007Sthurlow }
1326007Sthurlow 
1336007Sthurlow int
main(int argc,char * argv[])1346007Sthurlow main(int argc, char *argv[])
1356007Sthurlow {
1366007Sthurlow 	struct commands *cmd;
1376007Sthurlow 	char *cp;
138*10023SGordon.Ross@Sun.COM 	int err, opt;
1396007Sthurlow 
1406007Sthurlow 	(void) setlocale(LC_ALL, "");
1416007Sthurlow 	(void) textdomain(TEXT_DOMAIN);
1426007Sthurlow 
1438271SGordon.Ross@Sun.COM #ifdef APPLE
1448271SGordon.Ross@Sun.COM 	dropsuid(); /* see libsmbfs */
1458271SGordon.Ross@Sun.COM #endif
1466007Sthurlow 
1476007Sthurlow 	if (argc < 2)
1486007Sthurlow 		help();
1496007Sthurlow 
1506007Sthurlow 	while ((opt = getopt(argc, argv, "dhv")) != EOF) {
1516007Sthurlow 		switch (opt) {
1526007Sthurlow 		case 'd':
1536007Sthurlow 			smb_debug++;
1546007Sthurlow 			break;
1556007Sthurlow 		case 'h':
1566007Sthurlow 			help();
1576007Sthurlow 			/* NOTREACHED */
1586007Sthurlow 		case 'v':
1596007Sthurlow 			smb_verbose++;
1606007Sthurlow 			break;
1616007Sthurlow 		default:
1626007Sthurlow 			help();
1636007Sthurlow 			/* NOTREACHED */
1646007Sthurlow 		}
1656007Sthurlow 	}
1666007Sthurlow 	if (optind >= argc)
1676007Sthurlow 		help();
1686007Sthurlow 
1696007Sthurlow 	cp = argv[optind];
1706007Sthurlow 	cmd = lookupcmd(cp);
1716007Sthurlow 	if (cmd == NULL)
1726007Sthurlow 		errx(EX_DATAERR, gettext("unknown command %s"), cp);
1736007Sthurlow 
1746007Sthurlow 	if ((cmd->flags & CMDFL_NO_KMOD) == 0 && smb_lib_init() != 0)
1756007Sthurlow 		exit(1);
1766007Sthurlow 
1776007Sthurlow 	argc -= optind;
1786007Sthurlow 	argv += optind;
1796007Sthurlow 	optind = 1;
180*10023SGordon.Ross@Sun.COM 	err = cmd->fn(argc, argv);
181*10023SGordon.Ross@Sun.COM 	return ((err) ? 1 : 0);
1826007Sthurlow }
1836007Sthurlow 
1846007Sthurlow static void
help(void)1856007Sthurlow help(void) {
1866007Sthurlow 	printf("\n");
1876007Sthurlow 	printf(gettext("usage: %s [-hv] subcommand [args]\n"), __progname);
1886007Sthurlow 	printf(gettext("where subcommands are:\n"
1896007Sthurlow 	" crypt		slightly obscure password\n"
1906007Sthurlow 	" help		display help on specified subcommand\n"
1916007Sthurlow 	/* " lc 		display active connections\n" */
1926007Sthurlow 	" login		login to specified host\n"
1936007Sthurlow 	" logout 	logout from specified host\n"
1946007Sthurlow 	" logoutall	logout all users (requires privilege)\n"
1956007Sthurlow 	" lookup 	resolve NetBIOS name to IP address\n"
196*10023SGordon.Ross@Sun.COM 	" print		print file to the specified remote printer\n"
1976007Sthurlow 	" status 	resolve IP address or DNS name to NetBIOS names\n"
1986007Sthurlow 	" view		list resources on specified host\n"
1996007Sthurlow 	"\n"));
2006007Sthurlow 	exit(1);
2016007Sthurlow }
2026007Sthurlow 
2036007Sthurlow void
help_usage(void)2046007Sthurlow help_usage(void) {
2056007Sthurlow 	printf(gettext("usage: smbutil help command\n"));
2066007Sthurlow 	exit(1);
2076007Sthurlow }
208