19c9af259SGordon Ross /*
29c9af259SGordon Ross * Copyright (c) 2000, Boris Popov
39c9af259SGordon Ross * All rights reserved.
49c9af259SGordon Ross *
59c9af259SGordon Ross * Redistribution and use in source and binary forms, with or without
69c9af259SGordon Ross * modification, are permitted provided that the following conditions
79c9af259SGordon Ross * are met:
89c9af259SGordon Ross * 1. Redistributions of source code must retain the above copyright
99c9af259SGordon Ross * notice, this list of conditions and the following disclaimer.
109c9af259SGordon Ross * 2. Redistributions in binary form must reproduce the above copyright
119c9af259SGordon Ross * notice, this list of conditions and the following disclaimer in the
129c9af259SGordon Ross * documentation and/or other materials provided with the distribution.
139c9af259SGordon Ross * 3. All advertising materials mentioning features or use of this software
149c9af259SGordon Ross * must display the following acknowledgement:
159c9af259SGordon Ross * This product includes software developed by Boris Popov.
169c9af259SGordon Ross * 4. Neither the name of the author nor the names of any co-contributors
179c9af259SGordon Ross * may be used to endorse or promote products derived from this software
189c9af259SGordon Ross * without specific prior written permission.
199c9af259SGordon Ross *
209c9af259SGordon Ross * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
219c9af259SGordon Ross * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
229c9af259SGordon Ross * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
239c9af259SGordon Ross * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
249c9af259SGordon Ross * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
259c9af259SGordon Ross * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
269c9af259SGordon Ross * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
279c9af259SGordon Ross * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
289c9af259SGordon Ross * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
299c9af259SGordon Ross * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
309c9af259SGordon Ross * SUCH DAMAGE.
319c9af259SGordon Ross */
329c9af259SGordon Ross
339c9af259SGordon Ross /*
34613a2f6bSGordon Ross * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
359c9af259SGordon Ross * Use is subject to license terms.
367d815089SGordon Ross *
37*ce43d0aeSYuri Pankov * Copyright 2018 Nexenta Systems, Inc.
389c9af259SGordon Ross */
394bff34e3Sthurlow
404bff34e3Sthurlow #include <sys/param.h>
414bff34e3Sthurlow #include <sys/time.h>
424bff34e3Sthurlow #include <stdio.h>
434bff34e3Sthurlow #include <string.h>
444bff34e3Sthurlow #include <unistd.h>
454bff34e3Sthurlow #include <stdlib.h>
464bff34e3Sthurlow #include <err.h>
474bff34e3Sthurlow #include <sysexits.h>
484bff34e3Sthurlow #include <locale.h>
494bff34e3Sthurlow #include <libintl.h>
504bff34e3Sthurlow
514bff34e3Sthurlow #include <netsmb/smb_lib.h>
524bff34e3Sthurlow
534bff34e3Sthurlow #include "common.h"
544bff34e3Sthurlow
554bff34e3Sthurlow #ifndef EX_DATAERR
564bff34e3Sthurlow #define EX_DATAERR 1
574bff34e3Sthurlow #endif
584bff34e3Sthurlow
59f45bbb21SToomas Soome static void help(void) __NORETURN;
604bff34e3Sthurlow
614bff34e3Sthurlow typedef int cmd_fn_t (int argc, char *argv[]);
624bff34e3Sthurlow typedef void cmd_usage_t (void);
634bff34e3Sthurlow
644bff34e3Sthurlow #define CMDFL_NO_KMOD 0x0001
654bff34e3Sthurlow
664bff34e3Sthurlow static struct commands {
674bff34e3Sthurlow const char *name;
684bff34e3Sthurlow cmd_fn_t *fn;
694bff34e3Sthurlow cmd_usage_t *usage;
704bff34e3Sthurlow int flags;
714bff34e3Sthurlow } commands[] = {
724bff34e3Sthurlow {"crypt", cmd_crypt, NULL, CMDFL_NO_KMOD},
73a6d10110SGordon Ross {"discon", cmd_discon, discon_usage, 0},
744bff34e3Sthurlow {"help", cmd_help, help_usage, CMDFL_NO_KMOD},
757d815089SGordon Ross {"info", cmd_info, info_usage, 0},
764bff34e3Sthurlow {"login", cmd_login, login_usage, 0},
774bff34e3Sthurlow {"logout", cmd_logout, logout_usage, 0},
784bff34e3Sthurlow {"logoutall", cmd_logoutall, logoutall_usage, 0},
794bff34e3Sthurlow {"lookup", cmd_lookup, lookup_usage, CMDFL_NO_KMOD},
80613a2f6bSGordon Ross {"print", cmd_print, print_usage, 0},
81613a2f6bSGordon Ross {"status", cmd_status, status_usage, CMDFL_NO_KMOD},
824bff34e3Sthurlow {"view", cmd_view, view_usage, 0},
834bff34e3Sthurlow {NULL, NULL, NULL, 0}
844bff34e3Sthurlow };
854bff34e3Sthurlow
864bff34e3Sthurlow static struct commands *
lookupcmd(const char * name)874bff34e3Sthurlow lookupcmd(const char *name)
884bff34e3Sthurlow {
894bff34e3Sthurlow struct commands *cmd;
904bff34e3Sthurlow
914bff34e3Sthurlow for (cmd = commands; cmd->name; cmd++) {
924bff34e3Sthurlow if (strcmp(cmd->name, name) == 0)
934bff34e3Sthurlow return (cmd);
944bff34e3Sthurlow }
954bff34e3Sthurlow return (NULL);
964bff34e3Sthurlow }
974bff34e3Sthurlow
984bff34e3Sthurlow int
cmd_crypt(int argc,char * argv[])994bff34e3Sthurlow cmd_crypt(int argc, char *argv[])
1004bff34e3Sthurlow {
1014bff34e3Sthurlow char *cp, *psw;
1024bff34e3Sthurlow
1034bff34e3Sthurlow if (argc < 2)
1044bff34e3Sthurlow psw = getpassphrase(gettext("Password:"));
1054bff34e3Sthurlow else
1064bff34e3Sthurlow psw = argv[1];
1074bff34e3Sthurlow /* XXX Better to embed malloc/free in smb_simplecrypt? */
1084bff34e3Sthurlow cp = malloc(4 + 2 * strlen(psw));
1094bff34e3Sthurlow if (cp == NULL)
1104bff34e3Sthurlow errx(EX_DATAERR, gettext("out of memory"));
1114bff34e3Sthurlow smb_simplecrypt(cp, psw);
1124bff34e3Sthurlow printf("%s\n", cp);
1134bff34e3Sthurlow free(cp);
1144bff34e3Sthurlow return (0);
1154bff34e3Sthurlow }
1164bff34e3Sthurlow
1174bff34e3Sthurlow int
cmd_help(int argc,char * argv[])1184bff34e3Sthurlow cmd_help(int argc, char *argv[])
1194bff34e3Sthurlow {
1204bff34e3Sthurlow struct commands *cmd;
1214bff34e3Sthurlow char *cp;
1224bff34e3Sthurlow
1234bff34e3Sthurlow if (argc < 2)
1244bff34e3Sthurlow help_usage();
1254bff34e3Sthurlow cp = argv[1];
1264bff34e3Sthurlow cmd = lookupcmd(cp);
1274bff34e3Sthurlow if (cmd == NULL)
1284bff34e3Sthurlow errx(EX_DATAERR, gettext("unknown command %s"), cp);
1294bff34e3Sthurlow if (cmd->usage == NULL)
1304bff34e3Sthurlow errx(EX_DATAERR,
1314bff34e3Sthurlow gettext("no specific help for command %s"), cp);
1324bff34e3Sthurlow cmd->usage();
1334bff34e3Sthurlow return (0);
1344bff34e3Sthurlow }
1354bff34e3Sthurlow
1364bff34e3Sthurlow int
main(int argc,char * argv[])1374bff34e3Sthurlow main(int argc, char *argv[])
1384bff34e3Sthurlow {
1394bff34e3Sthurlow struct commands *cmd;
1404bff34e3Sthurlow char *cp;
141613a2f6bSGordon Ross int err, opt;
1424bff34e3Sthurlow
1434bff34e3Sthurlow (void) setlocale(LC_ALL, "");
1444bff34e3Sthurlow (void) textdomain(TEXT_DOMAIN);
1454bff34e3Sthurlow
1469c9af259SGordon Ross #ifdef APPLE
1479c9af259SGordon Ross dropsuid(); /* see libsmbfs */
1489c9af259SGordon Ross #endif
1494bff34e3Sthurlow
1504bff34e3Sthurlow if (argc < 2)
1514bff34e3Sthurlow help();
1524bff34e3Sthurlow
1534bff34e3Sthurlow while ((opt = getopt(argc, argv, "dhv")) != EOF) {
1544bff34e3Sthurlow switch (opt) {
1554bff34e3Sthurlow case 'd':
1564bff34e3Sthurlow smb_debug++;
1574bff34e3Sthurlow break;
1584bff34e3Sthurlow case 'h':
1594bff34e3Sthurlow help();
1604bff34e3Sthurlow /* NOTREACHED */
1614bff34e3Sthurlow case 'v':
1624bff34e3Sthurlow smb_verbose++;
1634bff34e3Sthurlow break;
1644bff34e3Sthurlow default:
1654bff34e3Sthurlow help();
1664bff34e3Sthurlow /* NOTREACHED */
1674bff34e3Sthurlow }
1684bff34e3Sthurlow }
1694bff34e3Sthurlow if (optind >= argc)
1704bff34e3Sthurlow help();
1714bff34e3Sthurlow
1724bff34e3Sthurlow cp = argv[optind];
1734bff34e3Sthurlow cmd = lookupcmd(cp);
1744bff34e3Sthurlow if (cmd == NULL)
1754bff34e3Sthurlow errx(EX_DATAERR, gettext("unknown command %s"), cp);
1764bff34e3Sthurlow
1774bff34e3Sthurlow if ((cmd->flags & CMDFL_NO_KMOD) == 0 && smb_lib_init() != 0)
1784bff34e3Sthurlow exit(1);
1794bff34e3Sthurlow
1804bff34e3Sthurlow argc -= optind;
1814bff34e3Sthurlow argv += optind;
1824bff34e3Sthurlow optind = 1;
183613a2f6bSGordon Ross err = cmd->fn(argc, argv);
184613a2f6bSGordon Ross return ((err) ? 1 : 0);
1854bff34e3Sthurlow }
1864bff34e3Sthurlow
1874bff34e3Sthurlow static void
help(void)188a6d10110SGordon Ross help(void)
189a6d10110SGordon Ross {
1904bff34e3Sthurlow printf("\n");
1914bff34e3Sthurlow printf(gettext("usage: %s [-hv] subcommand [args]\n"), __progname);
1924bff34e3Sthurlow printf(gettext("where subcommands are:\n"
1934bff34e3Sthurlow " crypt slightly obscure password\n"
1944bff34e3Sthurlow " help display help on specified subcommand\n"
1957d815089SGordon Ross " info display server type and version\n"
1964bff34e3Sthurlow " login login to specified host\n"
1974bff34e3Sthurlow " logout logout from specified host\n"
1984bff34e3Sthurlow " logoutall logout all users (requires privilege)\n"
1994bff34e3Sthurlow " lookup resolve NetBIOS name to IP address\n"
200613a2f6bSGordon Ross " print print file to the specified remote printer\n"
2014bff34e3Sthurlow " status resolve IP address or DNS name to NetBIOS names\n"
2024bff34e3Sthurlow " view list resources on specified host\n"
2034bff34e3Sthurlow "\n"));
2044bff34e3Sthurlow exit(1);
2054bff34e3Sthurlow }
2064bff34e3Sthurlow
2074bff34e3Sthurlow void
help_usage(void)208a6d10110SGordon Ross help_usage(void)
209a6d10110SGordon Ross {
2104bff34e3Sthurlow printf(gettext("usage: smbutil help command\n"));
2114bff34e3Sthurlow exit(1);
2124bff34e3Sthurlow }
213