xref: /onnv-gate/usr/src/cmd/fs.d/smbclnt/smbutil/view.c (revision 10023:71bf38dba3d6)
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 
35*10023SGordon.Ross@Sun.COM /*
36*10023SGordon.Ross@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37*10023SGordon.Ross@Sun.COM  * Use is subject to license terms.
38*10023SGordon.Ross@Sun.COM  */
39*10023SGordon.Ross@Sun.COM 
40*10023SGordon.Ross@Sun.COM #include <sys/types.h>
41*10023SGordon.Ross@Sun.COM #include <errno.h>
426007Sthurlow #include <stdio.h>
436007Sthurlow #include <err.h>
446007Sthurlow #include <unistd.h>
456007Sthurlow #include <strings.h>
466007Sthurlow #include <stdlib.h>
476007Sthurlow #include <sysexits.h>
486007Sthurlow #include <libintl.h>
496007Sthurlow 
50*10023SGordon.Ross@Sun.COM #include <netsmb/smb.h>
516007Sthurlow #include <netsmb/smb_lib.h>
526007Sthurlow #include <netsmb/smb_netshareenum.h>
536007Sthurlow 
546007Sthurlow #include "common.h"
556007Sthurlow 
56*10023SGordon.Ross@Sun.COM int enum_shares(smb_ctx_t *);
57*10023SGordon.Ross@Sun.COM void print_shares(int, int, struct share_info *);
58*10023SGordon.Ross@Sun.COM 
59*10023SGordon.Ross@Sun.COM void
60*10023SGordon.Ross@Sun.COM view_usage(void)
61*10023SGordon.Ross@Sun.COM {
62*10023SGordon.Ross@Sun.COM 	printf(gettext("usage: smbutil view [connection options] //"
63*10023SGordon.Ross@Sun.COM 	    "[workgroup;][user[:password]@]server\n"));
64*10023SGordon.Ross@Sun.COM 	exit(1);
65*10023SGordon.Ross@Sun.COM }
66*10023SGordon.Ross@Sun.COM 
67*10023SGordon.Ross@Sun.COM int
68*10023SGordon.Ross@Sun.COM cmd_view(int argc, char *argv[])
69*10023SGordon.Ross@Sun.COM {
70*10023SGordon.Ross@Sun.COM 	struct smb_ctx *ctx;
71*10023SGordon.Ross@Sun.COM 	int error, err2, opt;
72*10023SGordon.Ross@Sun.COM 
73*10023SGordon.Ross@Sun.COM 	if (argc < 2)
74*10023SGordon.Ross@Sun.COM 		view_usage();
75*10023SGordon.Ross@Sun.COM 
76*10023SGordon.Ross@Sun.COM 	error = smb_ctx_alloc(&ctx);
77*10023SGordon.Ross@Sun.COM 	if (error)
78*10023SGordon.Ross@Sun.COM 		return (error);
79*10023SGordon.Ross@Sun.COM 
80*10023SGordon.Ross@Sun.COM 	error = smb_ctx_scan_argv(ctx, argc, argv,
81*10023SGordon.Ross@Sun.COM 	    SMBL_SERVER, SMBL_SERVER, USE_WILDCARD);
82*10023SGordon.Ross@Sun.COM 	if (error)
83*10023SGordon.Ross@Sun.COM 		return (error);
84*10023SGordon.Ross@Sun.COM 
85*10023SGordon.Ross@Sun.COM 	error = smb_ctx_readrc(ctx);
86*10023SGordon.Ross@Sun.COM 	if (error)
87*10023SGordon.Ross@Sun.COM 		return (error);
88*10023SGordon.Ross@Sun.COM 
89*10023SGordon.Ross@Sun.COM 	while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) {
90*10023SGordon.Ross@Sun.COM 		if (opt == '?')
91*10023SGordon.Ross@Sun.COM 			view_usage();
92*10023SGordon.Ross@Sun.COM 		error = smb_ctx_opt(ctx, opt, optarg);
93*10023SGordon.Ross@Sun.COM 		if (error)
94*10023SGordon.Ross@Sun.COM 			return (error);
95*10023SGordon.Ross@Sun.COM 	}
96*10023SGordon.Ross@Sun.COM 
97*10023SGordon.Ross@Sun.COM 	smb_ctx_setshare(ctx, "IPC$", USE_IPC);
98*10023SGordon.Ross@Sun.COM 
99*10023SGordon.Ross@Sun.COM 	/*
100*10023SGordon.Ross@Sun.COM 	 * Resolve the server address,
101*10023SGordon.Ross@Sun.COM 	 * setup derived defaults.
102*10023SGordon.Ross@Sun.COM 	 */
103*10023SGordon.Ross@Sun.COM 	error = smb_ctx_resolve(ctx);
104*10023SGordon.Ross@Sun.COM 	if (error)
105*10023SGordon.Ross@Sun.COM 		return (error);
106*10023SGordon.Ross@Sun.COM 
107*10023SGordon.Ross@Sun.COM 	/*
108*10023SGordon.Ross@Sun.COM 	 * Have server, share, etc. from above:
109*10023SGordon.Ross@Sun.COM 	 * smb_ctx_scan_argv, option settings.
110*10023SGordon.Ross@Sun.COM 	 * Get the session and tree.
111*10023SGordon.Ross@Sun.COM 	 */
112*10023SGordon.Ross@Sun.COM again:
113*10023SGordon.Ross@Sun.COM 	error = smb_ctx_get_ssn(ctx);
114*10023SGordon.Ross@Sun.COM 	if (error == EAUTH) {
115*10023SGordon.Ross@Sun.COM 		err2 = smb_get_authentication(ctx);
116*10023SGordon.Ross@Sun.COM 		if (err2 == 0)
117*10023SGordon.Ross@Sun.COM 			goto again;
118*10023SGordon.Ross@Sun.COM 	}
119*10023SGordon.Ross@Sun.COM 	if (error) {
120*10023SGordon.Ross@Sun.COM 		smb_error(gettext("//%s: login failed"),
121*10023SGordon.Ross@Sun.COM 		    error, ctx->ct_fullserver);
122*10023SGordon.Ross@Sun.COM 		return (error);
123*10023SGordon.Ross@Sun.COM 	}
124*10023SGordon.Ross@Sun.COM 
125*10023SGordon.Ross@Sun.COM 	error = smb_ctx_get_tree(ctx);
126*10023SGordon.Ross@Sun.COM 	if (error) {
127*10023SGordon.Ross@Sun.COM 		smb_error(gettext("//%s/%s: tree connect failed"),
128*10023SGordon.Ross@Sun.COM 		    error, ctx->ct_fullserver, ctx->ct_origshare);
129*10023SGordon.Ross@Sun.COM 		return (error);
130*10023SGordon.Ross@Sun.COM 	}
131*10023SGordon.Ross@Sun.COM 
132*10023SGordon.Ross@Sun.COM 	/*
133*10023SGordon.Ross@Sun.COM 	 * Have IPC$ tcon, now list shares.
134*10023SGordon.Ross@Sun.COM 	 */
135*10023SGordon.Ross@Sun.COM 	error = enum_shares(ctx);
136*10023SGordon.Ross@Sun.COM 	if (error) {
137*10023SGordon.Ross@Sun.COM 		smb_error("cannot list shares", error);
138*10023SGordon.Ross@Sun.COM 		return (error);
139*10023SGordon.Ross@Sun.COM 	}
140*10023SGordon.Ross@Sun.COM 
141*10023SGordon.Ross@Sun.COM 	smb_ctx_free(ctx);
142*10023SGordon.Ross@Sun.COM 	return (0);
143*10023SGordon.Ross@Sun.COM }
144*10023SGordon.Ross@Sun.COM 
1456007Sthurlow #ifdef I18N	/* not defined, put here so xgettext(1) can find strings */
1466007Sthurlow static char *shtype[] = {
1476007Sthurlow 	gettext("disk"),
1486007Sthurlow 	gettext("printer"),
1496007Sthurlow 	gettext("device"),	/* Communications device */
1506007Sthurlow 	gettext("IPC"), 	/* Inter process communication */
1516007Sthurlow 	gettext("unknown")
1526007Sthurlow };
1536007Sthurlow #else
1546007Sthurlow static char *shtype[] = {
1556007Sthurlow 	"disk",
1566007Sthurlow 	"printer",
1576007Sthurlow 	"device",		/* Communications device */
1586007Sthurlow 	"IPC",  		/* IPC Inter process communication */
1596007Sthurlow 	"unknown"
1606007Sthurlow };
1616007Sthurlow #endif
1626007Sthurlow 
1636007Sthurlow int
164*10023SGordon.Ross@Sun.COM enum_shares(smb_ctx_t *ctx)
1656007Sthurlow {
166*10023SGordon.Ross@Sun.COM 	struct share_info *share_info;
167*10023SGordon.Ross@Sun.COM 	int error, entries, total;
1686007Sthurlow 
169*10023SGordon.Ross@Sun.COM 	/*
170*10023SGordon.Ross@Sun.COM 	 * XXX: Later, try RPC first,
171*10023SGordon.Ross@Sun.COM 	 * then fall back to RAP...
172*10023SGordon.Ross@Sun.COM 	 */
1736007Sthurlow 	error = smb_netshareenum(ctx, &entries, &total, &share_info);
1746007Sthurlow 	if (error) {
1756007Sthurlow 		smb_error(gettext("unable to list resources"), error);
176*10023SGordon.Ross@Sun.COM 		return (error);
1776007Sthurlow 	}
178*10023SGordon.Ross@Sun.COM 	print_shares(entries, total, share_info);
179*10023SGordon.Ross@Sun.COM 	return (0);
180*10023SGordon.Ross@Sun.COM }
181*10023SGordon.Ross@Sun.COM 
182*10023SGordon.Ross@Sun.COM void
183*10023SGordon.Ross@Sun.COM print_shares(int entries, int total,
184*10023SGordon.Ross@Sun.COM 	struct share_info *share_info)
185*10023SGordon.Ross@Sun.COM {
186*10023SGordon.Ross@Sun.COM 	struct share_info *ep;
187*10023SGordon.Ross@Sun.COM 	int i;
188*10023SGordon.Ross@Sun.COM 
189*10023SGordon.Ross@Sun.COM 	printf(gettext("Share        Type       Comment\n"));
190*10023SGordon.Ross@Sun.COM 	printf("-------------------------------\n");
191*10023SGordon.Ross@Sun.COM 
1926007Sthurlow 	for (ep = share_info, i = 0; i < entries; i++, ep++) {
1936007Sthurlow 		int sti = ep->type & STYPE_MASK;
1946007Sthurlow 		if (sti > STYPE_UNKNOWN)
1956007Sthurlow 			sti = STYPE_UNKNOWN;
1966007Sthurlow 		printf("%-12s %-10s %s\n", ep->netname,
1976007Sthurlow 		    gettext(shtype[sti]),
1986007Sthurlow 		    ep->remark ? ep->remark : "");
1996007Sthurlow 		free(ep->netname);
2008271SGordon.Ross@Sun.COM 		free(ep->remark);
2016007Sthurlow 	}
2026007Sthurlow 	printf(gettext("\n%d shares listed from %d available\n"),
2036007Sthurlow 	    entries, total);
2048271SGordon.Ross@Sun.COM 
2056007Sthurlow 	free(share_info);
2066007Sthurlow }
207