187866Ssheldonh/*
295267Ssheldonh * Copyright (c) 2000-2002, Boris Popov
387866Ssheldonh * All rights reserved.
487866Ssheldonh *
587866Ssheldonh * Redistribution and use in source and binary forms, with or without
687866Ssheldonh * modification, are permitted provided that the following conditions
787866Ssheldonh * are met:
887866Ssheldonh * 1. Redistributions of source code must retain the above copyright
987866Ssheldonh *    notice, this list of conditions and the following disclaimer.
1087866Ssheldonh * 2. Redistributions in binary form must reproduce the above copyright
1187866Ssheldonh *    notice, this list of conditions and the following disclaimer in the
1287866Ssheldonh *    documentation and/or other materials provided with the distribution.
1387866Ssheldonh * 3. All advertising materials mentioning features or use of this software
1487866Ssheldonh *    must display the following acknowledgement:
1587866Ssheldonh *    This product includes software developed by Boris Popov.
1687866Ssheldonh * 4. Neither the name of the author nor the names of any co-contributors
1787866Ssheldonh *    may be used to endorse or promote products derived from this software
1887866Ssheldonh *    without specific prior written permission.
1987866Ssheldonh *
2087866Ssheldonh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2187866Ssheldonh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2287866Ssheldonh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2387866Ssheldonh * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2487866Ssheldonh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2587866Ssheldonh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2687866Ssheldonh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2787866Ssheldonh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2887866Ssheldonh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2987866Ssheldonh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3087866Ssheldonh * SUCH DAMAGE.
3187866Ssheldonh *
3295267Ssheldonh * $Id: view.c,v 1.9 2002/02/20 09:26:42 bp Exp $
3387866Ssheldonh */
34150312Simura
35150312Simura#include <sys/cdefs.h>
36150312Simura__FBSDID("$FreeBSD$");
37150312Simura
38150312Simura#include <sys/endian.h>
3987866Ssheldonh#include <sys/param.h>
4087866Ssheldonh#include <sys/errno.h>
4187866Ssheldonh#include <sys/stat.h>
4287866Ssheldonh#include <sys/iconv.h>
4387866Ssheldonh#include <err.h>
4487866Ssheldonh#include <stdio.h>
4587866Ssheldonh#include <unistd.h>
4687866Ssheldonh#include <strings.h>
4787866Ssheldonh#include <stdlib.h>
4887866Ssheldonh#include <sysexits.h>
4987866Ssheldonh
5087866Ssheldonh#include <cflib.h>
5187866Ssheldonh
5287866Ssheldonh#include <netsmb/smb_lib.h>
5387866Ssheldonh#include <netsmb/smb_conn.h>
5487866Ssheldonh#include <netsmb/smb_rap.h>
5587866Ssheldonh
5687866Ssheldonh#include "common.h"
5787866Ssheldonh
5887866Ssheldonhstatic char *shtype[] = {
5987866Ssheldonh	"disk",
6087866Ssheldonh	"printer",
6195267Ssheldonh	"comm",		/* Communications device */
6295267Ssheldonh	"pipe",		/* IPC Inter process communication */
6387866Ssheldonh	"unknown"
6487866Ssheldonh};
6587866Ssheldonh
6687866Ssheldonhint
6787866Ssheldonhcmd_view(int argc, char *argv[])
6887866Ssheldonh{
6987866Ssheldonh	struct smb_ctx sctx, *ctx = &sctx;
7087866Ssheldonh	struct smb_share_info_1 *rpbuf, *ep;
7187866Ssheldonh	char *cp;
72202980Syongari	u_int32_t remark;
7395267Ssheldonh	u_int16_t type;
7487866Ssheldonh	int error, opt, bufsize, i, entries, total;
7595267Ssheldonh
7687866Ssheldonh
7787866Ssheldonh	if (argc < 2)
7887866Ssheldonh		view_usage();
7987866Ssheldonh	if (smb_ctx_init(ctx, argc, argv, SMBL_VC, SMBL_VC, SMB_ST_ANY) != 0)
8087866Ssheldonh		exit(1);
8187866Ssheldonh	if (smb_ctx_readrc(ctx) != 0)
8287866Ssheldonh		exit(1);
8387866Ssheldonh	if (smb_rc)
8487866Ssheldonh		rc_close(smb_rc);
8587866Ssheldonh	while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) {
8687866Ssheldonh		switch(opt){
8787866Ssheldonh		    case STDPARAM_ARGS:
8887866Ssheldonh			error = smb_ctx_opt(ctx, opt, optarg);
8987866Ssheldonh			if (error)
9087866Ssheldonh				exit(1);
9187866Ssheldonh			break;
9287866Ssheldonh		    default:
9387866Ssheldonh			view_usage();
9487866Ssheldonh			/*NOTREACHED*/
9587866Ssheldonh		}
9687866Ssheldonh	}
9788282Ssheldonh#ifdef APPLE
9888282Ssheldonh	if (loadsmbvfs())
9988282Ssheldonh		errx(EX_OSERR, "SMB filesystem is not available");
10088282Ssheldonh#endif
10187866Ssheldonh	smb_ctx_setshare(ctx, "IPC$", SMB_ST_ANY);
10287866Ssheldonh	if (smb_ctx_resolve(ctx) != 0)
10387866Ssheldonh		exit(1);
10487866Ssheldonh	error = smb_ctx_lookup(ctx, SMBL_SHARE, SMBLK_CREATE);
10587866Ssheldonh	if (error) {
10687866Ssheldonh		smb_error("could not login to server %s", error, ctx->ct_ssn.ioc_srvname);
10787866Ssheldonh		exit(1);
10887866Ssheldonh	}
10987866Ssheldonh	printf("Share        Type       Comment\n");
11087866Ssheldonh	printf("-------------------------------\n");
11195267Ssheldonh	bufsize = 0xffe0; /* samba notes win2k bug with 65535 */
11287866Ssheldonh	rpbuf = malloc(bufsize);
11387866Ssheldonh	error = smb_rap_NetShareEnum(ctx, 1, rpbuf, bufsize, &entries, &total);
11487866Ssheldonh	if (error &&
11587866Ssheldonh	    error != (SMB_ERROR_MORE_DATA | SMB_RAP_ERROR)) {
11687866Ssheldonh		smb_error("unable to list resources", error);
11787866Ssheldonh		exit(1);
11887866Ssheldonh	}
11987866Ssheldonh	for (ep = rpbuf, i = 0; i < entries; i++, ep++) {
120150312Simura		type = le16toh(ep->shi1_type);
121202980Syongari		remark = le32toh(ep->shi1_remark);
122202980Syongari		remark &= 0xFFFF;
12395267Ssheldonh
124202980Syongari		cp = (char*)rpbuf + remark;
12587866Ssheldonh		printf("%-12s %-10s %s\n", ep->shi1_netname,
12695267Ssheldonh		    shtype[min(type, sizeof shtype / sizeof(char *) - 1)],
127202980Syongari		    remark ? nls_str_toloc(cp, cp) : "");
12887866Ssheldonh	}
12987866Ssheldonh	printf("\n%d shares listed from %d available\n", entries, total);
13087866Ssheldonh	free(rpbuf);
13187866Ssheldonh	return 0;
13287866Ssheldonh}
13387866Ssheldonh
13487866Ssheldonh
13587866Ssheldonhvoid
13687866Ssheldonhview_usage(void)
13787866Ssheldonh{
13887866Ssheldonh	printf(
139161207Skeramida	"usage: smbutil view [connection options] //[user@]server\n"
14087866Ssheldonh	);
14187866Ssheldonh	exit(1);
14287866Ssheldonh}
14387866Ssheldonh
144