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 /*
34*613a2f6bSGordon Ross * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
359c9af259SGordon Ross * Use is subject to license terms.
369c9af259SGordon Ross */
374bff34e3Sthurlow
384bff34e3Sthurlow #include <sys/param.h>
394bff34e3Sthurlow #include <sys/time.h>
404bff34e3Sthurlow #include <stdio.h>
414bff34e3Sthurlow #include <string.h>
424bff34e3Sthurlow #include <unistd.h>
434bff34e3Sthurlow #include <stdlib.h>
444bff34e3Sthurlow #include <err.h>
454bff34e3Sthurlow #include <sysexits.h>
464bff34e3Sthurlow #include <locale.h>
474bff34e3Sthurlow #include <libintl.h>
484bff34e3Sthurlow
494bff34e3Sthurlow #include <netsmb/smb_lib.h>
504bff34e3Sthurlow
514bff34e3Sthurlow #include "common.h"
524bff34e3Sthurlow
534bff34e3Sthurlow #ifndef EX_DATAERR
544bff34e3Sthurlow #define EX_DATAERR 1
554bff34e3Sthurlow #endif
564bff34e3Sthurlow
574bff34e3Sthurlow static void help(void);
584bff34e3Sthurlow
594bff34e3Sthurlow
604bff34e3Sthurlow typedef int cmd_fn_t (int argc, char *argv[]);
614bff34e3Sthurlow typedef void cmd_usage_t (void);
624bff34e3Sthurlow
634bff34e3Sthurlow #define CMDFL_NO_KMOD 0x0001
644bff34e3Sthurlow
654bff34e3Sthurlow static struct commands {
664bff34e3Sthurlow const char *name;
674bff34e3Sthurlow cmd_fn_t *fn;
684bff34e3Sthurlow cmd_usage_t *usage;
694bff34e3Sthurlow int flags;
704bff34e3Sthurlow } commands[] = {
714bff34e3Sthurlow {"crypt", cmd_crypt, NULL, CMDFL_NO_KMOD},
724bff34e3Sthurlow {"help", cmd_help, help_usage, CMDFL_NO_KMOD},
734bff34e3Sthurlow {"login", cmd_login, login_usage, 0},
744bff34e3Sthurlow {"logout", cmd_logout, logout_usage, 0},
754bff34e3Sthurlow {"logoutall", cmd_logoutall, logoutall_usage, 0},
764bff34e3Sthurlow {"lookup", cmd_lookup, lookup_usage, CMDFL_NO_KMOD},
77*613a2f6bSGordon Ross {"print", cmd_print, print_usage, 0},
78*613a2f6bSGordon Ross {"status", cmd_status, status_usage, CMDFL_NO_KMOD},
794bff34e3Sthurlow {"view", cmd_view, view_usage, 0},
804bff34e3Sthurlow {NULL, NULL, NULL, 0}
814bff34e3Sthurlow };
824bff34e3Sthurlow
834bff34e3Sthurlow static struct commands *
lookupcmd(const char * name)844bff34e3Sthurlow lookupcmd(const char *name)
854bff34e3Sthurlow {
864bff34e3Sthurlow struct commands *cmd;
874bff34e3Sthurlow
884bff34e3Sthurlow for (cmd = commands; cmd->name; cmd++) {
894bff34e3Sthurlow if (strcmp(cmd->name, name) == 0)
904bff34e3Sthurlow return (cmd);
914bff34e3Sthurlow }
924bff34e3Sthurlow return (NULL);
934bff34e3Sthurlow }
944bff34e3Sthurlow
954bff34e3Sthurlow int
cmd_crypt(int argc,char * argv[])964bff34e3Sthurlow cmd_crypt(int argc, char *argv[])
974bff34e3Sthurlow {
984bff34e3Sthurlow char *cp, *psw;
994bff34e3Sthurlow
1004bff34e3Sthurlow if (argc < 2)
1014bff34e3Sthurlow psw = getpassphrase(gettext("Password:"));
1024bff34e3Sthurlow else
1034bff34e3Sthurlow psw = argv[1];
1044bff34e3Sthurlow /* XXX Better to embed malloc/free in smb_simplecrypt? */
1054bff34e3Sthurlow cp = malloc(4 + 2 * strlen(psw));
1064bff34e3Sthurlow if (cp == NULL)
1074bff34e3Sthurlow errx(EX_DATAERR, gettext("out of memory"));
1084bff34e3Sthurlow smb_simplecrypt(cp, psw);
1094bff34e3Sthurlow printf("%s\n", cp);
1104bff34e3Sthurlow free(cp);
1114bff34e3Sthurlow return (0);
1124bff34e3Sthurlow }
1134bff34e3Sthurlow
1144bff34e3Sthurlow int
cmd_help(int argc,char * argv[])1154bff34e3Sthurlow cmd_help(int argc, char *argv[])
1164bff34e3Sthurlow {
1174bff34e3Sthurlow struct commands *cmd;
1184bff34e3Sthurlow char *cp;
1194bff34e3Sthurlow
1204bff34e3Sthurlow if (argc < 2)
1214bff34e3Sthurlow help_usage();
1224bff34e3Sthurlow cp = argv[1];
1234bff34e3Sthurlow cmd = lookupcmd(cp);
1244bff34e3Sthurlow if (cmd == NULL)
1254bff34e3Sthurlow errx(EX_DATAERR, gettext("unknown command %s"), cp);
1264bff34e3Sthurlow if (cmd->usage == NULL)
1274bff34e3Sthurlow errx(EX_DATAERR,
1284bff34e3Sthurlow gettext("no specific help for command %s"), cp);
1294bff34e3Sthurlow cmd->usage();
1304bff34e3Sthurlow return (0);
1314bff34e3Sthurlow }
1324bff34e3Sthurlow
1334bff34e3Sthurlow int
main(int argc,char * argv[])1344bff34e3Sthurlow main(int argc, char *argv[])
1354bff34e3Sthurlow {
1364bff34e3Sthurlow struct commands *cmd;
1374bff34e3Sthurlow char *cp;
138*613a2f6bSGordon Ross int err, opt;
1394bff34e3Sthurlow
1404bff34e3Sthurlow (void) setlocale(LC_ALL, "");
1414bff34e3Sthurlow (void) textdomain(TEXT_DOMAIN);
1424bff34e3Sthurlow
1439c9af259SGordon Ross #ifdef APPLE
1449c9af259SGordon Ross dropsuid(); /* see libsmbfs */
1459c9af259SGordon Ross #endif
1464bff34e3Sthurlow
1474bff34e3Sthurlow if (argc < 2)
1484bff34e3Sthurlow help();
1494bff34e3Sthurlow
1504bff34e3Sthurlow while ((opt = getopt(argc, argv, "dhv")) != EOF) {
1514bff34e3Sthurlow switch (opt) {
1524bff34e3Sthurlow case 'd':
1534bff34e3Sthurlow smb_debug++;
1544bff34e3Sthurlow break;
1554bff34e3Sthurlow case 'h':
1564bff34e3Sthurlow help();
1574bff34e3Sthurlow /* NOTREACHED */
1584bff34e3Sthurlow case 'v':
1594bff34e3Sthurlow smb_verbose++;
1604bff34e3Sthurlow break;
1614bff34e3Sthurlow default:
1624bff34e3Sthurlow help();
1634bff34e3Sthurlow /* NOTREACHED */
1644bff34e3Sthurlow }
1654bff34e3Sthurlow }
1664bff34e3Sthurlow if (optind >= argc)
1674bff34e3Sthurlow help();
1684bff34e3Sthurlow
1694bff34e3Sthurlow cp = argv[optind];
1704bff34e3Sthurlow cmd = lookupcmd(cp);
1714bff34e3Sthurlow if (cmd == NULL)
1724bff34e3Sthurlow errx(EX_DATAERR, gettext("unknown command %s"), cp);
1734bff34e3Sthurlow
1744bff34e3Sthurlow if ((cmd->flags & CMDFL_NO_KMOD) == 0 && smb_lib_init() != 0)
1754bff34e3Sthurlow exit(1);
1764bff34e3Sthurlow
1774bff34e3Sthurlow argc -= optind;
1784bff34e3Sthurlow argv += optind;
1794bff34e3Sthurlow optind = 1;
180*613a2f6bSGordon Ross err = cmd->fn(argc, argv);
181*613a2f6bSGordon Ross return ((err) ? 1 : 0);
1824bff34e3Sthurlow }
1834bff34e3Sthurlow
1844bff34e3Sthurlow static void
help(void)1854bff34e3Sthurlow help(void) {
1864bff34e3Sthurlow printf("\n");
1874bff34e3Sthurlow printf(gettext("usage: %s [-hv] subcommand [args]\n"), __progname);
1884bff34e3Sthurlow printf(gettext("where subcommands are:\n"
1894bff34e3Sthurlow " crypt slightly obscure password\n"
1904bff34e3Sthurlow " help display help on specified subcommand\n"
1914bff34e3Sthurlow /* " lc display active connections\n" */
1924bff34e3Sthurlow " login login to specified host\n"
1934bff34e3Sthurlow " logout logout from specified host\n"
1944bff34e3Sthurlow " logoutall logout all users (requires privilege)\n"
1954bff34e3Sthurlow " lookup resolve NetBIOS name to IP address\n"
196*613a2f6bSGordon Ross " print print file to the specified remote printer\n"
1974bff34e3Sthurlow " status resolve IP address or DNS name to NetBIOS names\n"
1984bff34e3Sthurlow " view list resources on specified host\n"
1994bff34e3Sthurlow "\n"));
2004bff34e3Sthurlow exit(1);
2014bff34e3Sthurlow }
2024bff34e3Sthurlow
2034bff34e3Sthurlow void
help_usage(void)2044bff34e3Sthurlow help_usage(void) {
2054bff34e3Sthurlow printf(gettext("usage: smbutil help command\n"));
2064bff34e3Sthurlow exit(1);
2074bff34e3Sthurlow }
208