xref: /titanic_44/usr/src/cmd/fs.d/smbclnt/smbutil/view.c (revision 2d6b5ea734bb47d251c82670646fde46af15fd69)
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 #include <sys/param.h>
36 #include <sys/errno.h>
37 #include <sys/stat.h>
38 #include <stdio.h>
39 #include <err.h>
40 #include <unistd.h>
41 #include <strings.h>
42 #include <stdlib.h>
43 #include <sysexits.h>
44 #include <libintl.h>
45 
46 #include <cflib.h>
47 #include <netsmb/smb_lib.h>
48 #include <netsmb/smb_netshareenum.h>
49 
50 #include "common.h"
51 
52 #ifdef I18N	/* not defined, put here so xgettext(1) can find strings */
53 static char *shtype[] = {
54 	gettext("disk"),
55 	gettext("printer"),
56 	gettext("device"),	/* Communications device */
57 	gettext("IPC"), 	/* Inter process communication */
58 	gettext("unknown")
59 };
60 #else
61 static char *shtype[] = {
62 	"disk",
63 	"printer",
64 	"device",		/* Communications device */
65 	"IPC",  		/* IPC Inter process communication */
66 	"unknown"
67 };
68 #endif
69 
70 int
71 cmd_view(int argc, char *argv[])
72 {
73 	struct smb_ctx sctx, *ctx = &sctx;
74 	struct share_info *share_info, *ep;
75 	int error, opt, i, entries, total;
76 
77 	if (argc < 2)
78 		view_usage();
79 	error = smb_ctx_init(ctx, argc, argv, SMBL_VC, SMBL_VC, SMB_ST_ANY);
80 	if (error)
81 		exit(error);
82 	error = smb_ctx_readrc(ctx);
83 	if (error)
84 		exit(error);
85 	if (smb_rc)
86 		rc_close(smb_rc);
87 	while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) {
88 		switch (opt) {
89 		case STDPARAM_ARGS:
90 			error = smb_ctx_opt(ctx, opt, optarg);
91 			if (error)
92 				exit(error);
93 			break;
94 		default:
95 			view_usage();
96 			/*NOTREACHED*/
97 		}
98 	}
99 #ifdef APPLE
100 	if (loadsmbvfs())
101 		fprintf(stderr, gettext("SMB filesystem is not available"));
102 #endif
103 reauth:
104 	smb_ctx_setshare(ctx, "IPC$", SMB_ST_ANY);
105 	error = smb_ctx_resolve(ctx);
106 	if (error)
107 		exit(error);
108 	error = smb_ctx_lookup(ctx, SMBL_SHARE, SMBLK_CREATE);
109 	if (ctx->ct_flags & SMBCF_KCFOUND && smb_autherr(error)) {
110 		ctx->ct_ssn.ioc_password[0] = '\0';
111 		goto reauth;
112 	}
113 	if (error) {
114 		smb_error(gettext("could not login to server %s"),
115 		    error, ctx->ct_ssn.ioc_srvname);
116 		exit(error);
117 	}
118 	printf(gettext("Share        Type       Comment\n"));
119 	printf("-------------------------------\n");
120 	error = smb_netshareenum(ctx, &entries, &total, &share_info);
121 	if (error) {
122 		smb_error(gettext("unable to list resources"), error);
123 		exit(error);
124 	}
125 	for (ep = share_info, i = 0; i < entries; i++, ep++) {
126 		int sti = ep->type & STYPE_MASK;
127 		if (sti > STYPE_UNKNOWN)
128 			sti = STYPE_UNKNOWN;
129 		printf("%-12s %-10s %s\n", ep->netname,
130 		    gettext(shtype[sti]),
131 		    ep->remark ? ep->remark : "");
132 		free(ep->netname);
133 		free(ep->remark);
134 	}
135 	printf(gettext("\n%d shares listed from %d available\n"),
136 	    entries, total);
137 
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