2 * $Id: afp_options.c,v 1.30 2003-04-16 22:45:08 samnoble Exp $
4 * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
5 * Copyright (c) 1990,1993 Regents of The University of Michigan.
6 * All Rights Reserved. See COPYRIGHT.
8 * modified from main.c. this handles afp options.
13 #endif /* HAVE_CONFIG_H */
21 #else /* STDC_HEADERS */
25 #endif /* HAVE_STRCHR */
26 char *strchr (), *strrchr ();
28 #define memcpy(d,s,n) bcopy ((s), (d), (n))
29 #define memmove(d,s,n) bcopy ((s), (d), (n))
30 #endif /* ! HAVE_MEMCPY */
31 #endif /* STDC_HEADERS */
36 #endif /* HAVE_UNISTD_H */
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <atalk/logger.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
45 #endif /* HAVE_NETDB_H */
47 #include <atalk/paths.h>
48 #include <atalk/util.h>
53 #include <atalk/compat.h>
57 #include <sys/types.h>
58 #endif /* ADMIN_GRP */
61 #define MIN(a, b) ((a) < (b) ? (a) : (b))
64 #define OPTIONS "dn:f:s:uc:g:P:ptDS:TL:F:U:Ivm:"
67 /* return an option. this uses an internal array, so it's necessary
68 * to duplicate it if you want to hold it for long. this is probably
70 static char *getoption(char *buf, const char *option)
72 static char string[LENGTH + 1];
76 if (option && (buf = strstr(buf, option)))
77 buf = strpbrk(buf, " \t");
79 while (buf && isspace(*buf))
85 /* search for any quoted stuff */
86 if (*buf == '"' && (end = strchr(buf + 1, '"'))) {
88 len = MIN(end - buf, LENGTH);
89 } else if ((end = strpbrk(buf, " \t\n"))) /* option or eoln */
90 len = MIN(end - buf, LENGTH);
92 len = MIN(strlen(buf), LENGTH);
94 strncpy(string, buf, len);
99 /* get rid of any allocated afp_option buffers. */
100 void afp_options_free(struct afp_options *opt,
101 const struct afp_options *save)
103 if (opt->defaultvol && (opt->defaultvol != save->defaultvol))
104 free(opt->defaultvol);
105 if (opt->systemvol && (opt->systemvol != save->systemvol))
106 free(opt->systemvol);
107 if (opt->loginmesg && (opt->loginmesg != save->loginmesg))
108 free(opt->loginmesg);
109 if (opt->guest && (opt->guest != save->guest))
111 if (opt->server && (opt->server != save->server))
113 if (opt->ipaddr && (opt->ipaddr != save->ipaddr))
115 if (opt->fqdn && (opt->fqdn != save->fqdn))
117 if (opt->uampath && (opt->uampath != save->uampath))
119 if (opt->uamlist && (opt->uamlist != save->uamlist))
121 if (opt->nlspath && (opt->nlspath != save->nlspath))
123 if (opt->passwdfile && (opt->passwdfile != save->passwdfile))
124 free(opt->passwdfile);
125 if (opt->signature && (opt->signature != save->signature))
126 free(opt->signature);
127 if (opt->k5service && (opt->k5service != save->k5service))
128 free(opt->k5service);
129 if (opt->k5realm && (opt->k5realm != save->k5realm))
133 /* initialize options */
134 void afp_options_init(struct afp_options *options)
136 memset(options, 0, sizeof(struct afp_options));
137 options->connections = 20;
138 options->pidfile = _PATH_AFPDLOCK;
139 options->defaultvol = _PATH_AFPDDEFVOL;
140 options->systemvol = _PATH_AFPDSYSVOL;
141 options->configfile = _PATH_AFPDCONF;
142 options->nlspath = _PATH_AFPDNLSPATH;
143 options->uampath = _PATH_AFPDUAMPATH;
144 options->uamlist = "uams_clrtxt.so,uams_dhx.so";
145 options->guest = "nobody";
146 options->loginmesg = "";
147 options->transports = AFPTRANS_ALL;
148 options->passwdfile = _PATH_AFPDPWFILE;
149 options->tickleval = 30;
150 options->timeout = 4;
151 options->server_notif = 1;
152 options->authprintdir = NULL;
153 options->signature = "host";
156 options->admingid = 0;
157 #endif /* ADMIN_GRP */
158 options->k5service = NULL;
159 options->k5realm = NULL;
162 /* parse an afpd.conf line. i'm doing it this way because it's
163 * easy. it is, however, massively hokey. sample afpd.conf:
164 * server:AFPServer@zone -loginmesg "blah blah blah" -nodsi
165 * "private machine"@zone2 -noguest -port 11012
166 * server2 -nocleartxt -nodsi
168 * NOTE: this ignores unknown options
170 int afp_options_parseline(char *buf, struct afp_options *options)
175 if (*buf != '-' && (c = getoption(buf, NULL)) && (opt = strdup(c)))
176 options->server = opt;
179 if (strstr(buf, " -nodebug"))
180 options->flags &= ~OPTION_DEBUG;
182 if (strstr(buf, " -noslp"))
183 options->flags |= OPTION_NOSLP;
184 #endif /* USE_SRVLOC */
186 if (strstr(buf, " -nouservolfirst"))
187 options->flags &= ~OPTION_USERVOLFIRST;
188 if (strstr(buf, " -uservolfirst"))
189 options->flags |= OPTION_USERVOLFIRST;
190 if (strstr(buf, " -nouservol"))
191 options->flags |= OPTION_NOUSERVOL;
192 if (strstr(buf, " -uservol"))
193 options->flags &= ~OPTION_NOUSERVOL;
194 if (strstr(buf, " -proxy"))
195 options->flags |= OPTION_PROXY;
196 if (strstr(buf, " -noicon"))
197 options->flags &= ~OPTION_CUSTOMICON;
198 if (strstr(buf, " -icon"))
199 options->flags |= OPTION_CUSTOMICON;
202 if (strstr(buf, " -nosavepassword"))
203 options->passwdbits |= PASSWD_NOSAVE;
204 if (strstr(buf, " -savepassword"))
205 options->passwdbits &= ~PASSWD_NOSAVE;
206 if (strstr(buf, " -nosetpassword"))
207 options->passwdbits &= ~PASSWD_SET;
208 if (strstr(buf, " -setpassword"))
209 options->passwdbits |= PASSWD_SET;
212 if (strstr(buf, " -transall"))
213 options->transports = AFPTRANS_ALL;
214 if (strstr(buf, " -notransall"))
215 options->transports = AFPTRANS_NONE;
216 if (strstr(buf, " -tcp"))
217 options->transports |= AFPTRANS_TCP;
218 if (strstr(buf, " -notcp"))
219 options->transports &= ~AFPTRANS_TCP;
220 if (strstr(buf, " -ddp"))
221 options->transports |= AFPTRANS_DDP;
222 if (strstr(buf, " -noddp"))
223 options->transports &= ~AFPTRANS_DDP;
224 if (strstr(buf, "-client_polling"))
225 options->server_notif = 0;
227 /* figure out options w/ values. currently, this will ignore the setting
228 * if memory is lacking. */
229 if ((c = getoption(buf, "-defaultvol")) && (opt = strdup(c)))
230 options->defaultvol = opt;
231 if ((c = getoption(buf, "-systemvol")) && (opt = strdup(c)))
232 options->systemvol = opt;
233 if ((c = getoption(buf, "-loginmesg")) && (opt = strdup(c)))
234 options->loginmesg = opt;
235 if ((c = getoption(buf, "-guestname")) && (opt = strdup(c)))
236 options->guest = opt;
237 if ((c = getoption(buf, "-passwdfile")) && (opt = strdup(c)))
238 options->passwdfile = opt;
239 if ((c = getoption(buf, "-passwdminlen")))
240 options->passwdminlen = MIN(1, atoi(c));
241 if ((c = getoption(buf, "-loginmaxfail")))
242 options->loginmaxfail = atoi(c);
243 if ((c = getoption(buf, "-tickleval"))) {
244 options->tickleval = atoi(c);
245 if (options->tickleval <= 0) {
246 options->tickleval = 30;
249 if ((c = getoption(buf, "-timeout"))) {
250 options->timeout = atoi(c);
251 if (options->timeout <= 0) {
252 options->timeout = 4;
256 if ((c = getoption(buf, "-server_quantum")))
257 options->server_quantum = strtoul(c, NULL, 0);
259 #ifndef DISABLE_LOGGER
260 /* -setuplogtype <syslog|filelog> <logtype> <loglevel> <filename>*/
261 /* -[no]setuplog <logtype> <loglevel> [<filename>]*/
262 if ((c = getoption(buf, "-setuplog")))
264 char *ptr, *logsource, *logtype, *loglevel, *filename;
266 LOG(log_debug6, logtype_afpd, "setting up logtype, c is %s", c);
273 ptr = strpbrk(ptr, " \t");
277 while (*ptr && isspace(*ptr))
286 ptr = strpbrk(ptr, " \t");
290 while (*ptr && isspace(*ptr))
298 ptr = strpbrk(ptr, " \t");
302 while (*ptr && isspace(*ptr))
310 ptr = strpbrk(ptr, " \t");
314 while (*ptr && isspace(*ptr))
319 LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s",
320 logtype, loglevel, filename);
322 setuplog(logtype, loglevel, filename);
325 if ((c = getoption(buf, "-unsetuplog")))
327 char *ptr, *logtype, *loglevel, *filename;
329 LOG(log_debug6, logtype_afpd, "unsetting up logtype, c is %s", c);
335 ptr = strpbrk(ptr, " \t");
339 while (*ptr && isspace(*ptr))
347 ptr = strpbrk(ptr, " \t");
351 while (*ptr && isspace(*ptr))
359 ptr = strpbrk(ptr, " \t");
363 while (*ptr && isspace(*ptr))
368 LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s",
369 logtype, NULL, filename);
371 setuplog(logtype, NULL, filename);
373 #endif /* DISABLE_LOGGER */
375 if ((c = getoption(buf, "-admingroup"))) {
376 struct group *gr = getgrnam(c);
378 options->admingid = gr->gr_gid;
381 #endif /* ADMIN_GRP */
383 if ((c = getoption(buf, "-k5service")) && (opt = strdup(c)))
384 options->k5service = opt;
385 if ((c = getoption(buf, "-k5realm")) && (opt = strdup(c)))
386 options->k5realm = opt;
387 if ((c = getoption(buf, "-k5keytab")))
388 setenv( "KRB5_KTNAME", c, 1 );
389 if ((c = getoption(buf, "-authprintdir")) && (opt = strdup(c)))
390 options->authprintdir = opt;
391 if ((c = getoption(buf, "-uampath")) && (opt = strdup(c)))
392 options->uampath = opt;
393 if ((c = getoption(buf, "-uamlist")) && (opt = strdup(c)))
394 options->uamlist = opt;
395 if ((c = getoption(buf, "-nlspath")) && (opt = strdup(c)))
396 options->nlspath = opt;
398 if ((c = getoption(buf, "-ipaddr"))) {
399 struct in_addr inaddr;
400 if (inet_aton(c, &inaddr) && (opt = strdup(c))) {
401 if (!gethostbyaddr((const char *) &inaddr, sizeof(inaddr), AF_INET))
402 LOG(log_info, logtype_afpd, "WARNING: can't find %s\n", opt);
403 options->ipaddr = opt;
407 if ((c = getoption(buf, "-port")))
408 options->port = atoi(c);
409 if ((c = getoption(buf, "-ddpaddr")))
410 atalk_aton(c, &options->ddpaddr);
411 if ((c = getoption(buf, "-signature")) && (opt = strdup(c)))
412 options->signature = opt;
414 /* do a little checking for the domain name. */
415 if ((c = getoption(buf, "-fqdn"))) {
416 char *p = strchr(c, ':');
419 if (gethostbyname(c)) {
422 if ((opt = strdup(c)))
430 int afp_options_parse(int ac, char **av, struct afp_options *options)
436 char *tmp; /* Used for error checking the result of strtol */
439 if (gethostname(options->hostname, sizeof(options->hostname )) < 0 ) {
440 perror( "gethostname" );
443 if (NULL != ( p = strchr(options->hostname, '.' )) ) {
447 if (NULL == ( p = strrchr( av[ 0 ], '/' )) ) {
453 while (EOF != ( c = getopt( ac, av, OPTIONS )) ) {
456 options->flags |= OPTION_DEBUG;
459 options->server = optarg;
462 options->defaultvol = optarg;
465 options->systemvol = optarg;
468 options->flags |= OPTION_USERVOLFIRST;
471 options->connections = atoi( optarg );
474 options->guest = optarg;
478 options->pidfile = optarg;
482 options->passwdbits |= PASSWD_NOSAVE;
485 options->passwdbits |= PASSWD_SET;
489 options->transports &= ~AFPTRANS_DDP;
492 options->port = atoi(optarg);
495 options->transports &= ~AFPTRANS_TCP;
498 options->loginmesg = optarg;
501 options->configfile = optarg;
504 options->uamlist = optarg;
506 case 'v': /* version */
507 printf( "afpd (version %s)\n", VERSION );
511 options->flags |= OPTION_CUSTOMICON;
514 options->umask = strtoul(optarg, &tmp, 8);
515 if ((options->umask > 0777)) {
516 fprintf(stderr, "%s: out of range umask setting provided\n", p);
519 if (tmp[0] != '\0') {
520 fprintf(stderr, "%s: invalid characters in umask setting provided\n", p);
528 if ( err || optind != ac ) {
530 "Usage:\t%s [ -dpDTIt ] [ -n nbpname ] [ -f defvols ] \
531 [ -P pidfile ] [ -s sysvols ] \n", p );
533 "\t[ -u ] [ -c maxconn ] [ -g guest ] \
534 [ -S port ] [ -L loginmesg ] [ -F configfile ] [ -U uamlist ]\n" );
539 openlog( p, LOG_PID ); /* ultrix only */
542 syslog_setup(log_debug, logtype_default, logoption_ndelay|logoption_pid, logfacility_daemon);