blob: bf3397c166268d50132c709c4410deabb20f38bb [file] [log] [blame]
thurlow4bff34e2008-02-13 19:51:22 -08001/*
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 * $Id: view.c,v 1.9 2004/12/13 00:25:39 lindak Exp $
33 */
34
Gordon Ross613a2f62009-07-02 12:58:38 -040035/*
Gordon Rossae3d7f92010-07-27 17:15:36 -070036 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
Gordon Ross7d815082013-01-05 16:44:12 -050037 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
Gordon Ross613a2f62009-07-02 12:58:38 -040038 */
39
40#include <sys/types.h>
41#include <errno.h>
thurlow4bff34e2008-02-13 19:51:22 -080042#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
Gordon Ross613a2f62009-07-02 12:58:38 -040050#include <netsmb/smb.h>
thurlow4bff34e2008-02-13 19:51:22 -080051#include <netsmb/smb_lib.h>
thurlow4bff34e2008-02-13 19:51:22 -080052#include "common.h"
53
Gordon Ross7d815082013-01-05 16:44:12 -050054static int use_rap;
Gordon Ross613a2f62009-07-02 12:58:38 -040055
56void
57view_usage(void)
58{
59 printf(gettext("usage: smbutil view [connection options] //"
60 "[workgroup;][user[:password]@]server\n"));
61 exit(1);
62}
63
64int
65cmd_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)
Gordon Ross7d815082013-01-05 16:44:12 -050080 goto out;
Gordon Ross613a2f62009-07-02 12:58:38 -040081
82 error = smb_ctx_readrc(ctx);
83 if (error)
Gordon Ross7d815082013-01-05 16:44:12 -050084 goto out;
Gordon Ross613a2f62009-07-02 12:58:38 -040085
86 while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) {
87 if (opt == '?')
88 view_usage();
Gordon Ross7d815082013-01-05 16:44:12 -050089 /*
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 }
Gordon Ross613a2f62009-07-02 12:58:38 -040098 error = smb_ctx_opt(ctx, opt, optarg);
99 if (error)
Gordon Ross7d815082013-01-05 16:44:12 -0500100 goto out;
Gordon Ross613a2f62009-07-02 12:58:38 -0400101 }
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)
Gordon Ross7d815082013-01-05 16:44:12 -0500111 goto out;
Gordon Ross613a2f62009-07-02 12:58:38 -0400112
113 /*
114 * Have server, share, etc. from above:
115 * smb_ctx_scan_argv, option settings.
116 * Get the session and tree.
117 */
118again:
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);
Gordon Ross7d815082013-01-05 16:44:12 -0500128 goto out;
Gordon Ross613a2f62009-07-02 12:58:38 -0400129 }
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);
Gordon Ross7d815082013-01-05 16:44:12 -0500135 goto out;
Gordon Ross613a2f62009-07-02 12:58:38 -0400136 }
137
138 /*
139 * Have IPC$ tcon, now list shares.
Gordon Ross7d815082013-01-05 16:44:12 -0500140 * Try RPC; if that fails, do RAP.
Gordon Ross613a2f62009-07-02 12:58:38 -0400141 */
Gordon Ross7d815082013-01-05 16:44:12 -0500142 if (!use_rap)
143 error = share_enum_rpc(ctx, ctx->ct_fullserver);
144 if (error || use_rap)
145 error = share_enum_rap(ctx);
Gordon Ross613a2f62009-07-02 12:58:38 -0400146
Gordon Ross7d815082013-01-05 16:44:12 -0500147out:
Gordon Ross613a2f62009-07-02 12:58:38 -0400148 smb_ctx_free(ctx);
Gordon Rossa6d10112018-04-08 10:09:12 -0400149 return (error);
Gordon Ross613a2f62009-07-02 12:58:38 -0400150}
151
thurlow4bff34e2008-02-13 19:51:22 -0800152#ifdef I18N /* not defined, put here so xgettext(1) can find strings */
153static char *shtype[] = {
154 gettext("disk"),
155 gettext("printer"),
156 gettext("device"), /* Communications device */
Gordon Ross7d815082013-01-05 16:44:12 -0500157 gettext("IPC"), /* Inter process communication */
thurlow4bff34e2008-02-13 19:51:22 -0800158 gettext("unknown")
159};
160#else
161static char *shtype[] = {
162 "disk",
163 "printer",
164 "device", /* Communications device */
Gordon Ross7d815082013-01-05 16:44:12 -0500165 "IPC", /* IPC Inter process communication */
thurlow4bff34e2008-02-13 19:51:22 -0800166 "unknown"
167};
168#endif
169
Gordon Ross7d815082013-01-05 16:44:12 -0500170/*
171 * Print one line of the share list, or
172 * if SHARE is null, print the header line.
173 */
Gordon Ross613a2f62009-07-02 12:58:38 -0400174void
Gordon Ross7d815082013-01-05 16:44:12 -0500175view_print_share(char *share, int type, char *comment)
Gordon Ross613a2f62009-07-02 12:58:38 -0400176{
Gordon Ross7d815082013-01-05 16:44:12 -0500177 char *stname;
178 int stindex;
Gordon Ross613a2f62009-07-02 12:58:38 -0400179
Gordon Ross7d815082013-01-05 16:44:12 -0500180 if (share == NULL) {
181 printf(gettext("Share Type Comment\n"));
182 printf("-------------------------------\n");
183 return;
thurlow4bff34e2008-02-13 19:51:22 -0800184 }
Gordon Ross9c9af252008-12-04 12:51:31 -0500185
Gordon Ross7d815082013-01-05 16:44:12 -0500186 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);
thurlow4bff34e2008-02-13 19:51:22 -0800195}