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