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 2009 Sun Microsystems, Inc.  All rights reserved.
  35  * Use is subject to license terms.
  36  *
  37  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
  38  */
  39 
  40 #include <sys/param.h>
  41 #include <sys/time.h>
  42 #include <stdio.h>
  43 #include <string.h>
  44 #include <unistd.h>
  45 #include <stdlib.h>
  46 #include <err.h>
  47 #include <sysexits.h>
  48 #include <locale.h>
  49 #include <libintl.h>
  50 
  51 #include <netsmb/smb_lib.h>
  52 
  53 #include "common.h"
  54 
  55 #ifndef EX_DATAERR
  56 #define EX_DATAERR 1
  57 #endif
  58 
  59 static void help(void);
  60 
  61 
  62 typedef int cmd_fn_t (int argc, char *argv[]);
  63 typedef void cmd_usage_t (void);
  64 
  65 #define CMDFL_NO_KMOD   0x0001
  66 
  67 static struct commands {
  68         const char      *name;
  69         cmd_fn_t        *fn;
  70         cmd_usage_t     *usage;
  71         int             flags;
  72 } commands[] = {
  73         {"crypt",       cmd_crypt,      NULL, CMDFL_NO_KMOD},
  74         {"discon",      cmd_discon,     discon_usage, 0},
  75         {"help",        cmd_help,       help_usage, CMDFL_NO_KMOD},
  76         {"info",        cmd_info,       info_usage, 0},
  77         {"login",       cmd_login,      login_usage, 0},
  78         {"logout",      cmd_logout,     logout_usage, 0},
  79         {"logoutall",   cmd_logoutall,  logoutall_usage, 0},
  80         {"lookup",      cmd_lookup,     lookup_usage, CMDFL_NO_KMOD},
  81         {"print",       cmd_print,      print_usage, 0},
  82         {"status",      cmd_status,     status_usage, CMDFL_NO_KMOD},
  83         {"view",        cmd_view,       view_usage, 0},
  84         {NULL, NULL, NULL, 0}
  85 };
  86 
  87 static struct commands *
  88 lookupcmd(const char *name)
  89 {
  90         struct commands *cmd;
  91 
  92         for (cmd = commands; cmd->name; cmd++) {
  93                 if (strcmp(cmd->name, name) == 0)
  94                         return (cmd);
  95         }
  96         return (NULL);
  97 }
  98 
  99 int
 100 cmd_crypt(int argc, char *argv[])
 101 {
 102         char *cp, *psw;
 103 
 104         if (argc < 2)
 105                 psw = getpassphrase(gettext("Password:"));
 106         else
 107                 psw = argv[1];
 108         /* XXX Better to embed malloc/free in smb_simplecrypt? */
 109         cp = malloc(4 + 2 * strlen(psw));
 110         if (cp == NULL)
 111                 errx(EX_DATAERR, gettext("out of memory"));
 112         smb_simplecrypt(cp, psw);
 113         printf("%s\n", cp);
 114         free(cp);
 115         return (0);
 116 }
 117 
 118 int
 119 cmd_help(int argc, char *argv[])
 120 {
 121         struct commands *cmd;
 122         char *cp;
 123 
 124         if (argc < 2)
 125                 help_usage();
 126         cp = argv[1];
 127         cmd = lookupcmd(cp);
 128         if (cmd == NULL)
 129                 errx(EX_DATAERR, gettext("unknown command %s"), cp);
 130         if (cmd->usage == NULL)
 131                 errx(EX_DATAERR,
 132                     gettext("no specific help for command %s"), cp);
 133         cmd->usage();
 134         return (0);
 135 }
 136 
 137 int
 138 main(int argc, char *argv[])
 139 {
 140         struct commands *cmd;
 141         char *cp;
 142         int err, opt;
 143 
 144         (void) setlocale(LC_ALL, "");
 145         (void) textdomain(TEXT_DOMAIN);
 146 
 147 #ifdef APPLE
 148         dropsuid(); /* see libsmbfs */
 149 #endif
 150 
 151         if (argc < 2)
 152                 help();
 153 
 154         while ((opt = getopt(argc, argv, "dhv")) != EOF) {
 155                 switch (opt) {
 156                 case 'd':
 157                         smb_debug++;
 158                         break;
 159                 case 'h':
 160                         help();
 161                         /* NOTREACHED */
 162                 case 'v':
 163                         smb_verbose++;
 164                         break;
 165                 default:
 166                         help();
 167                         /* NOTREACHED */
 168                 }
 169         }
 170         if (optind >= argc)
 171                 help();
 172 
 173         cp = argv[optind];
 174         cmd = lookupcmd(cp);
 175         if (cmd == NULL)
 176                 errx(EX_DATAERR, gettext("unknown command %s"), cp);
 177 
 178         if ((cmd->flags & CMDFL_NO_KMOD) == 0 && smb_lib_init() != 0)
 179                 exit(1);
 180 
 181         argc -= optind;
 182         argv += optind;
 183         optind = 1;
 184         err = cmd->fn(argc, argv);
 185         return ((err) ? 1 : 0);
 186 }
 187 
 188 static void
 189 help(void) {
 190         printf("\n");
 191         printf(gettext("usage: %s [-hv] subcommand [args]\n"), __progname);
 192         printf(gettext("where subcommands are:\n"
 193         " crypt         slightly obscure password\n"
 194         " help          display help on specified subcommand\n"
 195         /* " lc         display active connections\n" */
 196         " info          display server type and version\n"
 197         " login         login to specified host\n"
 198         " logout        logout from specified host\n"
 199         " logoutall     logout all users (requires privilege)\n"
 200         " lookup        resolve NetBIOS name to IP address\n"
 201         " print         print file to the specified remote printer\n"
 202         " status        resolve IP address or DNS name to NetBIOS names\n"
 203         " view          list resources on specified host\n"
 204         "\n"));
 205         exit(1);
 206 }
 207 
 208 void
 209 help_usage(void) {
 210         printf(gettext("usage: smbutil help command\n"));
 211         exit(1);
 212 }