Print this page
3328 smbutil view does't work with Win2008 and later
requires 1575 untangle libmlrpc ...


  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  * $Id: view.c,v 1.9 2004/12/13 00:25:39 lindak Exp $
  33  */
  34 
  35 /*
  36  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.

  37  */
  38 
  39 #include <sys/types.h>
  40 #include <errno.h>
  41 #include <stdio.h>
  42 #include <err.h>
  43 #include <unistd.h>
  44 #include <strings.h>
  45 #include <stdlib.h>
  46 #include <sysexits.h>
  47 #include <libintl.h>
  48 
  49 #include <netsmb/smb.h>
  50 #include <netsmb/smb_lib.h>
  51 #include <netsmb/smb_netshareenum.h>
  52 
  53 #include "common.h"
  54 
  55 int enum_shares(smb_ctx_t *);
  56 void print_shares(int, int, struct share_info *);
  57 
  58 void
  59 view_usage(void)
  60 {
  61         printf(gettext("usage: smbutil view [connection options] //"
  62             "[workgroup;][user[:password]@]server\n"));
  63         exit(1);
  64 }
  65 
  66 int
  67 cmd_view(int argc, char *argv[])
  68 {
  69         struct smb_ctx *ctx;
  70         int error, err2, opt;
  71 
  72         if (argc < 2)
  73                 view_usage();
  74 
  75         error = smb_ctx_alloc(&ctx);
  76         if (error)
  77                 return (error);
  78 
  79         error = smb_ctx_scan_argv(ctx, argc, argv,
  80             SMBL_SERVER, SMBL_SERVER, USE_WILDCARD);
  81         if (error)
  82                 return (error);
  83 
  84         error = smb_ctx_readrc(ctx);
  85         if (error)
  86                 return (error);
  87 
  88         while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) {
  89                 if (opt == '?')
  90                         view_usage();









  91                 error = smb_ctx_opt(ctx, opt, optarg);
  92                 if (error)
  93                         return (error);
  94         }
  95 
  96         smb_ctx_setshare(ctx, "IPC$", USE_IPC);
  97 
  98         /*
  99          * Resolve the server address,
 100          * setup derived defaults.
 101          */
 102         error = smb_ctx_resolve(ctx);
 103         if (error)
 104                 return (error);
 105 
 106         /*
 107          * Have server, share, etc. from above:
 108          * smb_ctx_scan_argv, option settings.
 109          * Get the session and tree.
 110          */
 111 again:
 112         error = smb_ctx_get_ssn(ctx);
 113         if (error == EAUTH) {
 114                 err2 = smb_get_authentication(ctx);
 115                 if (err2 == 0)
 116                         goto again;
 117         }
 118         if (error) {
 119                 smb_error(gettext("//%s: login failed"),
 120                     error, ctx->ct_fullserver);
 121                 return (error);
 122         }
 123 
 124         error = smb_ctx_get_tree(ctx);
 125         if (error) {
 126                 smb_error(gettext("//%s/%s: tree connect failed"),
 127                     error, ctx->ct_fullserver, ctx->ct_origshare);
 128                 return (error);
 129         }
 130 
 131         /*
 132          * Have IPC$ tcon, now list shares.
 133          * This prints its own errors.
 134          */
 135         error = enum_shares(ctx);
 136         if (error)
 137                 return (error);

 138 

 139         smb_ctx_free(ctx);
 140         return (0);
 141 }
 142 
 143 #ifdef I18N     /* not defined, put here so xgettext(1) can find strings */
 144 static char *shtype[] = {
 145         gettext("disk"),
 146         gettext("printer"),
 147         gettext("device"),      /* Communications device */
 148         gettext("IPC"),         /* Inter process communication */
 149         gettext("unknown")
 150 };
 151 #else
 152 static char *shtype[] = {
 153         "disk",
 154         "printer",
 155         "device",               /* Communications device */
 156         "IPC",                  /* IPC Inter process communication */
 157         "unknown"
 158 };
 159 #endif
 160 
 161 int
 162 enum_shares(smb_ctx_t *ctx)
 163 {
 164         struct share_info *share_info;
 165         int error, entries, total;
 166 
 167         /*
 168          * XXX: Later, try RPC first,
 169          * then fall back to RAP...
 170          */
 171         error = smb_netshareenum(ctx, &entries, &total, &share_info);
 172         if (error) {
 173                 smb_error(gettext("//%s failed to list shares"),
 174                     error, ctx->ct_fullserver);
 175                 return (error);
 176         }
 177         print_shares(entries, total, share_info);
 178         return (0);
 179 }
 180 
 181 void
 182 print_shares(int entries, int total,
 183         struct share_info *share_info)
 184 {
 185         struct share_info *ep;
 186         int i;
 187 

 188         printf(gettext("Share        Type       Comment\n"));
 189         printf("-------------------------------\n");
 190 
 191         for (ep = share_info, i = 0; i < entries; i++, ep++) {
 192                 int sti = ep->type & STYPE_MASK;
 193                 if (sti > STYPE_UNKNOWN)
 194                         sti = STYPE_UNKNOWN;
 195                 printf("%-12s %-10s %s\n", ep->netname,
 196                     gettext(shtype[sti]),
 197                     ep->remark ? ep->remark : "");
 198                 free(ep->netname);
 199                 free(ep->remark);
 200         }
 201         printf(gettext("\n%d shares listed from %d available\n"),
 202             entries, total);
 203 
 204         free(share_info);








 205 }


  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  * $Id: view.c,v 1.9 2004/12/13 00:25:39 lindak Exp $
  33  */
  34 
  35 /*
  36  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  37  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
  38  */
  39 
  40 #include <sys/types.h>
  41 #include <errno.h>
  42 #include <stdio.h>
  43 #include <err.h>
  44 #include <unistd.h>
  45 #include <strings.h>
  46 #include <stdlib.h>
  47 #include <sysexits.h>
  48 #include <libintl.h>
  49 
  50 #include <netsmb/smb.h>
  51 #include <netsmb/smb_lib.h>


  52 #include "common.h"
  53 
  54 static int use_rap;

  55 
  56 void
  57 view_usage(void)
  58 {
  59         printf(gettext("usage: smbutil view [connection options] //"
  60             "[workgroup;][user[:password]@]server\n"));
  61         exit(1);
  62 }
  63 
  64 int
  65 cmd_view(int argc, char *argv[])
  66 {
  67         struct smb_ctx *ctx;
  68         int error, err2, opt;
  69 
  70         if (argc < 2)
  71                 view_usage();
  72 
  73         error = smb_ctx_alloc(&ctx);
  74         if (error)
  75                 return (error);
  76 
  77         error = smb_ctx_scan_argv(ctx, argc, argv,
  78             SMBL_SERVER, SMBL_SERVER, USE_WILDCARD);
  79         if (error)
  80                 goto out;
  81 
  82         error = smb_ctx_readrc(ctx);
  83         if (error)
  84                 goto out;
  85 
  86         while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) {
  87                 if (opt == '?')
  88                         view_usage();
  89                 /*
  90                  * This is an undocumented option, just for testing.
  91                  * Use the old LanMan Remote API Protocol (RAP) for
  92                  * enumerating shares.
  93                  */
  94                 if (opt == 'B') {
  95                         use_rap++;
  96                         continue;
  97                 }
  98                 error = smb_ctx_opt(ctx, opt, optarg);
  99                 if (error)
 100                         goto out;
 101         }
 102 
 103         smb_ctx_setshare(ctx, "IPC$", USE_IPC);
 104 
 105         /*
 106          * Resolve the server address,
 107          * setup derived defaults.
 108          */
 109         error = smb_ctx_resolve(ctx);
 110         if (error)
 111                 goto out;
 112 
 113         /*
 114          * Have server, share, etc. from above:
 115          * smb_ctx_scan_argv, option settings.
 116          * Get the session and tree.
 117          */
 118 again:
 119         error = smb_ctx_get_ssn(ctx);
 120         if (error == EAUTH) {
 121                 err2 = smb_get_authentication(ctx);
 122                 if (err2 == 0)
 123                         goto again;
 124         }
 125         if (error) {
 126                 smb_error(gettext("//%s: login failed"),
 127                     error, ctx->ct_fullserver);
 128                 goto out;
 129         }
 130 
 131         error = smb_ctx_get_tree(ctx);
 132         if (error) {
 133                 smb_error(gettext("//%s/%s: tree connect failed"),
 134                     error, ctx->ct_fullserver, ctx->ct_origshare);
 135                 goto out;
 136         }
 137 
 138         /*
 139          * Have IPC$ tcon, now list shares.
 140          * Try RPC; if that fails, do RAP.
 141          */
 142         if (!use_rap)
 143                 error = share_enum_rpc(ctx, ctx->ct_fullserver);
 144         if (error || use_rap)
 145                 error = share_enum_rap(ctx);
 146 
 147 out:
 148         smb_ctx_free(ctx);
 149         return (0);
 150 }
 151 
 152 #ifdef I18N     /* not defined, put here so xgettext(1) can find strings */
 153 static char *shtype[] = {
 154         gettext("disk"),
 155         gettext("printer"),
 156         gettext("device"),      /* Communications device */
 157         gettext("IPC"),         /* Inter process communication */
 158         gettext("unknown")
 159 };
 160 #else
 161 static char *shtype[] = {
 162         "disk",
 163         "printer",
 164         "device",               /* Communications device */
 165         "IPC",                  /* IPC Inter process communication */
 166         "unknown"
 167 };
 168 #endif
 169 
 170 /*
 171  * Print one line of the share list, or
 172  * if SHARE is null, print the header line.






 173  */










 174 void
 175 view_print_share(char *share, int type, char *comment)

 176 {
 177         char *stname;
 178         int stindex;
 179 
 180         if (share == NULL) {
 181                 printf(gettext("Share        Type       Comment\n"));
 182                 printf("-------------------------------\n");
 183                 return;









 184         }


 185 
 186         stindex = type & STYPE_MASK;
 187         if (stindex > STYPE_UNKNOWN)
 188                 stindex = STYPE_UNKNOWN;
 189         stname = gettext(shtype[stindex]);
 190 
 191         if (comment == NULL)
 192                 comment = "";
 193 
 194         printf("%-12s %-10s %s\n", share, stname, comment);
 195 }