#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 const char *shtype[] = {
"disk",
"printer",
"comm",
"pipe",
"unknown"
};
int
cmd_view(int argc, char *argv[])
{
struct smb_ctx *ctx = NULL;
struct share_info *share_info = NULL;
struct share_info *ep;
int error, opt, i, entries, total;
const char *cp;
const 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 = cp;
break;
}
if (! url)
view_usage();
errno = smb_ctx_init(&ctx, url, SMBL_VC, SMB_ST_ANY, FALSE);
if (errno) {
if (errno == ENOMEM)
err(EX_UNAVAILABLE, "failed to intitialize the smb library");
else
err(EX_UNAVAILABLE, "URL parsing failed, please correct the URL and try again");
}
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_MECHTYPE_KRB5) {
ctx->ct_ssn.ioc_opt |= SMBV_KERBEROS_ACCESS;
error = smb_session_security(ctx, NULL, NULL);
} else if (ctx->ct_vc_shared) {
error = smb_session_security(ctx, NULL, NULL);
} else
error = ENOTSUP;
if (error) {
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);
}
if ((ctx->ct_setup.ioc_user[0] == 0) && (ctx->ct_setup.ioc_password[0] == 0)) {
ctx->ct_ssn.ioc_opt |= SMBV_ANONYMOUS_ACCESS;
fprintf(stderr, "Connecting with anonymous authentication.\n");
}
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);
smb_freeshareinfo(share_info, entries);
smb_ctx_done(ctx);
return 0;
}
void
view_usage(void)
{
fprintf(stderr, "usage: smbutil view [connection options] //"
"[domain;][user[:password]@]"
"server\n");
exit(1);
}