xref: /onnv-gate/usr/src/cmd/fs.d/smbclnt/smbutil/view.c (revision 6007:d57e38e8fdd1)
1*6007Sthurlow /*
2*6007Sthurlow  * Copyright (c) 2000, Boris Popov
3*6007Sthurlow  * All rights reserved.
4*6007Sthurlow  *
5*6007Sthurlow  * Redistribution and use in source and binary forms, with or without
6*6007Sthurlow  * modification, are permitted provided that the following conditions
7*6007Sthurlow  * are met:
8*6007Sthurlow  * 1. Redistributions of source code must retain the above copyright
9*6007Sthurlow  *    notice, this list of conditions and the following disclaimer.
10*6007Sthurlow  * 2. Redistributions in binary form must reproduce the above copyright
11*6007Sthurlow  *    notice, this list of conditions and the following disclaimer in the
12*6007Sthurlow  *    documentation and/or other materials provided with the distribution.
13*6007Sthurlow  * 3. All advertising materials mentioning features or use of this software
14*6007Sthurlow  *    must display the following acknowledgement:
15*6007Sthurlow  *    This product includes software developed by Boris Popov.
16*6007Sthurlow  * 4. Neither the name of the author nor the names of any co-contributors
17*6007Sthurlow  *    may be used to endorse or promote products derived from this software
18*6007Sthurlow  *    without specific prior written permission.
19*6007Sthurlow  *
20*6007Sthurlow  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21*6007Sthurlow  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*6007Sthurlow  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*6007Sthurlow  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24*6007Sthurlow  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*6007Sthurlow  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*6007Sthurlow  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*6007Sthurlow  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*6007Sthurlow  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*6007Sthurlow  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*6007Sthurlow  * SUCH DAMAGE.
31*6007Sthurlow  *
32*6007Sthurlow  * $Id: view.c,v 1.9 2004/12/13 00:25:39 lindak Exp $
33*6007Sthurlow  */
34*6007Sthurlow 
35*6007Sthurlow #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*6007Sthurlow 
37*6007Sthurlow #include <sys/param.h>
38*6007Sthurlow #include <sys/errno.h>
39*6007Sthurlow #include <sys/stat.h>
40*6007Sthurlow #include <stdio.h>
41*6007Sthurlow #include <err.h>
42*6007Sthurlow #include <unistd.h>
43*6007Sthurlow #include <strings.h>
44*6007Sthurlow #include <stdlib.h>
45*6007Sthurlow #include <sysexits.h>
46*6007Sthurlow #include <libintl.h>
47*6007Sthurlow 
48*6007Sthurlow #include <cflib.h>
49*6007Sthurlow #include <netsmb/smb_lib.h>
50*6007Sthurlow #include <netsmb/smb_netshareenum.h>
51*6007Sthurlow 
52*6007Sthurlow #include "common.h"
53*6007Sthurlow 
54*6007Sthurlow #ifdef I18N	/* not defined, put here so xgettext(1) can find strings */
55*6007Sthurlow static char *shtype[] = {
56*6007Sthurlow 	gettext("disk"),
57*6007Sthurlow 	gettext("printer"),
58*6007Sthurlow 	gettext("device"),	/* Communications device */
59*6007Sthurlow 	gettext("IPC"), 	/* Inter process communication */
60*6007Sthurlow 	gettext("unknown")
61*6007Sthurlow };
62*6007Sthurlow #else
63*6007Sthurlow static char *shtype[] = {
64*6007Sthurlow 	"disk",
65*6007Sthurlow 	"printer",
66*6007Sthurlow 	"device",		/* Communications device */
67*6007Sthurlow 	"IPC",  		/* IPC Inter process communication */
68*6007Sthurlow 	"unknown"
69*6007Sthurlow };
70*6007Sthurlow #endif
71*6007Sthurlow 
72*6007Sthurlow int
73*6007Sthurlow cmd_view(int argc, char *argv[])
74*6007Sthurlow {
75*6007Sthurlow 	struct smb_ctx sctx, *ctx = &sctx;
76*6007Sthurlow 	struct share_info *share_info, *ep;
77*6007Sthurlow 	int error, opt, i, entries, total;
78*6007Sthurlow 
79*6007Sthurlow 	if (argc < 2)
80*6007Sthurlow 		view_usage();
81*6007Sthurlow 	error = smb_ctx_init(ctx, argc, argv, SMBL_VC, SMBL_VC, SMB_ST_ANY);
82*6007Sthurlow 	if (error)
83*6007Sthurlow 		exit(error);
84*6007Sthurlow 	error = smb_ctx_readrc(ctx);
85*6007Sthurlow 	if (error)
86*6007Sthurlow 		exit(error);
87*6007Sthurlow 	if (smb_rc)
88*6007Sthurlow 		rc_close(smb_rc);
89*6007Sthurlow 	while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) {
90*6007Sthurlow 		switch (opt) {
91*6007Sthurlow 		case STDPARAM_ARGS:
92*6007Sthurlow 			error = smb_ctx_opt(ctx, opt, optarg);
93*6007Sthurlow 			if (error)
94*6007Sthurlow 				exit(error);
95*6007Sthurlow 			break;
96*6007Sthurlow 		default:
97*6007Sthurlow 			view_usage();
98*6007Sthurlow 			/*NOTREACHED*/
99*6007Sthurlow 		}
100*6007Sthurlow 	}
101*6007Sthurlow #ifdef APPLE
102*6007Sthurlow 	if (loadsmbvfs())
103*6007Sthurlow 		fprintf(stderr, gettext("SMB filesystem is not available"));
104*6007Sthurlow #endif
105*6007Sthurlow reauth:
106*6007Sthurlow 	smb_ctx_setshare(ctx, "IPC$", SMB_ST_ANY);
107*6007Sthurlow 	error = smb_ctx_resolve(ctx);
108*6007Sthurlow 	if (error)
109*6007Sthurlow 		exit(error);
110*6007Sthurlow 	error = smb_ctx_lookup(ctx, SMBL_SHARE, SMBLK_CREATE);
111*6007Sthurlow 	if (ctx->ct_flags & SMBCF_KCFOUND && smb_autherr(error)) {
112*6007Sthurlow 		ctx->ct_ssn.ioc_password[0] = '\0';
113*6007Sthurlow 		goto reauth;
114*6007Sthurlow 	}
115*6007Sthurlow 	if (error) {
116*6007Sthurlow 		smb_error(gettext("could not login to server %s"),
117*6007Sthurlow 		    error, ctx->ct_ssn.ioc_srvname);
118*6007Sthurlow 		exit(error);
119*6007Sthurlow 	}
120*6007Sthurlow 	printf(gettext("Share        Type       Comment\n"));
121*6007Sthurlow 	printf("-------------------------------\n");
122*6007Sthurlow 	error = smb_netshareenum(ctx, &entries, &total, &share_info);
123*6007Sthurlow 	if (error) {
124*6007Sthurlow 		smb_error(gettext("unable to list resources"), error);
125*6007Sthurlow 		exit(error);
126*6007Sthurlow 	}
127*6007Sthurlow 	for (ep = share_info, i = 0; i < entries; i++, ep++) {
128*6007Sthurlow 		int sti = ep->type & STYPE_MASK;
129*6007Sthurlow 		if (sti > STYPE_UNKNOWN)
130*6007Sthurlow 			sti = STYPE_UNKNOWN;
131*6007Sthurlow 		printf("%-12s %-10s %s\n", ep->netname,
132*6007Sthurlow 		    gettext(shtype[sti]),
133*6007Sthurlow 		    ep->remark ? ep->remark : "");
134*6007Sthurlow 		free(ep->netname);
135*6007Sthurlow 	}
136*6007Sthurlow 	printf(gettext("\n%d shares listed from %d available\n"),
137*6007Sthurlow 	    entries, total);
138*6007Sthurlow 	free(share_info);
139*6007Sthurlow 	smb_ctx_done(ctx);
140*6007Sthurlow #ifdef APPLE
141*6007Sthurlow 	smb_save2keychain(ctx);
142*6007Sthurlow #endif
143*6007Sthurlow 	return (0);
144*6007Sthurlow }
145*6007Sthurlow 
146*6007Sthurlow 
147*6007Sthurlow void
148*6007Sthurlow view_usage(void)
149*6007Sthurlow {
150*6007Sthurlow 	printf(gettext("usage: smbutil view [connection options] //"
151*6007Sthurlow 	    "[workgroup;][user[:password]@]server\n"));
152*6007Sthurlow 	exit(1);
153*6007Sthurlow }
154