2 * $Id: afp_options.c,v 1.30.2.2.2.6 2004-01-25 11:52:14 bfernhomberg 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))
65 char Cnid_srv[MAXHOSTNAMELEN + 1] = "localhost";
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->passwdfile && (opt->passwdfile != save->passwdfile))
137 free(opt->passwdfile);
138 if (opt->signature && (opt->signature != save->signature))
139 free(opt->signature);
140 if (opt->k5service && (opt->k5service != save->k5service))
141 free(opt->k5service);
142 if (opt->k5realm && (opt->k5realm != save->k5realm))
144 if (opt->k5keytab && (opt->k5keytab != save->k5keytab))
146 if (opt->unixcodepage && (opt->unixcodepage != save->unixcodepage))
147 free(opt->unixcodepage);
148 if (opt->maccodepage && (opt->maccodepage != save->maccodepage))
149 free(opt->maccodepage);
152 /* initialize options */
153 void afp_options_init(struct afp_options *options)
155 memset(options, 0, sizeof(struct afp_options));
156 options->connections = 20;
157 options->pidfile = _PATH_AFPDLOCK;
158 options->defaultvol.name = _PATH_AFPDDEFVOL;
159 options->systemvol.name = _PATH_AFPDSYSVOL;
160 options->configfile = _PATH_AFPDCONF;
161 options->uampath = _PATH_AFPDUAMPATH;
162 options->uamlist = "uams_clrtxt.so,uams_dhx.so";
163 options->guest = "nobody";
164 options->loginmesg = "";
165 options->transports = AFPTRANS_ALL;
166 options->passwdfile = _PATH_AFPDPWFILE;
167 options->tickleval = 30;
168 options->timeout = 4;
169 options->sleep = 10* 120; /* 10 h in 30 seconds tick */
170 options->server_notif = 1;
171 options->authprintdir = NULL;
172 options->signature = "host";
175 options->admingid = 0;
176 #endif /* ADMIN_GRP */
177 options->k5service = NULL;
178 options->k5realm = NULL;
179 options->k5keytab = NULL;
180 options->unixcharset = CH_UNIX;
181 options->unixcodepage = "LOCALE";
182 options->maccharset = CH_MAC;
183 options->maccodepage = "MAC_ROMAN";
186 /* parse an afpd.conf line. i'm doing it this way because it's
187 * easy. it is, however, massively hokey. sample afpd.conf:
188 * server:AFPServer@zone -loginmesg "blah blah blah" -nodsi
189 * "private machine"@zone2 -noguest -port 11012
190 * server2 -nocleartxt -nodsi
192 * NOTE: this ignores unknown options
194 int afp_options_parseline(char *buf, struct afp_options *options)
199 if (*buf != '-' && (c = getoption(buf, NULL)) && (opt = strdup(c)))
200 options->server = opt;
203 if (strstr(buf, " -nodebug"))
204 options->flags &= ~OPTION_DEBUG;
206 if (strstr(buf, " -noslp"))
207 options->flags |= OPTION_NOSLP;
208 #endif /* USE_SRVLOC */
210 if (strstr(buf, " -nouservolfirst"))
211 options->flags &= ~OPTION_USERVOLFIRST;
212 if (strstr(buf, " -uservolfirst"))
213 options->flags |= OPTION_USERVOLFIRST;
214 if (strstr(buf, " -nouservol"))
215 options->flags |= OPTION_NOUSERVOL;
216 if (strstr(buf, " -uservol"))
217 options->flags &= ~OPTION_NOUSERVOL;
218 if (strstr(buf, " -proxy"))
219 options->flags |= OPTION_PROXY;
220 if (strstr(buf, " -noicon"))
221 options->flags &= ~OPTION_CUSTOMICON;
222 if (strstr(buf, " -icon"))
223 options->flags |= OPTION_CUSTOMICON;
226 if (strstr(buf, " -nosavepassword"))
227 options->passwdbits |= PASSWD_NOSAVE;
228 if (strstr(buf, " -savepassword"))
229 options->passwdbits &= ~PASSWD_NOSAVE;
230 if (strstr(buf, " -nosetpassword"))
231 options->passwdbits &= ~PASSWD_SET;
232 if (strstr(buf, " -setpassword"))
233 options->passwdbits |= PASSWD_SET;
236 if (strstr(buf, " -transall"))
237 options->transports = AFPTRANS_ALL;
238 if (strstr(buf, " -notransall"))
239 options->transports = AFPTRANS_NONE;
240 if (strstr(buf, " -tcp"))
241 options->transports |= AFPTRANS_TCP;
242 if (strstr(buf, " -notcp"))
243 options->transports &= ~AFPTRANS_TCP;
244 if (strstr(buf, " -ddp"))
245 options->transports |= AFPTRANS_DDP;
246 if (strstr(buf, " -noddp"))
247 options->transports &= ~AFPTRANS_DDP;
248 if (strstr(buf, "-client_polling"))
249 options->server_notif = 0;
251 /* figure out options w/ values. currently, this will ignore the setting
252 * if memory is lacking. */
253 if ((c = getoption(buf, "-defaultvol")) && (opt = strdup(c)))
254 options->defaultvol.name = opt;
255 if ((c = getoption(buf, "-systemvol")) && (opt = strdup(c)))
256 options->systemvol.name = opt;
257 if ((c = getoption(buf, "-loginmesg")) && (opt = strdup(c)))
258 options->loginmesg = opt;
259 if ((c = getoption(buf, "-guestname")) && (opt = strdup(c)))
260 options->guest = opt;
261 if ((c = getoption(buf, "-passwdfile")) && (opt = strdup(c)))
262 options->passwdfile = opt;
263 if ((c = getoption(buf, "-passwdminlen")))
264 options->passwdminlen = MIN(1, atoi(c));
265 if ((c = getoption(buf, "-loginmaxfail")))
266 options->loginmaxfail = atoi(c);
267 if ((c = getoption(buf, "-tickleval"))) {
268 options->tickleval = atoi(c);
269 if (options->tickleval <= 0) {
270 options->tickleval = 30;
273 if ((c = getoption(buf, "-timeout"))) {
274 options->timeout = atoi(c);
275 if (options->timeout <= 0) {
276 options->timeout = 4;
280 if ((c = getoption(buf, "-sleep"))) {
281 options->sleep = atoi(c) *120;
282 if (options->sleep <= 4) {
287 if ((c = getoption(buf, "-server_quantum")))
288 options->server_quantum = strtoul(c, NULL, 0);
290 #ifndef DISABLE_LOGGER
291 /* -setuplogtype <syslog|filelog> <logtype> <loglevel> <filename>*/
292 /* -[no]setuplog <logtype> <loglevel> [<filename>]*/
293 if ((c = getoption(buf, "-setuplog")))
295 char *ptr, *logsource, *logtype, *loglevel, *filename;
297 LOG(log_debug6, logtype_afpd, "setting up logtype, c is %s", c);
304 ptr = strpbrk(ptr, " \t");
308 while (*ptr && isspace(*ptr))
317 ptr = strpbrk(ptr, " \t");
321 while (*ptr && isspace(*ptr))
329 ptr = strpbrk(ptr, " \t");
333 while (*ptr && isspace(*ptr))
341 ptr = strpbrk(ptr, " \t");
345 while (*ptr && isspace(*ptr))
350 LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s",
351 logtype, loglevel, filename);
353 setuplog(logtype, loglevel, filename);
356 if ((c = getoption(buf, "-unsetuplog")))
358 char *ptr, *logtype, *loglevel, *filename;
360 LOG(log_debug6, logtype_afpd, "unsetting up logtype, c is %s", c);
366 ptr = strpbrk(ptr, " \t");
370 while (*ptr && isspace(*ptr))
378 ptr = strpbrk(ptr, " \t");
382 while (*ptr && isspace(*ptr))
390 ptr = strpbrk(ptr, " \t");
394 while (*ptr && isspace(*ptr))
399 LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s",
400 logtype, NULL, filename);
402 setuplog(logtype, NULL, filename);
404 #endif /* DISABLE_LOGGER */
406 if ((c = getoption(buf, "-admingroup"))) {
407 struct group *gr = getgrnam(c);
409 options->admingid = gr->gr_gid;
412 #endif /* ADMIN_GRP */
414 if ((c = getoption(buf, "-k5service")) && (opt = strdup(c)))
415 options->k5service = opt;
416 if ((c = getoption(buf, "-k5realm")) && (opt = strdup(c)))
417 options->k5realm = opt;
418 if ((c = getoption(buf, "-k5keytab"))) {
419 if ( NULL == (options->k5keytab = (char *) malloc(sizeof(char)*(strlen(c)+14)) )) {
420 LOG(log_error, logtype_afpd, "malloc failed");
423 snprintf(options->k5keytab, strlen(c)+14, "KRB5_KTNAME=%s", c);
424 putenv(options->k5keytab);
425 /* setenv( "KRB5_KTNAME", c, 1 ); */
427 if ((c = getoption(buf, "-authprintdir")) && (opt = strdup(c)))
428 options->authprintdir = opt;
429 if ((c = getoption(buf, "-uampath")) && (opt = strdup(c)))
430 options->uampath = opt;
431 if ((c = getoption(buf, "-uamlist")) && (opt = strdup(c)))
432 options->uamlist = opt;
434 if ((c = getoption(buf, "-ipaddr"))) {
435 struct in_addr inaddr;
436 if (inet_aton(c, &inaddr) && (opt = strdup(c))) {
437 if (!gethostbyaddr((const char *) &inaddr, sizeof(inaddr), AF_INET))
438 LOG(log_info, logtype_afpd, "WARNING: can't find %s\n", opt);
439 options->ipaddr = opt;
443 /* FIXME CNID Cnid_srv is a server attribute */
444 if ((c = getoption(buf, "-cnidserver"))) {
448 if (p != NULL && (len = p - c) <= MAXHOSTNAMELEN) {
449 memcpy(Cnid_srv, c, len);
451 Cnid_port = atoi(p +1);
455 if ((c = getoption(buf, "-port")))
456 options->port = atoi(c);
457 if ((c = getoption(buf, "-ddpaddr")))
458 atalk_aton(c, &options->ddpaddr);
459 if ((c = getoption(buf, "-signature")) && (opt = strdup(c)))
460 options->signature = opt;
462 /* do a little checking for the domain name. */
463 if ((c = getoption(buf, "-fqdn"))) {
464 char *p = strchr(c, ':');
467 if (gethostbyname(c)) {
470 if ((opt = strdup(c)))
474 LOG(log_error, logtype_afpd, "error parsing -fqdn, gethostbyname failed for: %s", c);
478 if ((c = getoption(buf, "-unixcodepage"))) {
479 if ((charset_t)-1 == ( options->unixcharset = add_charset(c)) ) {
480 options->unixcharset = CH_UNIX;
481 LOG(log_warning, logtype_afpd, "setting Unix codepage to '%s' failed", c);
484 if ((opt = strdup(c)))
485 options->unixcodepage = opt;
489 if ((c = getoption(buf, "-maccodepage"))) {
490 if ((charset_t)-1 == ( options->maccharset = add_charset(c)) ) {
491 options->maccharset = CH_MAC;
492 LOG(log_warning, logtype_afpd, "setting Mac codepage to '%s' failed", c);
495 if ((opt = strdup(c)))
496 options->maccodepage = opt;
503 int afp_options_parse(int ac, char **av, struct afp_options *options)
509 char *tmp; /* Used for error checking the result of strtol */
512 if (gethostname(options->hostname, sizeof(options->hostname )) < 0 ) {
513 perror( "gethostname" );
516 if (NULL != ( p = strchr(options->hostname, '.' )) ) {
520 if (NULL == ( p = strrchr( av[ 0 ], '/' )) ) {
526 while (EOF != ( c = getopt( ac, av, OPTIONS )) ) {
529 options->flags |= OPTION_DEBUG;
532 options->server = optarg;
535 options->defaultvol.name = optarg;
538 options->systemvol.name = optarg;
541 options->flags |= OPTION_USERVOLFIRST;
544 options->connections = atoi( optarg );
547 options->guest = optarg;
551 options->pidfile = optarg;
555 options->passwdbits |= PASSWD_NOSAVE;
558 options->passwdbits |= PASSWD_SET;
562 options->transports &= ~AFPTRANS_DDP;
565 options->port = atoi(optarg);
568 options->transports &= ~AFPTRANS_TCP;
571 options->loginmesg = optarg;
574 options->configfile = optarg;
577 options->uamlist = optarg;
579 case 'v': /* version */
580 printf( "afpd (version %s)\n", VERSION );
584 options->flags |= OPTION_CUSTOMICON;
587 options->umask = strtoul(optarg, &tmp, 8);
588 if ((options->umask > 0777)) {
589 fprintf(stderr, "%s: out of range umask setting provided\n", p);
592 if (tmp[0] != '\0') {
593 fprintf(stderr, "%s: invalid characters in umask setting provided\n", p);
601 if ( err || optind != ac ) {
603 "Usage:\t%s [ -dpDTIt ] [ -n nbpname ] [ -f defvols ] \
604 [ -P pidfile ] [ -s sysvols ] \n", p );
606 "\t[ -u ] [ -c maxconn ] [ -g guest ] \
607 [ -S port ] [ -L loginmesg ] [ -F configfile ] [ -U uamlist ]\n" );
612 openlog( p, LOG_PID ); /* ultrix only */
615 syslog_setup(log_debug, logtype_default, logoption_ndelay|logoption_pid, logfacility_daemon);