2 * $Id: afp_options.c,v 1.32 2003-06-09 15:09:19 srittau 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->sleep = 10* 120; /* 10 h in 30 seconds tick */
152 options->server_notif = 1;
153 options->authprintdir = NULL;
154 options->signature = "host";
157 options->admingid = 0;
158 #endif /* ADMIN_GRP */
159 options->k5service = NULL;
160 options->k5realm = NULL;
163 /* parse an afpd.conf line. i'm doing it this way because it's
164 * easy. it is, however, massively hokey. sample afpd.conf:
165 * server:AFPServer@zone -loginmesg "blah blah blah" -nodsi
166 * "private machine"@zone2 -noguest -port 11012
167 * server2 -nocleartxt -nodsi
169 * NOTE: this ignores unknown options
171 int afp_options_parseline(char *buf, struct afp_options *options)
176 if (*buf != '-' && (c = getoption(buf, NULL)) && (opt = strdup(c)))
177 options->server = opt;
180 if (strstr(buf, " -nodebug"))
181 options->flags &= ~OPTION_DEBUG;
183 if (strstr(buf, " -noslp"))
184 options->flags |= OPTION_NOSLP;
185 #endif /* USE_SRVLOC */
187 if (strstr(buf, " -nouservolfirst"))
188 options->flags &= ~OPTION_USERVOLFIRST;
189 if (strstr(buf, " -uservolfirst"))
190 options->flags |= OPTION_USERVOLFIRST;
191 if (strstr(buf, " -nouservol"))
192 options->flags |= OPTION_NOUSERVOL;
193 if (strstr(buf, " -uservol"))
194 options->flags &= ~OPTION_NOUSERVOL;
195 if (strstr(buf, " -proxy"))
196 options->flags |= OPTION_PROXY;
197 if (strstr(buf, " -noicon"))
198 options->flags &= ~OPTION_CUSTOMICON;
199 if (strstr(buf, " -icon"))
200 options->flags |= OPTION_CUSTOMICON;
203 if (strstr(buf, " -nosavepassword"))
204 options->passwdbits |= PASSWD_NOSAVE;
205 if (strstr(buf, " -savepassword"))
206 options->passwdbits &= ~PASSWD_NOSAVE;
207 if (strstr(buf, " -nosetpassword"))
208 options->passwdbits &= ~PASSWD_SET;
209 if (strstr(buf, " -setpassword"))
210 options->passwdbits |= PASSWD_SET;
213 if (strstr(buf, " -transall"))
214 options->transports = AFPTRANS_ALL;
215 if (strstr(buf, " -notransall"))
216 options->transports = AFPTRANS_NONE;
217 if (strstr(buf, " -tcp"))
218 options->transports |= AFPTRANS_TCP;
219 if (strstr(buf, " -notcp"))
220 options->transports &= ~AFPTRANS_TCP;
221 if (strstr(buf, " -ddp"))
222 options->transports |= AFPTRANS_DDP;
223 if (strstr(buf, " -noddp"))
224 options->transports &= ~AFPTRANS_DDP;
225 if (strstr(buf, "-client_polling"))
226 options->server_notif = 0;
228 /* figure out options w/ values. currently, this will ignore the setting
229 * if memory is lacking. */
230 if ((c = getoption(buf, "-defaultvol")) && (opt = strdup(c)))
231 options->defaultvol = opt;
232 if ((c = getoption(buf, "-systemvol")) && (opt = strdup(c)))
233 options->systemvol = opt;
234 if ((c = getoption(buf, "-loginmesg")) && (opt = strdup(c)))
235 options->loginmesg = opt;
236 if ((c = getoption(buf, "-guestname")) && (opt = strdup(c)))
237 options->guest = opt;
238 if ((c = getoption(buf, "-passwdfile")) && (opt = strdup(c)))
239 options->passwdfile = opt;
240 if ((c = getoption(buf, "-passwdminlen")))
241 options->passwdminlen = MIN(1, atoi(c));
242 if ((c = getoption(buf, "-loginmaxfail")))
243 options->loginmaxfail = atoi(c);
244 if ((c = getoption(buf, "-tickleval"))) {
245 options->tickleval = atoi(c);
246 if (options->tickleval <= 0) {
247 options->tickleval = 30;
250 if ((c = getoption(buf, "-timeout"))) {
251 options->timeout = atoi(c);
252 if (options->timeout <= 0) {
253 options->timeout = 4;
257 if ((c = getoption(buf, "-sleep"))) {
258 options->sleep = atoi(c) *120;
259 if (options->sleep <= 4) {
264 if ((c = getoption(buf, "-server_quantum")))
265 options->server_quantum = strtoul(c, NULL, 0);
267 #ifndef DISABLE_LOGGER
268 /* -setuplogtype <syslog|filelog> <logtype> <loglevel> <filename>*/
269 /* -[no]setuplog <logtype> <loglevel> [<filename>]*/
270 if ((c = getoption(buf, "-setuplog")))
272 char *ptr, *logtype, *loglevel, *filename;
277 LOG(log_debug6, logtype_afpd, "setting up logtype, c is %s", c);
284 ptr = strpbrk(ptr, " \t");
288 while (*ptr && isspace(*ptr))
297 ptr = strpbrk(ptr, " \t");
301 while (*ptr && isspace(*ptr))
309 ptr = strpbrk(ptr, " \t");
313 while (*ptr && isspace(*ptr))
321 ptr = strpbrk(ptr, " \t");
325 while (*ptr && isspace(*ptr))
330 LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s",
331 logtype, loglevel, filename);
333 setuplog(logtype, loglevel, filename);
336 if ((c = getoption(buf, "-unsetuplog")))
338 char *ptr, *logtype, *loglevel, *filename;
340 LOG(log_debug6, logtype_afpd, "unsetting up logtype, c is %s", c);
346 ptr = strpbrk(ptr, " \t");
350 while (*ptr && isspace(*ptr))
358 ptr = strpbrk(ptr, " \t");
362 while (*ptr && isspace(*ptr))
370 ptr = strpbrk(ptr, " \t");
374 while (*ptr && isspace(*ptr))
379 LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s",
380 logtype, NULL, filename);
382 setuplog(logtype, NULL, filename);
384 #endif /* DISABLE_LOGGER */
386 if ((c = getoption(buf, "-admingroup"))) {
387 struct group *gr = getgrnam(c);
389 options->admingid = gr->gr_gid;
392 #endif /* ADMIN_GRP */
394 if ((c = getoption(buf, "-k5service")) && (opt = strdup(c)))
395 options->k5service = opt;
396 if ((c = getoption(buf, "-k5realm")) && (opt = strdup(c)))
397 options->k5realm = opt;
398 if ((c = getoption(buf, "-k5keytab")))
399 setenv( "KRB5_KTNAME", c, 1 );
400 if ((c = getoption(buf, "-authprintdir")) && (opt = strdup(c)))
401 options->authprintdir = opt;
402 if ((c = getoption(buf, "-uampath")) && (opt = strdup(c)))
403 options->uampath = opt;
404 if ((c = getoption(buf, "-uamlist")) && (opt = strdup(c)))
405 options->uamlist = opt;
406 if ((c = getoption(buf, "-nlspath")) && (opt = strdup(c)))
407 options->nlspath = opt;
409 if ((c = getoption(buf, "-ipaddr"))) {
410 struct in_addr inaddr;
411 if (inet_aton(c, &inaddr) && (opt = strdup(c))) {
412 if (!gethostbyaddr((const char *) &inaddr, sizeof(inaddr), AF_INET))
413 LOG(log_info, logtype_afpd, "WARNING: can't find %s\n", opt);
414 options->ipaddr = opt;
418 if ((c = getoption(buf, "-port")))
419 options->port = atoi(c);
420 if ((c = getoption(buf, "-ddpaddr")))
421 atalk_aton(c, &options->ddpaddr);
422 if ((c = getoption(buf, "-signature")) && (opt = strdup(c)))
423 options->signature = opt;
425 /* do a little checking for the domain name. */
426 if ((c = getoption(buf, "-fqdn"))) {
427 char *p = strchr(c, ':');
430 if (gethostbyname(c)) {
433 if ((opt = strdup(c)))
441 int afp_options_parse(int ac, char **av, struct afp_options *options)
447 char *tmp; /* Used for error checking the result of strtol */
450 if (gethostname(options->hostname, sizeof(options->hostname )) < 0 ) {
451 perror( "gethostname" );
454 if (NULL != ( p = strchr(options->hostname, '.' )) ) {
458 if (NULL == ( p = strrchr( av[ 0 ], '/' )) ) {
464 while (EOF != ( c = getopt( ac, av, OPTIONS )) ) {
467 options->flags |= OPTION_DEBUG;
470 options->server = optarg;
473 options->defaultvol = optarg;
476 options->systemvol = optarg;
479 options->flags |= OPTION_USERVOLFIRST;
482 options->connections = atoi( optarg );
485 options->guest = optarg;
489 options->pidfile = optarg;
493 options->passwdbits |= PASSWD_NOSAVE;
496 options->passwdbits |= PASSWD_SET;
500 options->transports &= ~AFPTRANS_DDP;
503 options->port = atoi(optarg);
506 options->transports &= ~AFPTRANS_TCP;
509 options->loginmesg = optarg;
512 options->configfile = optarg;
515 options->uamlist = optarg;
517 case 'v': /* version */
518 printf( "afpd (version %s)\n", VERSION );
522 options->flags |= OPTION_CUSTOMICON;
525 options->umask = strtoul(optarg, &tmp, 8);
526 if ((options->umask > 0777)) {
527 fprintf(stderr, "%s: out of range umask setting provided\n", p);
530 if (tmp[0] != '\0') {
531 fprintf(stderr, "%s: invalid characters in umask setting provided\n", p);
539 if ( err || optind != ac ) {
541 "Usage:\t%s [ -dpDTIt ] [ -n nbpname ] [ -f defvols ] \
542 [ -P pidfile ] [ -s sysvols ] \n", p );
544 "\t[ -u ] [ -c maxconn ] [ -g guest ] \
545 [ -S port ] [ -L loginmesg ] [ -F configfile ] [ -U uamlist ]\n" );
550 openlog( p, LOG_PID ); /* ultrix only */
553 syslog_setup(log_debug, logtype_default, logoption_ndelay|logoption_pid, logfacility_daemon);