xref: /onnv-gate/usr/src/cmd/fs.d/smbclnt/smbutil/view.c (revision 8271:792589b3384f)
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 
356007Sthurlow #include <sys/param.h>
366007Sthurlow #include <sys/errno.h>
376007Sthurlow #include <sys/stat.h>
386007Sthurlow #include <stdio.h>
396007Sthurlow #include <err.h>
406007Sthurlow #include <unistd.h>
416007Sthurlow #include <strings.h>
426007Sthurlow #include <stdlib.h>
436007Sthurlow #include <sysexits.h>
446007Sthurlow #include <libintl.h>
456007Sthurlow 
466007Sthurlow #include <cflib.h>
476007Sthurlow #include <netsmb/smb_lib.h>
486007Sthurlow #include <netsmb/smb_netshareenum.h>
496007Sthurlow 
506007Sthurlow #include "common.h"
516007Sthurlow 
526007Sthurlow #ifdef I18N	/* not defined, put here so xgettext(1) can find strings */
536007Sthurlow static char *shtype[] = {
546007Sthurlow 	gettext("disk"),
556007Sthurlow 	gettext("printer"),
566007Sthurlow 	gettext("device"),	/* Communications device */
576007Sthurlow 	gettext("IPC"), 	/* Inter process communication */
586007Sthurlow 	gettext("unknown")
596007Sthurlow };
606007Sthurlow #else
616007Sthurlow static char *shtype[] = {
626007Sthurlow 	"disk",
636007Sthurlow 	"printer",
646007Sthurlow 	"device",		/* Communications device */
656007Sthurlow 	"IPC",  		/* IPC Inter process communication */
666007Sthurlow 	"unknown"
676007Sthurlow };
686007Sthurlow #endif
696007Sthurlow 
706007Sthurlow int
716007Sthurlow cmd_view(int argc, char *argv[])
726007Sthurlow {
736007Sthurlow 	struct smb_ctx sctx, *ctx = &sctx;
746007Sthurlow 	struct share_info *share_info, *ep;
756007Sthurlow 	int error, opt, i, entries, total;
766007Sthurlow 
776007Sthurlow 	if (argc < 2)
786007Sthurlow 		view_usage();
796007Sthurlow 	error = smb_ctx_init(ctx, argc, argv, SMBL_VC, SMBL_VC, SMB_ST_ANY);
806007Sthurlow 	if (error)
816007Sthurlow 		exit(error);
826007Sthurlow 	error = smb_ctx_readrc(ctx);
836007Sthurlow 	if (error)
846007Sthurlow 		exit(error);
856007Sthurlow 	if (smb_rc)
866007Sthurlow 		rc_close(smb_rc);
876007Sthurlow 	while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) {
886007Sthurlow 		switch (opt) {
896007Sthurlow 		case STDPARAM_ARGS:
906007Sthurlow 			error = smb_ctx_opt(ctx, opt, optarg);
916007Sthurlow 			if (error)
926007Sthurlow 				exit(error);
936007Sthurlow 			break;
946007Sthurlow 		default:
956007Sthurlow 			view_usage();
966007Sthurlow 			/*NOTREACHED*/
976007Sthurlow 		}
986007Sthurlow 	}
996007Sthurlow #ifdef APPLE
1006007Sthurlow 	if (loadsmbvfs())
1016007Sthurlow 		fprintf(stderr, gettext("SMB filesystem is not available"));
1026007Sthurlow #endif
1036007Sthurlow reauth:
1046007Sthurlow 	smb_ctx_setshare(ctx, "IPC$", SMB_ST_ANY);
1056007Sthurlow 	error = smb_ctx_resolve(ctx);
1066007Sthurlow 	if (error)
1076007Sthurlow 		exit(error);
1086007Sthurlow 	error = smb_ctx_lookup(ctx, SMBL_SHARE, SMBLK_CREATE);
1096007Sthurlow 	if (ctx->ct_flags & SMBCF_KCFOUND && smb_autherr(error)) {
1106007Sthurlow 		ctx->ct_ssn.ioc_password[0] = '\0';
1116007Sthurlow 		goto reauth;
1126007Sthurlow 	}
1136007Sthurlow 	if (error) {
1146007Sthurlow 		smb_error(gettext("could not login to server %s"),
1156007Sthurlow 		    error, ctx->ct_ssn.ioc_srvname);
1166007Sthurlow 		exit(error);
1176007Sthurlow 	}
1186007Sthurlow 	printf(gettext("Share        Type       Comment\n"));
1196007Sthurlow 	printf("-------------------------------\n");
1206007Sthurlow 	error = smb_netshareenum(ctx, &entries, &total, &share_info);
1216007Sthurlow 	if (error) {
1226007Sthurlow 		smb_error(gettext("unable to list resources"), error);
1236007Sthurlow 		exit(error);
1246007Sthurlow 	}
1256007Sthurlow 	for (ep = share_info, i = 0; i < entries; i++, ep++) {
1266007Sthurlow 		int sti = ep->type & STYPE_MASK;
1276007Sthurlow 		if (sti > STYPE_UNKNOWN)
1286007Sthurlow 			sti = STYPE_UNKNOWN;
1296007Sthurlow 		printf("%-12s %-10s %s\n", ep->netname,
1306007Sthurlow 		    gettext(shtype[sti]),
1316007Sthurlow 		    ep->remark ? ep->remark : "");
1326007Sthurlow 		free(ep->netname);
133*8271SGordon.Ross@Sun.COM 		free(ep->remark);
1346007Sthurlow 	}
1356007Sthurlow 	printf(gettext("\n%d shares listed from %d available\n"),
1366007Sthurlow 	    entries, total);
137*8271SGordon.Ross@Sun.COM 
1386007Sthurlow 	free(share_info);
1396007Sthurlow 	smb_ctx_done(ctx);
1406007Sthurlow #ifdef APPLE
1416007Sthurlow 	smb_save2keychain(ctx);
1426007Sthurlow #endif
1436007Sthurlow 	return (0);
1446007Sthurlow }
1456007Sthurlow 
1466007Sthurlow 
1476007Sthurlow void
1486007Sthurlow view_usage(void)
1496007Sthurlow {
1506007Sthurlow 	printf(gettext("usage: smbutil view [connection options] //"
1516007Sthurlow 	    "[workgroup;][user[:password]@]server\n"));
1526007Sthurlow 	exit(1);
1536007Sthurlow }
154