16007Sthurlow /*
26007Sthurlow * Copyright (c) 2000, Boris Popov
36007Sthurlow * All rights reserved.
46007Sthurlow *
56007Sthurlow * Redistribution and use in source and binary forms, with or without
66007Sthurlow * modification, are permitted provided that the following conditions
76007Sthurlow * are met:
86007Sthurlow * 1. Redistributions of source code must retain the above copyright
96007Sthurlow * notice, this list of conditions and the following disclaimer.
106007Sthurlow * 2. Redistributions in binary form must reproduce the above copyright
116007Sthurlow * notice, this list of conditions and the following disclaimer in the
126007Sthurlow * documentation and/or other materials provided with the distribution.
136007Sthurlow * 3. All advertising materials mentioning features or use of this software
146007Sthurlow * must display the following acknowledgement:
156007Sthurlow * This product includes software developed by Boris Popov.
166007Sthurlow * 4. Neither the name of the author nor the names of any co-contributors
176007Sthurlow * may be used to endorse or promote products derived from this software
186007Sthurlow * without specific prior written permission.
196007Sthurlow *
206007Sthurlow * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
216007Sthurlow * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
226007Sthurlow * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
236007Sthurlow * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
246007Sthurlow * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
256007Sthurlow * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
266007Sthurlow * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
276007Sthurlow * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
286007Sthurlow * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
296007Sthurlow * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
306007Sthurlow * SUCH DAMAGE.
316007Sthurlow *
326007Sthurlow * $Id: view.c,v 1.9 2004/12/13 00:25:39 lindak Exp $
336007Sthurlow */
346007Sthurlow
3510023SGordon.Ross@Sun.COM /*
36*12925SGordon.Ross@Sun.COM * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
3710023SGordon.Ross@Sun.COM */
3810023SGordon.Ross@Sun.COM
3910023SGordon.Ross@Sun.COM #include <sys/types.h>
4010023SGordon.Ross@Sun.COM #include <errno.h>
416007Sthurlow #include <stdio.h>
426007Sthurlow #include <err.h>
436007Sthurlow #include <unistd.h>
446007Sthurlow #include <strings.h>
456007Sthurlow #include <stdlib.h>
466007Sthurlow #include <sysexits.h>
476007Sthurlow #include <libintl.h>
486007Sthurlow
4910023SGordon.Ross@Sun.COM #include <netsmb/smb.h>
506007Sthurlow #include <netsmb/smb_lib.h>
516007Sthurlow #include <netsmb/smb_netshareenum.h>
526007Sthurlow
536007Sthurlow #include "common.h"
546007Sthurlow
5510023SGordon.Ross@Sun.COM int enum_shares(smb_ctx_t *);
5610023SGordon.Ross@Sun.COM void print_shares(int, int, struct share_info *);
5710023SGordon.Ross@Sun.COM
5810023SGordon.Ross@Sun.COM void
view_usage(void)5910023SGordon.Ross@Sun.COM view_usage(void)
6010023SGordon.Ross@Sun.COM {
6110023SGordon.Ross@Sun.COM printf(gettext("usage: smbutil view [connection options] //"
6210023SGordon.Ross@Sun.COM "[workgroup;][user[:password]@]server\n"));
6310023SGordon.Ross@Sun.COM exit(1);
6410023SGordon.Ross@Sun.COM }
6510023SGordon.Ross@Sun.COM
6610023SGordon.Ross@Sun.COM int
cmd_view(int argc,char * argv[])6710023SGordon.Ross@Sun.COM cmd_view(int argc, char *argv[])
6810023SGordon.Ross@Sun.COM {
6910023SGordon.Ross@Sun.COM struct smb_ctx *ctx;
7010023SGordon.Ross@Sun.COM int error, err2, opt;
7110023SGordon.Ross@Sun.COM
7210023SGordon.Ross@Sun.COM if (argc < 2)
7310023SGordon.Ross@Sun.COM view_usage();
7410023SGordon.Ross@Sun.COM
7510023SGordon.Ross@Sun.COM error = smb_ctx_alloc(&ctx);
7610023SGordon.Ross@Sun.COM if (error)
7710023SGordon.Ross@Sun.COM return (error);
7810023SGordon.Ross@Sun.COM
7910023SGordon.Ross@Sun.COM error = smb_ctx_scan_argv(ctx, argc, argv,
8010023SGordon.Ross@Sun.COM SMBL_SERVER, SMBL_SERVER, USE_WILDCARD);
8110023SGordon.Ross@Sun.COM if (error)
8210023SGordon.Ross@Sun.COM return (error);
8310023SGordon.Ross@Sun.COM
8410023SGordon.Ross@Sun.COM error = smb_ctx_readrc(ctx);
8510023SGordon.Ross@Sun.COM if (error)
8610023SGordon.Ross@Sun.COM return (error);
8710023SGordon.Ross@Sun.COM
8810023SGordon.Ross@Sun.COM while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) {
8910023SGordon.Ross@Sun.COM if (opt == '?')
9010023SGordon.Ross@Sun.COM view_usage();
9110023SGordon.Ross@Sun.COM error = smb_ctx_opt(ctx, opt, optarg);
9210023SGordon.Ross@Sun.COM if (error)
9310023SGordon.Ross@Sun.COM return (error);
9410023SGordon.Ross@Sun.COM }
9510023SGordon.Ross@Sun.COM
9610023SGordon.Ross@Sun.COM smb_ctx_setshare(ctx, "IPC$", USE_IPC);
9710023SGordon.Ross@Sun.COM
9810023SGordon.Ross@Sun.COM /*
9910023SGordon.Ross@Sun.COM * Resolve the server address,
10010023SGordon.Ross@Sun.COM * setup derived defaults.
10110023SGordon.Ross@Sun.COM */
10210023SGordon.Ross@Sun.COM error = smb_ctx_resolve(ctx);
10310023SGordon.Ross@Sun.COM if (error)
10410023SGordon.Ross@Sun.COM return (error);
10510023SGordon.Ross@Sun.COM
10610023SGordon.Ross@Sun.COM /*
10710023SGordon.Ross@Sun.COM * Have server, share, etc. from above:
10810023SGordon.Ross@Sun.COM * smb_ctx_scan_argv, option settings.
10910023SGordon.Ross@Sun.COM * Get the session and tree.
11010023SGordon.Ross@Sun.COM */
11110023SGordon.Ross@Sun.COM again:
11210023SGordon.Ross@Sun.COM error = smb_ctx_get_ssn(ctx);
11310023SGordon.Ross@Sun.COM if (error == EAUTH) {
11410023SGordon.Ross@Sun.COM err2 = smb_get_authentication(ctx);
11510023SGordon.Ross@Sun.COM if (err2 == 0)
11610023SGordon.Ross@Sun.COM goto again;
11710023SGordon.Ross@Sun.COM }
11810023SGordon.Ross@Sun.COM if (error) {
11910023SGordon.Ross@Sun.COM smb_error(gettext("//%s: login failed"),
12010023SGordon.Ross@Sun.COM error, ctx->ct_fullserver);
12110023SGordon.Ross@Sun.COM return (error);
12210023SGordon.Ross@Sun.COM }
12310023SGordon.Ross@Sun.COM
12410023SGordon.Ross@Sun.COM error = smb_ctx_get_tree(ctx);
12510023SGordon.Ross@Sun.COM if (error) {
12610023SGordon.Ross@Sun.COM smb_error(gettext("//%s/%s: tree connect failed"),
12710023SGordon.Ross@Sun.COM error, ctx->ct_fullserver, ctx->ct_origshare);
12810023SGordon.Ross@Sun.COM return (error);
12910023SGordon.Ross@Sun.COM }
13010023SGordon.Ross@Sun.COM
13110023SGordon.Ross@Sun.COM /*
13210023SGordon.Ross@Sun.COM * Have IPC$ tcon, now list shares.
133*12925SGordon.Ross@Sun.COM * This prints its own errors.
13410023SGordon.Ross@Sun.COM */
13510023SGordon.Ross@Sun.COM error = enum_shares(ctx);
136*12925SGordon.Ross@Sun.COM if (error)
13710023SGordon.Ross@Sun.COM return (error);
13810023SGordon.Ross@Sun.COM
13910023SGordon.Ross@Sun.COM smb_ctx_free(ctx);
14010023SGordon.Ross@Sun.COM return (0);
14110023SGordon.Ross@Sun.COM }
14210023SGordon.Ross@Sun.COM
1436007Sthurlow #ifdef I18N /* not defined, put here so xgettext(1) can find strings */
1446007Sthurlow static char *shtype[] = {
1456007Sthurlow gettext("disk"),
1466007Sthurlow gettext("printer"),
1476007Sthurlow gettext("device"), /* Communications device */
1486007Sthurlow gettext("IPC"), /* Inter process communication */
1496007Sthurlow gettext("unknown")
1506007Sthurlow };
1516007Sthurlow #else
1526007Sthurlow static char *shtype[] = {
1536007Sthurlow "disk",
1546007Sthurlow "printer",
1556007Sthurlow "device", /* Communications device */
1566007Sthurlow "IPC", /* IPC Inter process communication */
1576007Sthurlow "unknown"
1586007Sthurlow };
1596007Sthurlow #endif
1606007Sthurlow
1616007Sthurlow int
enum_shares(smb_ctx_t * ctx)16210023SGordon.Ross@Sun.COM enum_shares(smb_ctx_t *ctx)
1636007Sthurlow {
16410023SGordon.Ross@Sun.COM struct share_info *share_info;
16510023SGordon.Ross@Sun.COM int error, entries, total;
1666007Sthurlow
16710023SGordon.Ross@Sun.COM /*
16810023SGordon.Ross@Sun.COM * XXX: Later, try RPC first,
16910023SGordon.Ross@Sun.COM * then fall back to RAP...
17010023SGordon.Ross@Sun.COM */
1716007Sthurlow error = smb_netshareenum(ctx, &entries, &total, &share_info);
1726007Sthurlow if (error) {
173*12925SGordon.Ross@Sun.COM smb_error(gettext("//%s failed to list shares"),
174*12925SGordon.Ross@Sun.COM error, ctx->ct_fullserver);
17510023SGordon.Ross@Sun.COM return (error);
1766007Sthurlow }
17710023SGordon.Ross@Sun.COM print_shares(entries, total, share_info);
17810023SGordon.Ross@Sun.COM return (0);
17910023SGordon.Ross@Sun.COM }
18010023SGordon.Ross@Sun.COM
18110023SGordon.Ross@Sun.COM void
print_shares(int entries,int total,struct share_info * share_info)18210023SGordon.Ross@Sun.COM print_shares(int entries, int total,
18310023SGordon.Ross@Sun.COM struct share_info *share_info)
18410023SGordon.Ross@Sun.COM {
18510023SGordon.Ross@Sun.COM struct share_info *ep;
18610023SGordon.Ross@Sun.COM int i;
18710023SGordon.Ross@Sun.COM
18810023SGordon.Ross@Sun.COM printf(gettext("Share Type Comment\n"));
18910023SGordon.Ross@Sun.COM printf("-------------------------------\n");
19010023SGordon.Ross@Sun.COM
1916007Sthurlow for (ep = share_info, i = 0; i < entries; i++, ep++) {
1926007Sthurlow int sti = ep->type & STYPE_MASK;
1936007Sthurlow if (sti > STYPE_UNKNOWN)
1946007Sthurlow sti = STYPE_UNKNOWN;
1956007Sthurlow printf("%-12s %-10s %s\n", ep->netname,
1966007Sthurlow gettext(shtype[sti]),
1976007Sthurlow ep->remark ? ep->remark : "");
1986007Sthurlow free(ep->netname);
1998271SGordon.Ross@Sun.COM free(ep->remark);
2006007Sthurlow }
2016007Sthurlow printf(gettext("\n%d shares listed from %d available\n"),
2026007Sthurlow entries, total);
2038271SGordon.Ross@Sun.COM
2046007Sthurlow free(share_info);
2056007Sthurlow }
206