2 * $Id: afp_options.c,v 1.30.2.2.2.1 2003-09-09 16:42:19 didg 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))
68 #define OPTIONS "dn:f:s:uc:g:P:ptDS:TL:F:U:Ivm:"
71 /* return an option. this uses an internal array, so it's necessary
72 * to duplicate it if you want to hold it for long. this is probably
74 static char *getoption(char *buf, const char *option)
76 static char string[LENGTH + 1];
80 if (option && (buf = strstr(buf, option)))
81 buf = strpbrk(buf, " \t");
83 while (buf && isspace(*buf))
89 /* search for any quoted stuff */
90 if (*buf == '"' && (end = strchr(buf + 1, '"'))) {
92 len = MIN(end - buf, LENGTH);
93 } else if ((end = strpbrk(buf, " \t\n"))) /* option or eoln */
94 len = MIN(end - buf, LENGTH);
96 len = MIN(strlen(buf), LENGTH);
98 strncpy(string, buf, len);
103 /* get rid of any allocated afp_option buffers. */
104 void afp_options_free(struct afp_options *opt,
105 const struct afp_options *save)
107 if (opt->defaultvol.name && (opt->defaultvol.name != save->defaultvol.name))
108 free(opt->defaultvol.name);
109 if (opt->defaultvol.full_name && (opt->defaultvol.full_name != save->defaultvol.full_name))
110 free(opt->defaultvol.full_name);
112 if (opt->systemvol.name && (opt->systemvol.name != save->systemvol.name))
113 free(opt->systemvol.name);
114 if (opt->systemvol.full_name && (opt->systemvol.full_name != save->systemvol.full_name))
115 free(opt->systemvol.full_name);
117 if (opt->uservol.name && (opt->uservol.name != save->uservol.name))
118 free(opt->uservol.name);
119 if (opt->uservol.full_name && (opt->uservol.full_name != save->uservol.full_name))
120 free(opt->uservol.full_name);
122 if (opt->loginmesg && (opt->loginmesg != save->loginmesg))
123 free(opt->loginmesg);
124 if (opt->guest && (opt->guest != save->guest))
126 if (opt->server && (opt->server != save->server))
128 if (opt->ipaddr && (opt->ipaddr != save->ipaddr))
130 if (opt->fqdn && (opt->fqdn != save->fqdn))
132 if (opt->uampath && (opt->uampath != save->uampath))
134 if (opt->uamlist && (opt->uamlist != save->uamlist))
136 if (opt->nlspath && (opt->nlspath != save->nlspath))
138 if (opt->passwdfile && (opt->passwdfile != save->passwdfile))
139 free(opt->passwdfile);
140 if (opt->signature && (opt->signature != save->signature))
141 free(opt->signature);
142 if (opt->k5service && (opt->k5service != save->k5service))
143 free(opt->k5service);
144 if (opt->k5realm && (opt->k5realm != save->k5realm))
146 if (opt->k5keytab && (opt->k5keytab != save->k5keytab))
148 if (opt->unixcodepage && (opt->unixcodepage != save->unixcodepage))
149 free(opt->unixcodepage);
150 if (opt->maccodepage && (opt->maccodepage != save->maccodepage))
151 free(opt->maccodepage);
154 /* initialize options */
155 void afp_options_init(struct afp_options *options)
157 memset(options, 0, sizeof(struct afp_options));
158 options->connections = 20;
159 options->pidfile = _PATH_AFPDLOCK;
160 options->defaultvol.name = _PATH_AFPDDEFVOL;
161 options->systemvol.name = _PATH_AFPDSYSVOL;
162 options->configfile = _PATH_AFPDCONF;
163 options->nlspath = _PATH_AFPDNLSPATH;
164 options->uampath = _PATH_AFPDUAMPATH;
165 options->uamlist = "uams_clrtxt.so,uams_dhx.so";
166 options->guest = "nobody";
167 options->loginmesg = "";
168 options->transports = AFPTRANS_ALL;
169 options->passwdfile = _PATH_AFPDPWFILE;
170 options->tickleval = 30;
171 options->timeout = 4;
172 options->sleep = 10* 120; /* 10 h in 30 seconds tick */
173 options->server_notif = 1;
174 options->authprintdir = NULL;
175 options->signature = "host";
178 options->admingid = 0;
179 #endif /* ADMIN_GRP */
180 options->k5service = NULL;
181 options->k5realm = NULL;
182 options->k5keytab = NULL;
183 options->unixcharset = CH_UNIX;
184 options->unixcodepage = "LOCALE";
185 options->maccharset = CH_MAC;
186 options->maccodepage = "MAC_ROMAN";
189 /* parse an afpd.conf line. i'm doing it this way because it's
190 * easy. it is, however, massively hokey. sample afpd.conf:
191 * server:AFPServer@zone -loginmesg "blah blah blah" -nodsi
192 * "private machine"@zone2 -noguest -port 11012
193 * server2 -nocleartxt -nodsi
195 * NOTE: this ignores unknown options
197 int afp_options_parseline(char *buf, struct afp_options *options)
202 if (*buf != '-' && (c = getoption(buf, NULL)) && (opt = strdup(c)))
203 options->server = opt;
206 if (strstr(buf, " -nodebug"))
207 options->flags &= ~OPTION_DEBUG;
209 if (strstr(buf, " -noslp"))
210 options->flags |= OPTION_NOSLP;
211 #endif /* USE_SRVLOC */
213 if (strstr(buf, " -nouservolfirst"))
214 options->flags &= ~OPTION_USERVOLFIRST;
215 if (strstr(buf, " -uservolfirst"))
216 options->flags |= OPTION_USERVOLFIRST;
217 if (strstr(buf, " -nouservol"))
218 options->flags |= OPTION_NOUSERVOL;
219 if (strstr(buf, " -uservol"))
220 options->flags &= ~OPTION_NOUSERVOL;
221 if (strstr(buf, " -proxy"))
222 options->flags |= OPTION_PROXY;
223 if (strstr(buf, " -noicon"))
224 options->flags &= ~OPTION_CUSTOMICON;
225 if (strstr(buf, " -icon"))
226 options->flags |= OPTION_CUSTOMICON;
229 if (strstr(buf, " -nosavepassword"))
230 options->passwdbits |= PASSWD_NOSAVE;
231 if (strstr(buf, " -savepassword"))
232 options->passwdbits &= ~PASSWD_NOSAVE;
233 if (strstr(buf, " -nosetpassword"))
234 options->passwdbits &= ~PASSWD_SET;
235 if (strstr(buf, " -setpassword"))
236 options->passwdbits |= PASSWD_SET;
239 if (strstr(buf, " -transall"))
240 options->transports = AFPTRANS_ALL;
241 if (strstr(buf, " -notransall"))
242 options->transports = AFPTRANS_NONE;
243 if (strstr(buf, " -tcp"))
244 options->transports |= AFPTRANS_TCP;
245 if (strstr(buf, " -notcp"))
246 options->transports &= ~AFPTRANS_TCP;
247 if (strstr(buf, " -ddp"))
248 options->transports |= AFPTRANS_DDP;
249 if (strstr(buf, " -noddp"))
250 options->transports &= ~AFPTRANS_DDP;
251 if (strstr(buf, "-client_polling"))
252 options->server_notif = 0;
254 /* figure out options w/ values. currently, this will ignore the setting
255 * if memory is lacking. */
256 if ((c = getoption(buf, "-defaultvol")) && (opt = strdup(c)))
257 options->defaultvol.name = opt;
258 if ((c = getoption(buf, "-systemvol")) && (opt = strdup(c)))
259 options->systemvol.name = opt;
260 if ((c = getoption(buf, "-loginmesg")) && (opt = strdup(c)))
261 options->loginmesg = opt;
262 if ((c = getoption(buf, "-guestname")) && (opt = strdup(c)))
263 options->guest = opt;
264 if ((c = getoption(buf, "-passwdfile")) && (opt = strdup(c)))
265 options->passwdfile = opt;
266 if ((c = getoption(buf, "-passwdminlen")))
267 options->passwdminlen = MIN(1, atoi(c));
268 if ((c = getoption(buf, "-loginmaxfail")))
269 options->loginmaxfail = atoi(c);
270 if ((c = getoption(buf, "-tickleval"))) {
271 options->tickleval = atoi(c);
272 if (options->tickleval <= 0) {
273 options->tickleval = 30;
276 if ((c = getoption(buf, "-timeout"))) {
277 options->timeout = atoi(c);
278 if (options->timeout <= 0) {
279 options->timeout = 4;
283 if ((c = getoption(buf, "-sleep"))) {
284 options->sleep = atoi(c) *120;
285 if (options->sleep <= 4) {
290 if ((c = getoption(buf, "-server_quantum")))
291 options->server_quantum = strtoul(c, NULL, 0);
293 #ifndef DISABLE_LOGGER
294 /* -setuplogtype <syslog|filelog> <logtype> <loglevel> <filename>*/
295 /* -[no]setuplog <logtype> <loglevel> [<filename>]*/
296 if ((c = getoption(buf, "-setuplog")))
298 char *ptr, *logsource, *logtype, *loglevel, *filename;
300 LOG(log_debug6, logtype_afpd, "setting up logtype, c is %s", c);
307 ptr = strpbrk(ptr, " \t");
311 while (*ptr && isspace(*ptr))
320 ptr = strpbrk(ptr, " \t");
324 while (*ptr && isspace(*ptr))
332 ptr = strpbrk(ptr, " \t");
336 while (*ptr && isspace(*ptr))
344 ptr = strpbrk(ptr, " \t");
348 while (*ptr && isspace(*ptr))
353 LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s",
354 logtype, loglevel, filename);
356 setuplog(logtype, loglevel, filename);
359 if ((c = getoption(buf, "-unsetuplog")))
361 char *ptr, *logtype, *loglevel, *filename;
363 LOG(log_debug6, logtype_afpd, "unsetting up logtype, c is %s", c);
369 ptr = strpbrk(ptr, " \t");
373 while (*ptr && isspace(*ptr))
381 ptr = strpbrk(ptr, " \t");
385 while (*ptr && isspace(*ptr))
393 ptr = strpbrk(ptr, " \t");
397 while (*ptr && isspace(*ptr))
402 LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s",
403 logtype, NULL, filename);
405 setuplog(logtype, NULL, filename);
407 #endif /* DISABLE_LOGGER */
409 if ((c = getoption(buf, "-admingroup"))) {
410 struct group *gr = getgrnam(c);
412 options->admingid = gr->gr_gid;
415 #endif /* ADMIN_GRP */
417 if ((c = getoption(buf, "-k5service")) && (opt = strdup(c)))
418 options->k5service = opt;
419 if ((c = getoption(buf, "-k5realm")) && (opt = strdup(c)))
420 options->k5realm = opt;
421 if ((c = getoption(buf, "-k5keytab"))) {
422 if ( NULL == (options->k5keytab = (char *) malloc(sizeof(char)*(strlen(c)+14)) )) {
423 LOG(log_error, logtype_afpd, "malloc failed");
426 snprintf(options->k5keytab, strlen(c)+14, "KRB5_KTNAME=%s", c);
427 putenv(options->k5keytab);
428 /* setenv( "KRB5_KTNAME", c, 1 ); */
430 if ((c = getoption(buf, "-authprintdir")) && (opt = strdup(c)))
431 options->authprintdir = opt;
432 if ((c = getoption(buf, "-uampath")) && (opt = strdup(c)))
433 options->uampath = opt;
434 if ((c = getoption(buf, "-uamlist")) && (opt = strdup(c)))
435 options->uamlist = opt;
436 if ((c = getoption(buf, "-nlspath")) && (opt = strdup(c)))
437 options->nlspath = opt;
439 if ((c = getoption(buf, "-ipaddr"))) {
440 struct in_addr inaddr;
441 if (inet_aton(c, &inaddr) && (opt = strdup(c))) {
442 if (!gethostbyaddr((const char *) &inaddr, sizeof(inaddr), AF_INET))
443 LOG(log_info, logtype_afpd, "WARNING: can't find %s\n", opt);
444 options->ipaddr = opt;
449 if ((c = getoption(buf, "-cnidserver"))) {
452 Cnid_srv = strdup(c);
453 p = strchr(Cnid_srv, ':');
455 Cnid_port = atoi(p +1);
458 if ((c = getoption(buf, "-port")))
459 options->port = atoi(c);
460 if ((c = getoption(buf, "-ddpaddr")))
461 atalk_aton(c, &options->ddpaddr);
462 if ((c = getoption(buf, "-signature")) && (opt = strdup(c)))
463 options->signature = opt;
465 /* do a little checking for the domain name. */
466 if ((c = getoption(buf, "-fqdn"))) {
467 char *p = strchr(c, ':');
470 if (gethostbyname(c)) {
473 if ((opt = strdup(c)))
478 if ((c = getoption(buf, "-unixcodepage"))) {
479 options->unixcodepage = c;
480 if ( 0 == ( options->unixcharset = add_charset(options->unixcodepage)) ) {
481 options->unixcodepage= "LOCALE";
482 options->unixcharset = CH_UNIX;
486 if ((c = getoption(buf, "-maccodepage"))) {
487 options->maccodepage = c;
488 if ( 0 == ( options->maccharset = add_charset(options->maccodepage)) ) {
489 options->maccodepage= "Mac_Roman";
490 options->maccharset = CH_MAC;
492 LOG(log_debug, logtype_afpd, "Setting Mac Codepage to '%s'", options->maccodepage);
498 int afp_options_parse(int ac, char **av, struct afp_options *options)
504 char *tmp; /* Used for error checking the result of strtol */
507 if (gethostname(options->hostname, sizeof(options->hostname )) < 0 ) {
508 perror( "gethostname" );
511 if (NULL != ( p = strchr(options->hostname, '.' )) ) {
515 if (NULL == ( p = strrchr( av[ 0 ], '/' )) ) {
521 while (EOF != ( c = getopt( ac, av, OPTIONS )) ) {
524 options->flags |= OPTION_DEBUG;
527 options->server = optarg;
530 options->defaultvol.name = optarg;
533 options->systemvol.name = optarg;
536 options->flags |= OPTION_USERVOLFIRST;
539 options->connections = atoi( optarg );
542 options->guest = optarg;
546 options->pidfile = optarg;
550 options->passwdbits |= PASSWD_NOSAVE;
553 options->passwdbits |= PASSWD_SET;
557 options->transports &= ~AFPTRANS_DDP;
560 options->port = atoi(optarg);
563 options->transports &= ~AFPTRANS_TCP;
566 options->loginmesg = optarg;
569 options->configfile = optarg;
572 options->uamlist = optarg;
574 case 'v': /* version */
575 printf( "afpd (version %s)\n", VERSION );
579 options->flags |= OPTION_CUSTOMICON;
582 options->umask = strtoul(optarg, &tmp, 8);
583 if ((options->umask > 0777)) {
584 fprintf(stderr, "%s: out of range umask setting provided\n", p);
587 if (tmp[0] != '\0') {
588 fprintf(stderr, "%s: invalid characters in umask setting provided\n", p);
596 if ( err || optind != ac ) {
598 "Usage:\t%s [ -dpDTIt ] [ -n nbpname ] [ -f defvols ] \
599 [ -P pidfile ] [ -s sysvols ] \n", p );
601 "\t[ -u ] [ -c maxconn ] [ -g guest ] \
602 [ -S port ] [ -L loginmesg ] [ -F configfile ] [ -U uamlist ]\n" );
607 openlog( p, LOG_PID ); /* ultrix only */
610 syslog_setup(log_debug, logtype_default, logoption_ndelay|logoption_pid, logfacility_daemon);