aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/smbfs/smbutil/smbutil.c
blob: d66003f4346f9b167b72876a0c93eb1cb9d62eb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <sys/param.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <err.h>
#include <sysexits.h>

#include <netsmb/smb_lib.h>
#include <netsmb/smb_conn.h>

#include "common.h"

extern char *__progname;

static void help(void);
static void help_usage(void);
static int  cmd_crypt(int argc, char *argv[]);
static int  cmd_help(int argc, char *argv[]);

int verbose;

typedef int cmd_fn_t (int argc, char *argv[]);
typedef void cmd_usage_t (void);

#define	CMDFL_NO_KMOD	0x0001

static struct commands {
	const char *	name;
	cmd_fn_t*	fn;
	cmd_usage_t *	usage;
	int 		flags;
} commands[] = {
	{"crypt",	cmd_crypt,	NULL, CMDFL_NO_KMOD},
	{"help",	cmd_help,	help_usage, CMDFL_NO_KMOD},
	{"lc",		cmd_dumptree,	NULL},
	{"login",	cmd_login,	login_usage},
	{"logout",	cmd_logout,	logout_usage},
	{"lookup",	cmd_lookup,	lookup_usage, CMDFL_NO_KMOD},
	{"print",	cmd_print,	print_usage},
	{"view",	cmd_view,	view_usage},
	{NULL, NULL}
};

static struct commands *
lookupcmd(const char *name)
{
	struct commands *cmd;

	for (cmd = commands; cmd->name; cmd++) {
		if (strcmp(cmd->name, name) == 0)
			return cmd;
	}
	return NULL;
}

int
cmd_crypt(int argc, char *argv[])
{
	char *cp, *psw;
    
	if (argc < 2)
		psw = getpass("Password:");
	else
		psw = argv[1];
	cp = malloc(strlen(psw + 4));
	if (cp == NULL)
		errx(EX_DATAERR, "out of memory");
	smb_simplecrypt(cp, psw);
	printf("%s\n", cp);
	free(cp);
	exit(0);
}

int
cmd_help(int argc, char *argv[])
{
	struct commands *cmd;
	char *cp;
    
	if (argc < 2)
		help_usage();
	cp = argv[1];
	cmd = lookupcmd(cp);
	if (cmd == NULL)
		errx(EX_DATAERR, "unknown command %s", cp);
	if (cmd->usage == NULL)
		errx(EX_DATAERR, "no specific help for command %s", cp);
	cmd->usage();
	exit(0);
}

int
main(int argc, char *argv[])
{
	struct commands *cmd;
	char *cp;
	int opt;

	if (argc < 2)
		help();

	while ((opt = getopt(argc, argv, "hv")) != EOF) {
		switch (opt) {
		    case 'h':
			help();
			/*NOTREACHED */
		    case 'v':
			verbose = 1;
			break;
		    default:
			warnx("invalid option %c", opt);
			help();
			/*NOTREACHED */
		}
	}
	if (optind >= argc)
		help();

	cp = argv[optind];
	cmd = lookupcmd(cp);
	if (cmd == NULL)
		errx(EX_DATAERR, "unknown command %s", cp);

	if ((cmd->flags & CMDFL_NO_KMOD) == 0 && smb_lib_init() != 0)
		exit(1);

	argc -= optind;
	argv += optind;
	optind = optreset = 1;
	return cmd->fn(argc, argv);
}

static void
help(void) {
	printf("\n");
	printf("usage: %s [-hv] command [args]\n", __progname);
	printf("where commands are:\n"
	" crypt [password]		slightly encrypt password\n"
	" help command			display help on \"command\"\n"
	" lc 				display active connections\n"
	" login //user@host[/share]	login to the specified host\n"
	" logout //user@host[/share]	logout from the specified host\n"
	" print //user@host/share file	print file to the specified remote printer\n"
	" view //user@host		list resources on the specified host\n"
	"\n");
	exit(1);
}

static void
help_usage(void) {
	printf("usage: smbutil help command\n");
	exit(1);
}