xref: /titanic_50/usr/src/cmd/fs.d/smbclnt/smbutil/view.c (revision 6c258465bad88e154b792fa29bed32dfd376ced0)
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 /*
36  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37  * Use is subject to license terms.
38  */
39 
40 #include <sys/types.h>
41 #include <errno.h>
42 #include <stdio.h>
43 #include <err.h>
44 #include <unistd.h>
45 #include <strings.h>
46 #include <stdlib.h>
47 #include <sysexits.h>
48 #include <libintl.h>
49 
50 #include <netsmb/smb.h>
51 #include <netsmb/smb_lib.h>
52 #include <netsmb/smb_netshareenum.h>
53 
54 #include "common.h"
55 
56 int enum_shares(smb_ctx_t *);
57 void print_shares(int, int, struct share_info *);
58 
59 void
60 view_usage(void)
61 {
62 	printf(gettext("usage: smbutil view [connection options] //"
63 	    "[workgroup;][user[:password]@]server\n"));
64 	exit(1);
65 }
66 
67 int
68 cmd_view(int argc, char *argv[])
69 {
70 	struct smb_ctx *ctx;
71 	int error, err2, opt;
72 
73 	if (argc < 2)
74 		view_usage();
75 
76 	error = smb_ctx_alloc(&ctx);
77 	if (error)
78 		return (error);
79 
80 	error = smb_ctx_scan_argv(ctx, argc, argv,
81 	    SMBL_SERVER, SMBL_SERVER, USE_WILDCARD);
82 	if (error)
83 		return (error);
84 
85 	error = smb_ctx_readrc(ctx);
86 	if (error)
87 		return (error);
88 
89 	while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) {
90 		if (opt == '?')
91 			view_usage();
92 		error = smb_ctx_opt(ctx, opt, optarg);
93 		if (error)
94 			return (error);
95 	}
96 
97 	smb_ctx_setshare(ctx, "IPC$", USE_IPC);
98 
99 	/*
100 	 * Resolve the server address,
101 	 * setup derived defaults.
102 	 */
103 	error = smb_ctx_resolve(ctx);
104 	if (error)
105 		return (error);
106 
107 	/*
108 	 * Have server, share, etc. from above:
109 	 * smb_ctx_scan_argv, option settings.
110 	 * Get the session and tree.
111 	 */
112 again:
113 	error = smb_ctx_get_ssn(ctx);
114 	if (error == EAUTH) {
115 		err2 = smb_get_authentication(ctx);
116 		if (err2 == 0)
117 			goto again;
118 	}
119 	if (error) {
120 		smb_error(gettext("//%s: login failed"),
121 		    error, ctx->ct_fullserver);
122 		return (error);
123 	}
124 
125 	error = smb_ctx_get_tree(ctx);
126 	if (error) {
127 		smb_error(gettext("//%s/%s: tree connect failed"),
128 		    error, ctx->ct_fullserver, ctx->ct_origshare);
129 		return (error);
130 	}
131 
132 	/*
133 	 * Have IPC$ tcon, now list shares.
134 	 */
135 	error = enum_shares(ctx);
136 	if (error) {
137 		smb_error("cannot list shares", error);
138 		return (error);
139 	}
140 
141 	smb_ctx_free(ctx);
142 	return (0);
143 }
144 
145 #ifdef I18N	/* not defined, put here so xgettext(1) can find strings */
146 static char *shtype[] = {
147 	gettext("disk"),
148 	gettext("printer"),
149 	gettext("device"),	/* Communications device */
150 	gettext("IPC"), 	/* Inter process communication */
151 	gettext("unknown")
152 };
153 #else
154 static char *shtype[] = {
155 	"disk",
156 	"printer",
157 	"device",		/* Communications device */
158 	"IPC",  		/* IPC Inter process communication */
159 	"unknown"
160 };
161 #endif
162 
163 int
164 enum_shares(smb_ctx_t *ctx)
165 {
166 	struct share_info *share_info;
167 	int error, entries, total;
168 
169 	/*
170 	 * XXX: Later, try RPC first,
171 	 * then fall back to RAP...
172 	 */
173 	error = smb_netshareenum(ctx, &entries, &total, &share_info);
174 	if (error) {
175 		smb_error(gettext("unable to list resources"), error);
176 		return (error);
177 	}
178 	print_shares(entries, total, share_info);
179 	return (0);
180 }
181 
182 void
183 print_shares(int entries, int total,
184 	struct share_info *share_info)
185 {
186 	struct share_info *ep;
187 	int i;
188 
189 	printf(gettext("Share        Type       Comment\n"));
190 	printf("-------------------------------\n");
191 
192 	for (ep = share_info, i = 0; i < entries; i++, ep++) {
193 		int sti = ep->type & STYPE_MASK;
194 		if (sti > STYPE_UNKNOWN)
195 			sti = STYPE_UNKNOWN;
196 		printf("%-12s %-10s %s\n", ep->netname,
197 		    gettext(shtype[sti]),
198 		    ep->remark ? ep->remark : "");
199 		free(ep->netname);
200 		free(ep->remark);
201 	}
202 	printf(gettext("\n%d shares listed from %d available\n"),
203 	    entries, total);
204 
205 	free(share_info);
206 }
207