#include <sys/param.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <err.h>
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <stdlib.h>
#include <sysexits.h>
#include <cflib.h>
#include <sys/mchain.h>
#include <netsmb/smb_lib.h>
#include <netsmb/smb_conn.h>
#include <netsmb/smb_netshareenum.h>
#include "common.h"
static char *shtype[] = {
"disk",
"printer",
"comm",
"pipe",
"unknown"
};
int
cmd_view(int argc, char *argv[])
{
struct smb_ctx *ctx = NULL;
struct share_info *share_info, *ep;
int error, opt, i, entries, total;
const char *cp;
char * url = NULL;
int prompt_user = (isatty(STDIN_FILENO)) ? TRUE : FALSE;
if (argc < 2)
view_usage();
for (opt = 1; opt < argc; opt++) {
cp = argv[opt];
if (strncmp(cp, "//", 2) != 0)
continue;
url = (char *)cp;
break;
}
if (! url)
view_usage();
error = smb_ctx_init(&ctx, url, SMBL_VC, SMB_ST_ANY, FALSE);
if (error)
exit(error);
while ((opt = getopt(argc, argv, "N")) != EOF) {
switch(opt){
case 'N':
prompt_user = FALSE;
break;
default:
view_usage();
}
}
smb_ctx_setshare(ctx, "IPC$", SMB_ST_ANY);
errno = smb_connect(ctx);
if (errno)
err(EX_NOHOST, "server connection failed");
if (ctx->ct_vc_flags & SMBV_KERBEROS_SUPPORT)
error = smb_session_security(ctx, NULL, NULL);
else if (ctx->ct_ssn.ioc_opt & SMBV_EXT_SEC)
error = ENOTSUP;
else
error = smb_session_security(ctx, NULL, NULL);
if (error) {
ctx->ct_ssn.ioc_opt &= ~SMBV_EXT_SEC;
ctx->ct_flags &= ~SMBCF_CONNECTED;
errno = smb_connect(ctx);
if (errno)
err(EX_NOHOST, "server connection failed");
if (prompt_user && ((ctx->ct_flags & SMBCF_EXPLICITPWD) != SMBCF_EXPLICITPWD)) {
char passwd[SMB_MAXPASSWORDLEN + 1];
strncpy(passwd, getpass(SMB_PASSWORD_KEY ":"), SMB_MAXPASSWORDLEN);
smb_ctx_setpassword(ctx, passwd);
}
errno = smb_session_security(ctx, NULL, NULL);
if (errno)
err(EX_NOPERM, "server rejected the connection");
}
errno = smb_share_connect(ctx);
if (errno)
err(EX_IOERR, "connection to the share failed");
fprintf(stdout, "Share Type Comment\n");
fprintf(stdout, "-------------------------------\n");
errno = smb_netshareenum(ctx, &entries, &total, &share_info);
if (errno)
err(EX_IOERR, "unable to list resources");
for (ep = share_info, i = 0; i < entries; i++, ep++) {
fprintf(stdout, "%-12s %-10s %s\n", ep->netname,
shtype[min(ep->type, sizeof shtype / sizeof(char *) - 1)],
ep->remark ? ep->remark : "");
}
fprintf(stdout, "\n%d shares listed from %d available\n", entries, total);
free(share_info);
smb_ctx_done(ctx);
return 0;
}
void
view_usage(void)
{
fprintf(stderr, "usage: smbutil view [connection options] //"
"[domain;][user[:password]@]"
"server\n");
exit(1);
}