xref: /titanic_51/usr/src/cmd/fs.d/smbclnt/smbutil/view.c (revision ae3d7f90695ef456a6da4f7bdccd448ebe0b99e1)
14bff34e3Sthurlow /*
24bff34e3Sthurlow  * Copyright (c) 2000, Boris Popov
34bff34e3Sthurlow  * All rights reserved.
44bff34e3Sthurlow  *
54bff34e3Sthurlow  * Redistribution and use in source and binary forms, with or without
64bff34e3Sthurlow  * modification, are permitted provided that the following conditions
74bff34e3Sthurlow  * are met:
84bff34e3Sthurlow  * 1. Redistributions of source code must retain the above copyright
94bff34e3Sthurlow  *    notice, this list of conditions and the following disclaimer.
104bff34e3Sthurlow  * 2. Redistributions in binary form must reproduce the above copyright
114bff34e3Sthurlow  *    notice, this list of conditions and the following disclaimer in the
124bff34e3Sthurlow  *    documentation and/or other materials provided with the distribution.
134bff34e3Sthurlow  * 3. All advertising materials mentioning features or use of this software
144bff34e3Sthurlow  *    must display the following acknowledgement:
154bff34e3Sthurlow  *    This product includes software developed by Boris Popov.
164bff34e3Sthurlow  * 4. Neither the name of the author nor the names of any co-contributors
174bff34e3Sthurlow  *    may be used to endorse or promote products derived from this software
184bff34e3Sthurlow  *    without specific prior written permission.
194bff34e3Sthurlow  *
204bff34e3Sthurlow  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
214bff34e3Sthurlow  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224bff34e3Sthurlow  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234bff34e3Sthurlow  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
244bff34e3Sthurlow  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254bff34e3Sthurlow  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264bff34e3Sthurlow  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274bff34e3Sthurlow  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284bff34e3Sthurlow  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294bff34e3Sthurlow  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304bff34e3Sthurlow  * SUCH DAMAGE.
314bff34e3Sthurlow  *
324bff34e3Sthurlow  * $Id: view.c,v 1.9 2004/12/13 00:25:39 lindak Exp $
334bff34e3Sthurlow  */
344bff34e3Sthurlow 
35613a2f6bSGordon Ross /*
36*ae3d7f90SGordon Ross  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
37613a2f6bSGordon Ross  */
38613a2f6bSGordon Ross 
39613a2f6bSGordon Ross #include <sys/types.h>
40613a2f6bSGordon Ross #include <errno.h>
414bff34e3Sthurlow #include <stdio.h>
424bff34e3Sthurlow #include <err.h>
434bff34e3Sthurlow #include <unistd.h>
444bff34e3Sthurlow #include <strings.h>
454bff34e3Sthurlow #include <stdlib.h>
464bff34e3Sthurlow #include <sysexits.h>
474bff34e3Sthurlow #include <libintl.h>
484bff34e3Sthurlow 
49613a2f6bSGordon Ross #include <netsmb/smb.h>
504bff34e3Sthurlow #include <netsmb/smb_lib.h>
514bff34e3Sthurlow #include <netsmb/smb_netshareenum.h>
524bff34e3Sthurlow 
534bff34e3Sthurlow #include "common.h"
544bff34e3Sthurlow 
55613a2f6bSGordon Ross int enum_shares(smb_ctx_t *);
56613a2f6bSGordon Ross void print_shares(int, int, struct share_info *);
57613a2f6bSGordon Ross 
58613a2f6bSGordon Ross void
59613a2f6bSGordon Ross view_usage(void)
60613a2f6bSGordon Ross {
61613a2f6bSGordon Ross 	printf(gettext("usage: smbutil view [connection options] //"
62613a2f6bSGordon Ross 	    "[workgroup;][user[:password]@]server\n"));
63613a2f6bSGordon Ross 	exit(1);
64613a2f6bSGordon Ross }
65613a2f6bSGordon Ross 
66613a2f6bSGordon Ross int
67613a2f6bSGordon Ross cmd_view(int argc, char *argv[])
68613a2f6bSGordon Ross {
69613a2f6bSGordon Ross 	struct smb_ctx *ctx;
70613a2f6bSGordon Ross 	int error, err2, opt;
71613a2f6bSGordon Ross 
72613a2f6bSGordon Ross 	if (argc < 2)
73613a2f6bSGordon Ross 		view_usage();
74613a2f6bSGordon Ross 
75613a2f6bSGordon Ross 	error = smb_ctx_alloc(&ctx);
76613a2f6bSGordon Ross 	if (error)
77613a2f6bSGordon Ross 		return (error);
78613a2f6bSGordon Ross 
79613a2f6bSGordon Ross 	error = smb_ctx_scan_argv(ctx, argc, argv,
80613a2f6bSGordon Ross 	    SMBL_SERVER, SMBL_SERVER, USE_WILDCARD);
81613a2f6bSGordon Ross 	if (error)
82613a2f6bSGordon Ross 		return (error);
83613a2f6bSGordon Ross 
84613a2f6bSGordon Ross 	error = smb_ctx_readrc(ctx);
85613a2f6bSGordon Ross 	if (error)
86613a2f6bSGordon Ross 		return (error);
87613a2f6bSGordon Ross 
88613a2f6bSGordon Ross 	while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) {
89613a2f6bSGordon Ross 		if (opt == '?')
90613a2f6bSGordon Ross 			view_usage();
91613a2f6bSGordon Ross 		error = smb_ctx_opt(ctx, opt, optarg);
92613a2f6bSGordon Ross 		if (error)
93613a2f6bSGordon Ross 			return (error);
94613a2f6bSGordon Ross 	}
95613a2f6bSGordon Ross 
96613a2f6bSGordon Ross 	smb_ctx_setshare(ctx, "IPC$", USE_IPC);
97613a2f6bSGordon Ross 
98613a2f6bSGordon Ross 	/*
99613a2f6bSGordon Ross 	 * Resolve the server address,
100613a2f6bSGordon Ross 	 * setup derived defaults.
101613a2f6bSGordon Ross 	 */
102613a2f6bSGordon Ross 	error = smb_ctx_resolve(ctx);
103613a2f6bSGordon Ross 	if (error)
104613a2f6bSGordon Ross 		return (error);
105613a2f6bSGordon Ross 
106613a2f6bSGordon Ross 	/*
107613a2f6bSGordon Ross 	 * Have server, share, etc. from above:
108613a2f6bSGordon Ross 	 * smb_ctx_scan_argv, option settings.
109613a2f6bSGordon Ross 	 * Get the session and tree.
110613a2f6bSGordon Ross 	 */
111613a2f6bSGordon Ross again:
112613a2f6bSGordon Ross 	error = smb_ctx_get_ssn(ctx);
113613a2f6bSGordon Ross 	if (error == EAUTH) {
114613a2f6bSGordon Ross 		err2 = smb_get_authentication(ctx);
115613a2f6bSGordon Ross 		if (err2 == 0)
116613a2f6bSGordon Ross 			goto again;
117613a2f6bSGordon Ross 	}
118613a2f6bSGordon Ross 	if (error) {
119613a2f6bSGordon Ross 		smb_error(gettext("//%s: login failed"),
120613a2f6bSGordon Ross 		    error, ctx->ct_fullserver);
121613a2f6bSGordon Ross 		return (error);
122613a2f6bSGordon Ross 	}
123613a2f6bSGordon Ross 
124613a2f6bSGordon Ross 	error = smb_ctx_get_tree(ctx);
125613a2f6bSGordon Ross 	if (error) {
126613a2f6bSGordon Ross 		smb_error(gettext("//%s/%s: tree connect failed"),
127613a2f6bSGordon Ross 		    error, ctx->ct_fullserver, ctx->ct_origshare);
128613a2f6bSGordon Ross 		return (error);
129613a2f6bSGordon Ross 	}
130613a2f6bSGordon Ross 
131613a2f6bSGordon Ross 	/*
132613a2f6bSGordon Ross 	 * Have IPC$ tcon, now list shares.
133*ae3d7f90SGordon Ross 	 * This prints its own errors.
134613a2f6bSGordon Ross 	 */
135613a2f6bSGordon Ross 	error = enum_shares(ctx);
136*ae3d7f90SGordon Ross 	if (error)
137613a2f6bSGordon Ross 		return (error);
138613a2f6bSGordon Ross 
139613a2f6bSGordon Ross 	smb_ctx_free(ctx);
140613a2f6bSGordon Ross 	return (0);
141613a2f6bSGordon Ross }
142613a2f6bSGordon Ross 
1434bff34e3Sthurlow #ifdef I18N	/* not defined, put here so xgettext(1) can find strings */
1444bff34e3Sthurlow static char *shtype[] = {
1454bff34e3Sthurlow 	gettext("disk"),
1464bff34e3Sthurlow 	gettext("printer"),
1474bff34e3Sthurlow 	gettext("device"),	/* Communications device */
1484bff34e3Sthurlow 	gettext("IPC"), 	/* Inter process communication */
1494bff34e3Sthurlow 	gettext("unknown")
1504bff34e3Sthurlow };
1514bff34e3Sthurlow #else
1524bff34e3Sthurlow static char *shtype[] = {
1534bff34e3Sthurlow 	"disk",
1544bff34e3Sthurlow 	"printer",
1554bff34e3Sthurlow 	"device",		/* Communications device */
1564bff34e3Sthurlow 	"IPC",  		/* IPC Inter process communication */
1574bff34e3Sthurlow 	"unknown"
1584bff34e3Sthurlow };
1594bff34e3Sthurlow #endif
1604bff34e3Sthurlow 
1614bff34e3Sthurlow int
162613a2f6bSGordon Ross enum_shares(smb_ctx_t *ctx)
1634bff34e3Sthurlow {
164613a2f6bSGordon Ross 	struct share_info *share_info;
165613a2f6bSGordon Ross 	int error, entries, total;
1664bff34e3Sthurlow 
167613a2f6bSGordon Ross 	/*
168613a2f6bSGordon Ross 	 * XXX: Later, try RPC first,
169613a2f6bSGordon Ross 	 * then fall back to RAP...
170613a2f6bSGordon Ross 	 */
1714bff34e3Sthurlow 	error = smb_netshareenum(ctx, &entries, &total, &share_info);
1724bff34e3Sthurlow 	if (error) {
173*ae3d7f90SGordon Ross 		smb_error(gettext("//%s failed to list shares"),
174*ae3d7f90SGordon Ross 		    error, ctx->ct_fullserver);
175613a2f6bSGordon Ross 		return (error);
1764bff34e3Sthurlow 	}
177613a2f6bSGordon Ross 	print_shares(entries, total, share_info);
178613a2f6bSGordon Ross 	return (0);
179613a2f6bSGordon Ross }
180613a2f6bSGordon Ross 
181613a2f6bSGordon Ross void
182613a2f6bSGordon Ross print_shares(int entries, int total,
183613a2f6bSGordon Ross 	struct share_info *share_info)
184613a2f6bSGordon Ross {
185613a2f6bSGordon Ross 	struct share_info *ep;
186613a2f6bSGordon Ross 	int i;
187613a2f6bSGordon Ross 
188613a2f6bSGordon Ross 	printf(gettext("Share        Type       Comment\n"));
189613a2f6bSGordon Ross 	printf("-------------------------------\n");
190613a2f6bSGordon Ross 
1914bff34e3Sthurlow 	for (ep = share_info, i = 0; i < entries; i++, ep++) {
1924bff34e3Sthurlow 		int sti = ep->type & STYPE_MASK;
1934bff34e3Sthurlow 		if (sti > STYPE_UNKNOWN)
1944bff34e3Sthurlow 			sti = STYPE_UNKNOWN;
1954bff34e3Sthurlow 		printf("%-12s %-10s %s\n", ep->netname,
1964bff34e3Sthurlow 		    gettext(shtype[sti]),
1974bff34e3Sthurlow 		    ep->remark ? ep->remark : "");
1984bff34e3Sthurlow 		free(ep->netname);
1999c9af259SGordon Ross 		free(ep->remark);
2004bff34e3Sthurlow 	}
2014bff34e3Sthurlow 	printf(gettext("\n%d shares listed from %d available\n"),
2024bff34e3Sthurlow 	    entries, total);
2039c9af259SGordon Ross 
2044bff34e3Sthurlow 	free(share_info);
2054bff34e3Sthurlow }
206