xref: /illumos-gate/usr/src/cmd/fs.d/smbclnt/smbutil/view.c (revision 17a5fa85fe0c34b1146222e40a80b42f2aae8500)
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 (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
37  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
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 "common.h"
53 
54 void
55 view_usage(void)
56 {
57 	printf(gettext("usage: smbutil view [connection options] //"
58 	    "[workgroup;][user[:password]@]server\n"));
59 	exit(1);
60 }
61 
62 int
63 cmd_view(int argc, char *argv[])
64 {
65 	struct smb_ctx *ctx;
66 	int error, err2, opt;
67 
68 	if (argc < 2)
69 		view_usage();
70 
71 	error = smb_ctx_alloc(&ctx);
72 	if (error)
73 		return (error);
74 
75 	error = smb_ctx_scan_argv(ctx, argc, argv,
76 	    SMBL_SERVER, SMBL_SERVER, USE_WILDCARD);
77 	if (error)
78 		goto out;
79 
80 	error = smb_ctx_readrc(ctx);
81 	if (error)
82 		goto out;
83 
84 	while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) {
85 		if (opt == '?')
86 			view_usage();
87 		error = smb_ctx_opt(ctx, opt, optarg);
88 		if (error)
89 			goto out;
90 	}
91 
92 	smb_ctx_setshare(ctx, "IPC$", USE_IPC);
93 
94 	/*
95 	 * Resolve the server address,
96 	 * setup derived defaults.
97 	 */
98 	error = smb_ctx_resolve(ctx);
99 	if (error)
100 		goto out;
101 
102 	/*
103 	 * Have server, share, etc. from above:
104 	 * smb_ctx_scan_argv, option settings.
105 	 * Get the session and tree.
106 	 */
107 again:
108 	error = smb_ctx_get_ssn(ctx);
109 	if (error == EAUTH) {
110 		err2 = smb_get_authentication(ctx);
111 		if (err2 == 0)
112 			goto again;
113 	}
114 	if (error) {
115 		smb_error(gettext("//%s: login failed"),
116 		    error, ctx->ct_fullserver);
117 		goto out;
118 	}
119 
120 	error = smb_ctx_get_tree(ctx);
121 	if (error) {
122 		smb_error(gettext("//%s/%s: tree connect failed"),
123 		    error, ctx->ct_fullserver, ctx->ct_origshare);
124 		goto out;
125 	}
126 
127 	/*
128 	 * Have IPC$ tcon, now list shares.
129 	 */
130 	error = share_enum_rpc(ctx, ctx->ct_fullserver);
131 
132 out:
133 	smb_ctx_free(ctx);
134 	return (error);
135 }
136 
137 #ifdef I18N	/* not defined, put here so xgettext(1) can find strings */
138 static char *shtype[] = {
139 	gettext("disk"),
140 	gettext("printer"),
141 	gettext("device"),	/* Communications device */
142 	gettext("IPC"),		/* Inter process communication */
143 	gettext("unknown")
144 };
145 #else
146 static char *shtype[] = {
147 	"disk",
148 	"printer",
149 	"device",		/* Communications device */
150 	"IPC",			/* IPC Inter process communication */
151 	"unknown"
152 };
153 #endif
154 
155 /*
156  * Print one line of the share list, or
157  * if SHARE is null, print the header line.
158  */
159 void
160 view_print_share(char *share, int type, char *comment)
161 {
162 	char *stname;
163 	int stindex;
164 
165 	if (share == NULL) {
166 		printf(gettext("Share        Type       Comment\n"));
167 		printf("-------------------------------\n");
168 		return;
169 	}
170 
171 	stindex = type & STYPE_MASK;
172 	if (stindex > STYPE_UNKNOWN)
173 		stindex = STYPE_UNKNOWN;
174 	stname = gettext(shtype[stindex]);
175 
176 	if (comment == NULL)
177 		comment = "";
178 
179 	printf("%-12s %-10s %s\n", share, stname, comment);
180 }
181