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 /*
36ae3d7f90SGordon Ross  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
37*40c0e231SGordon Ross  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
38613a2f6bSGordon Ross  */
39613a2f6bSGordon Ross 
40613a2f6bSGordon Ross #include <sys/types.h>
41613a2f6bSGordon Ross #include <errno.h>
424bff34e3Sthurlow #include <stdio.h>
434bff34e3Sthurlow #include <err.h>
444bff34e3Sthurlow #include <unistd.h>
454bff34e3Sthurlow #include <strings.h>
464bff34e3Sthurlow #include <stdlib.h>
474bff34e3Sthurlow #include <sysexits.h>
484bff34e3Sthurlow #include <libintl.h>
494bff34e3Sthurlow 
50613a2f6bSGordon Ross #include <netsmb/smb.h>
514bff34e3Sthurlow #include <netsmb/smb_lib.h>
524bff34e3Sthurlow #include "common.h"
534bff34e3Sthurlow 
54613a2f6bSGordon Ross void
view_usage(void)55613a2f6bSGordon Ross view_usage(void)
56613a2f6bSGordon Ross {
57613a2f6bSGordon Ross 	printf(gettext("usage: smbutil view [connection options] //"
58613a2f6bSGordon Ross 	    "[workgroup;][user[:password]@]server\n"));
59613a2f6bSGordon Ross 	exit(1);
60613a2f6bSGordon Ross }
61613a2f6bSGordon Ross 
62613a2f6bSGordon Ross int
cmd_view(int argc,char * argv[])63613a2f6bSGordon Ross cmd_view(int argc, char *argv[])
64613a2f6bSGordon Ross {
65613a2f6bSGordon Ross 	struct smb_ctx *ctx;
66613a2f6bSGordon Ross 	int error, err2, opt;
67613a2f6bSGordon Ross 
68613a2f6bSGordon Ross 	if (argc < 2)
69613a2f6bSGordon Ross 		view_usage();
70613a2f6bSGordon Ross 
71613a2f6bSGordon Ross 	error = smb_ctx_alloc(&ctx);
72613a2f6bSGordon Ross 	if (error)
73613a2f6bSGordon Ross 		return (error);
74613a2f6bSGordon Ross 
75613a2f6bSGordon Ross 	error = smb_ctx_scan_argv(ctx, argc, argv,
76613a2f6bSGordon Ross 	    SMBL_SERVER, SMBL_SERVER, USE_WILDCARD);
77613a2f6bSGordon Ross 	if (error)
787d815089SGordon Ross 		goto out;
79613a2f6bSGordon Ross 
80613a2f6bSGordon Ross 	error = smb_ctx_readrc(ctx);
81613a2f6bSGordon Ross 	if (error)
827d815089SGordon Ross 		goto out;
83613a2f6bSGordon Ross 
84613a2f6bSGordon Ross 	while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) {
85613a2f6bSGordon Ross 		if (opt == '?')
86613a2f6bSGordon Ross 			view_usage();
87613a2f6bSGordon Ross 		error = smb_ctx_opt(ctx, opt, optarg);
88613a2f6bSGordon Ross 		if (error)
897d815089SGordon Ross 			goto out;
90613a2f6bSGordon Ross 	}
91613a2f6bSGordon Ross 
92613a2f6bSGordon Ross 	smb_ctx_setshare(ctx, "IPC$", USE_IPC);
93613a2f6bSGordon Ross 
94613a2f6bSGordon Ross 	/*
95613a2f6bSGordon Ross 	 * Resolve the server address,
96613a2f6bSGordon Ross 	 * setup derived defaults.
97613a2f6bSGordon Ross 	 */
98613a2f6bSGordon Ross 	error = smb_ctx_resolve(ctx);
99613a2f6bSGordon Ross 	if (error)
1007d815089SGordon Ross 		goto out;
101613a2f6bSGordon Ross 
102613a2f6bSGordon Ross 	/*
103613a2f6bSGordon Ross 	 * Have server, share, etc. from above:
104613a2f6bSGordon Ross 	 * smb_ctx_scan_argv, option settings.
105613a2f6bSGordon Ross 	 * Get the session and tree.
106613a2f6bSGordon Ross 	 */
107613a2f6bSGordon Ross again:
108613a2f6bSGordon Ross 	error = smb_ctx_get_ssn(ctx);
109613a2f6bSGordon Ross 	if (error == EAUTH) {
110613a2f6bSGordon Ross 		err2 = smb_get_authentication(ctx);
111613a2f6bSGordon Ross 		if (err2 == 0)
112613a2f6bSGordon Ross 			goto again;
113613a2f6bSGordon Ross 	}
114613a2f6bSGordon Ross 	if (error) {
115613a2f6bSGordon Ross 		smb_error(gettext("//%s: login failed"),
116613a2f6bSGordon Ross 		    error, ctx->ct_fullserver);
1177d815089SGordon Ross 		goto out;
118613a2f6bSGordon Ross 	}
119613a2f6bSGordon Ross 
120613a2f6bSGordon Ross 	error = smb_ctx_get_tree(ctx);
121613a2f6bSGordon Ross 	if (error) {
122613a2f6bSGordon Ross 		smb_error(gettext("//%s/%s: tree connect failed"),
123613a2f6bSGordon Ross 		    error, ctx->ct_fullserver, ctx->ct_origshare);
1247d815089SGordon Ross 		goto out;
125613a2f6bSGordon Ross 	}
126613a2f6bSGordon Ross 
127613a2f6bSGordon Ross 	/*
128613a2f6bSGordon Ross 	 * Have IPC$ tcon, now list shares.
129613a2f6bSGordon Ross 	 */
130*40c0e231SGordon Ross 	error = share_enum_rpc(ctx, ctx->ct_fullserver);
131613a2f6bSGordon Ross 
1327d815089SGordon Ross out:
133613a2f6bSGordon Ross 	smb_ctx_free(ctx);
134a6d10110SGordon Ross 	return (error);
135613a2f6bSGordon Ross }
136613a2f6bSGordon Ross 
1374bff34e3Sthurlow #ifdef I18N	/* not defined, put here so xgettext(1) can find strings */
1384bff34e3Sthurlow static char *shtype[] = {
1394bff34e3Sthurlow 	gettext("disk"),
1404bff34e3Sthurlow 	gettext("printer"),
1414bff34e3Sthurlow 	gettext("device"),	/* Communications device */
1427d815089SGordon Ross 	gettext("IPC"),		/* Inter process communication */
1434bff34e3Sthurlow 	gettext("unknown")
1444bff34e3Sthurlow };
1454bff34e3Sthurlow #else
1464bff34e3Sthurlow static char *shtype[] = {
1474bff34e3Sthurlow 	"disk",
1484bff34e3Sthurlow 	"printer",
1494bff34e3Sthurlow 	"device",		/* Communications device */
1507d815089SGordon Ross 	"IPC",			/* IPC Inter process communication */
1514bff34e3Sthurlow 	"unknown"
1524bff34e3Sthurlow };
1534bff34e3Sthurlow #endif
1544bff34e3Sthurlow 
1557d815089SGordon Ross /*
1567d815089SGordon Ross  * Print one line of the share list, or
1577d815089SGordon Ross  * if SHARE is null, print the header line.
1587d815089SGordon Ross  */
1597d815089SGordon Ross void
view_print_share(char * share,int type,char * comment)1607d815089SGordon Ross view_print_share(char *share, int type, char *comment)
1614bff34e3Sthurlow {
1627d815089SGordon Ross 	char *stname;
1637d815089SGordon Ross 	int stindex;
1644bff34e3Sthurlow 
1657d815089SGordon Ross 	if (share == NULL) {
1667d815089SGordon Ross 		printf(gettext("Share        Type       Comment\n"));
1677d815089SGordon Ross 		printf("-------------------------------\n");
1687d815089SGordon Ross 		return;
1694bff34e3Sthurlow 	}
170613a2f6bSGordon Ross 
1717d815089SGordon Ross 	stindex = type & STYPE_MASK;
1727d815089SGordon Ross 	if (stindex > STYPE_UNKNOWN)
1737d815089SGordon Ross 		stindex = STYPE_UNKNOWN;
1747d815089SGordon Ross 	stname = gettext(shtype[stindex]);
1757d815089SGordon Ross 
1767d815089SGordon Ross 	if (comment == NULL)
1777d815089SGordon Ross 		comment = "";
1789c9af259SGordon Ross 
1797d815089SGordon Ross 	printf("%-12s %-10s %s\n", share, stname, comment);
1804bff34e3Sthurlow }
181