disklabel[32,64] utilities - Change the default disklabel program
[dragonfly.git] / contrib / smbfs / smbutil / smbutil.c
blob4d14f9e4a3f476df9bb0b15719a59002995083c6
1 #include <sys/param.h>
2 #include <sys/time.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <err.h>
8 #include <sysexits.h>
10 #include <netsmb/smb_lib.h>
11 #include <netsmb/smb_conn.h>
13 #include "common.h"
15 extern char *__progname;
17 static void help(void);
18 static void help_usage(void);
19 static int cmd_crypt(int argc, char *argv[]);
20 static int cmd_help(int argc, char *argv[]);
22 int verbose;
24 typedef int cmd_fn_t (int argc, char *argv[]);
25 typedef void cmd_usage_t (void);
27 #define CMDFL_NO_KMOD 0x0001
29 static struct commands {
30 const char * name;
31 cmd_fn_t* fn;
32 cmd_usage_t * usage;
33 int flags;
34 } commands[] = {
35 {"crypt", cmd_crypt, NULL, CMDFL_NO_KMOD},
36 {"help", cmd_help, help_usage, CMDFL_NO_KMOD},
37 {"lc", cmd_dumptree, NULL},
38 {"login", cmd_login, login_usage},
39 {"logout", cmd_logout, logout_usage},
40 {"lookup", cmd_lookup, lookup_usage, CMDFL_NO_KMOD},
41 {"print", cmd_print, print_usage},
42 {"view", cmd_view, view_usage},
43 {NULL, NULL}
46 static struct commands *
47 lookupcmd(const char *name)
49 struct commands *cmd;
51 for (cmd = commands; cmd->name; cmd++) {
52 if (strcmp(cmd->name, name) == 0)
53 return cmd;
55 return NULL;
58 int
59 cmd_crypt(int argc, char *argv[])
61 char *cp, *psw;
63 if (argc < 2)
64 psw = getpass("Password:");
65 else
66 psw = argv[1];
67 cp = smb_simplecrypt(NULL, psw);
68 if (cp == NULL)
69 errx(EX_DATAERR, "out of memory");
70 printf("%s\n", cp);
71 free(cp);
72 exit(0);
75 int
76 cmd_help(int argc, char *argv[])
78 struct commands *cmd;
79 char *cp;
81 if (argc < 2)
82 help_usage();
83 cp = argv[1];
84 cmd = lookupcmd(cp);
85 if (cmd == NULL)
86 errx(EX_DATAERR, "unknown command %s", cp);
87 if (cmd->usage == NULL)
88 errx(EX_DATAERR, "no specific help for command %s", cp);
89 cmd->usage();
90 exit(0);
93 int
94 main(int argc, char *argv[])
96 struct commands *cmd;
97 char *cp;
98 int opt;
99 #ifdef APPLE
100 extern void dropsuid();
102 dropsuid();
103 #endif /* APPLE */
105 if (argc < 2)
106 help();
108 while ((opt = getopt(argc, argv, "hv")) != EOF) {
109 switch (opt) {
110 case 'h':
111 help();
112 /*NOTREACHED */
113 case 'v':
114 verbose = 1;
115 break;
116 default:
117 warnx("invalid option %c", opt);
118 help();
119 /*NOTREACHED */
122 if (optind >= argc)
123 help();
125 cp = argv[optind];
126 cmd = lookupcmd(cp);
127 if (cmd == NULL)
128 errx(EX_DATAERR, "unknown command %s", cp);
130 if ((cmd->flags & CMDFL_NO_KMOD) == 0 && smb_lib_init() != 0)
131 exit(1);
133 argc -= optind;
134 argv += optind;
135 optind = optreset = 1;
136 return cmd->fn(argc, argv);
139 static void
140 help(void) {
141 printf("\n");
142 printf("usage: %s [-hv] command [args]\n", __progname);
143 printf("where commands are:\n"
144 " crypt [password] slightly encrypt password\n"
145 " help command display help on \"command\"\n"
146 " lc display active connections\n"
147 " login //user@host[/share] login to the specified host\n"
148 " logout //user@host[/share] logout from the specified host\n"
149 " print //user@host/share file print file to the specified remote printer\n"
150 " view //user@host list resources on the specified host\n"
151 "\n");
152 exit(1);
155 static void
156 help_usage(void) {
157 printf("usage: smbutil help command\n");
158 exit(1);