]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_options.c
Solaris compatibility fix, use putenv() for KRB5_KEYTAB rather than setenv().
[netatalk.git] / etc / afpd / afp_options.c
1 /*
2  * $Id: afp_options.c,v 1.33 2003-08-22 20:41:09 samnoble Exp $
3  *
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.
7  *
8  * modified from main.c. this handles afp options.
9  */
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif /* HAVE_CONFIG_H */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 /* STDC check */
19 #if STDC_HEADERS
20 #include <string.h>
21 #else /* STDC_HEADERS */
22 #ifndef HAVE_STRCHR
23 #define strchr index
24 #define strrchr index
25 #endif /* HAVE_STRCHR */
26 char *strchr (), *strrchr ();
27 #ifndef HAVE_MEMCPY
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 */
32
33 #include <ctype.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif /* HAVE_UNISTD_H */
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <atalk/logger.h>
40
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #ifdef HAVE_NETDB_H
44 #include <netdb.h>
45 #endif /* HAVE_NETDB_H */
46
47 #include <atalk/paths.h>
48 #include <atalk/util.h>
49 #include "globals.h"
50 #include "status.h"
51 #include "auth.h"
52
53 #include <atalk/compat.h>
54
55 #ifdef ADMIN_GRP
56 #include <grp.h>
57 #include <sys/types.h>
58 #endif /* ADMIN_GRP */
59
60 #ifndef MIN
61 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
62 #endif /* MIN */
63
64 #define OPTIONS "dn:f:s:uc:g:P:ptDS:TL:F:U:Ivm:"
65 #define LENGTH 512
66
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
69  * non-optimal. */
70 static char *getoption(char *buf, const char *option)
71 {
72     static char string[LENGTH + 1];
73     char *end;
74     int len;
75
76     if (option && (buf = strstr(buf, option)))
77         buf = strpbrk(buf, " \t");
78
79     while (buf && isspace(*buf))
80         buf++;
81
82     if (!buf)
83         return NULL;
84
85     /* search for any quoted stuff */
86     if (*buf == '"' && (end = strchr(buf + 1, '"'))) {
87         buf++;
88         len = MIN(end - buf, LENGTH);
89     } else if ((end = strpbrk(buf, " \t\n"))) /* option or eoln */
90         len = MIN(end - buf, LENGTH);
91     else
92         len = MIN(strlen(buf), LENGTH);
93
94     strncpy(string, buf, len);
95     string[len] = '\0';
96     return string;
97 }
98
99 /* get rid of any allocated afp_option buffers. */
100 void afp_options_free(struct afp_options *opt,
101                       const struct afp_options *save)
102 {
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))
110         free(opt->guest);
111     if (opt->server && (opt->server != save->server))
112         free(opt->server);
113     if (opt->ipaddr && (opt->ipaddr != save->ipaddr))
114         free(opt->ipaddr);
115     if (opt->fqdn && (opt->fqdn != save->fqdn))
116         free(opt->fqdn);
117     if (opt->uampath && (opt->uampath != save->uampath))
118         free(opt->uampath);
119     if (opt->uamlist && (opt->uamlist != save->uamlist))
120         free(opt->uamlist);
121     if (opt->nlspath && (opt->nlspath != save->nlspath))
122         free(opt->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))
130         free(opt->k5realm);
131     if (opt->k5keytab && (opt->k5keytab != save->k5keytab))
132         free(opt->k5keytab);
133 }
134
135 /* initialize options */
136 void afp_options_init(struct afp_options *options)
137 {
138     memset(options, 0, sizeof(struct afp_options));
139     options->connections = 20;
140     options->pidfile = _PATH_AFPDLOCK;
141     options->defaultvol = _PATH_AFPDDEFVOL;
142     options->systemvol = _PATH_AFPDSYSVOL;
143     options->configfile = _PATH_AFPDCONF;
144     options->nlspath = _PATH_AFPDNLSPATH;
145     options->uampath = _PATH_AFPDUAMPATH;
146     options->uamlist = "uams_clrtxt.so,uams_dhx.so";
147     options->guest = "nobody";
148     options->loginmesg = "";
149     options->transports = AFPTRANS_ALL;
150     options->passwdfile = _PATH_AFPDPWFILE;
151     options->tickleval = 30;
152     options->timeout = 4;
153     options->sleep = 10* 120; /* 10 h in 30 seconds tick */
154     options->server_notif = 1;
155     options->authprintdir = NULL;
156     options->signature = "host";
157     options->umask = 0;
158 #ifdef ADMIN_GRP
159     options->admingid = 0;
160 #endif /* ADMIN_GRP */
161     options->k5service = NULL;
162     options->k5realm = NULL;
163 }
164
165 /* parse an afpd.conf line. i'm doing it this way because it's
166  * easy. it is, however, massively hokey. sample afpd.conf:
167  * server:AFPServer@zone -loginmesg "blah blah blah" -nodsi 
168  * "private machine"@zone2 -noguest -port 11012
169  * server2 -nocleartxt -nodsi
170  *
171  * NOTE: this ignores unknown options 
172  */
173 int afp_options_parseline(char *buf, struct afp_options *options)
174 {
175     char *c, *opt;
176
177     /* handle server */
178     if (*buf != '-' && (c = getoption(buf, NULL)) && (opt = strdup(c)))
179         options->server = opt;
180
181     /* parse toggles */
182     if (strstr(buf, " -nodebug"))
183         options->flags &= ~OPTION_DEBUG;
184 #ifdef USE_SRVLOC
185     if (strstr(buf, " -noslp"))
186         options->flags |= OPTION_NOSLP;
187 #endif /* USE_SRVLOC */
188
189     if (strstr(buf, " -nouservolfirst"))
190         options->flags &= ~OPTION_USERVOLFIRST;
191     if (strstr(buf, " -uservolfirst"))
192         options->flags |= OPTION_USERVOLFIRST;
193     if (strstr(buf, " -nouservol"))
194         options->flags |= OPTION_NOUSERVOL;
195     if (strstr(buf, " -uservol"))
196         options->flags &= ~OPTION_NOUSERVOL;
197     if (strstr(buf, " -proxy"))
198         options->flags |= OPTION_PROXY;
199     if (strstr(buf, " -noicon"))
200         options->flags &= ~OPTION_CUSTOMICON;
201     if (strstr(buf, " -icon"))
202         options->flags |= OPTION_CUSTOMICON;
203
204     /* passwd bits */
205     if (strstr(buf, " -nosavepassword"))
206         options->passwdbits |= PASSWD_NOSAVE;
207     if (strstr(buf, " -savepassword"))
208         options->passwdbits &= ~PASSWD_NOSAVE;
209     if (strstr(buf, " -nosetpassword"))
210         options->passwdbits &= ~PASSWD_SET;
211     if (strstr(buf, " -setpassword"))
212         options->passwdbits |= PASSWD_SET;
213
214     /* transports */
215     if (strstr(buf, " -transall"))
216         options->transports = AFPTRANS_ALL;
217     if (strstr(buf, " -notransall"))
218         options->transports = AFPTRANS_NONE;
219     if (strstr(buf, " -tcp"))
220         options->transports |= AFPTRANS_TCP;
221     if (strstr(buf, " -notcp"))
222         options->transports &= ~AFPTRANS_TCP;
223     if (strstr(buf, " -ddp"))
224         options->transports |= AFPTRANS_DDP;
225     if (strstr(buf, " -noddp"))
226         options->transports &= ~AFPTRANS_DDP;
227     if (strstr(buf, "-client_polling"))
228         options->server_notif = 0;
229
230     /* figure out options w/ values. currently, this will ignore the setting
231      * if memory is lacking. */
232     if ((c = getoption(buf, "-defaultvol")) && (opt = strdup(c)))
233         options->defaultvol = opt;
234     if ((c = getoption(buf, "-systemvol")) && (opt = strdup(c)))
235         options->systemvol = opt;
236     if ((c = getoption(buf, "-loginmesg")) && (opt = strdup(c)))
237         options->loginmesg = opt;
238     if ((c = getoption(buf, "-guestname")) && (opt = strdup(c)))
239         options->guest = opt;
240     if ((c = getoption(buf, "-passwdfile")) && (opt = strdup(c)))
241         options->passwdfile = opt;
242     if ((c = getoption(buf, "-passwdminlen")))
243         options->passwdminlen = MIN(1, atoi(c));
244     if ((c = getoption(buf, "-loginmaxfail")))
245         options->loginmaxfail = atoi(c);
246     if ((c = getoption(buf, "-tickleval"))) {
247         options->tickleval = atoi(c);
248         if (options->tickleval <= 0) {
249             options->tickleval = 30;
250         }
251     }
252     if ((c = getoption(buf, "-timeout"))) {
253         options->timeout = atoi(c);
254         if (options->timeout <= 0) {
255             options->timeout = 4;
256         }
257     }
258
259     if ((c = getoption(buf, "-sleep"))) {
260         options->sleep = atoi(c) *120;
261         if (options->sleep <= 4) {
262             options->sleep = 4;
263         }
264     }
265
266     if ((c = getoption(buf, "-server_quantum")))
267         options->server_quantum = strtoul(c, NULL, 0);
268
269 #ifndef DISABLE_LOGGER
270     /* -setuplogtype <syslog|filelog> <logtype> <loglevel> <filename>*/
271     /* -[no]setuplog <logtype> <loglevel> [<filename>]*/
272     if ((c = getoption(buf, "-setuplog")))
273     {
274       char *ptr, *logtype, *loglevel, *filename;
275 #if 0
276       char *logsource;
277 #endif
278
279       LOG(log_debug6, logtype_afpd, "setting up logtype, c is %s", c);
280       ptr = c;
281
282 #if 0
283       logsource = ptr = c;
284       if (ptr)
285       {
286         ptr = strpbrk(ptr, " \t");
287         if (ptr) 
288         {
289           *ptr++ = 0;
290           while (*ptr && isspace(*ptr))
291             ptr++;
292         }
293       }
294 #endif
295
296       logtype = ptr; 
297       if (ptr)
298       {
299         ptr = strpbrk(ptr, " \t");
300         if (ptr) 
301         {
302           *ptr++ = 0;
303           while (*ptr && isspace(*ptr))
304             ptr++;
305         }
306       }
307
308       loglevel = ptr;
309       if (ptr)
310       {
311         ptr = strpbrk(ptr, " \t");
312         if (ptr) 
313         {
314           *ptr++ = 0;
315           while (*ptr && isspace(*ptr))
316             ptr++;
317         }
318       }
319
320       filename = ptr;
321       if (ptr)
322       {
323         ptr = strpbrk(ptr, " \t");
324         if (ptr) 
325         {
326           *ptr++ = 0;
327           while (*ptr && isspace(*ptr))
328             ptr++;
329         }
330       }
331
332       LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s", 
333           logtype, loglevel, filename);
334
335       setuplog(logtype, loglevel, filename);
336     }
337
338     if ((c = getoption(buf, "-unsetuplog")))
339     {
340       char *ptr, *logtype, *loglevel, *filename;
341
342       LOG(log_debug6, logtype_afpd, "unsetting up logtype, c is %s", c);
343
344       ptr = c;
345       logtype = ptr;
346       if (ptr)
347       {
348         ptr = strpbrk(ptr, " \t");
349         if (ptr)
350         {
351           *ptr++ = 0;
352           while (*ptr && isspace(*ptr))
353             ptr++;
354         }
355       }
356
357       loglevel = ptr;
358       if (ptr)
359       {
360         ptr = strpbrk(ptr, " \t");
361         if (ptr)
362         {
363           *ptr++ = 0;
364            while (*ptr && isspace(*ptr))
365              ptr++;
366         }
367       }
368
369       filename = ptr;
370       if (ptr)
371       {
372         ptr = strpbrk(ptr, " \t");
373         if (ptr)
374         {
375           *ptr++ = 0;
376           while (*ptr && isspace(*ptr))
377             ptr++;
378         }
379       }
380       
381       LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s",
382               logtype, NULL, filename);
383
384       setuplog(logtype, NULL, filename);
385     }
386 #endif /* DISABLE_LOGGER */
387 #ifdef ADMIN_GRP
388     if ((c = getoption(buf, "-admingroup"))) {
389         struct group *gr = getgrnam(c);
390         if (gr != NULL) {
391             options->admingid = gr->gr_gid;
392         }
393     }
394 #endif /* ADMIN_GRP */
395
396     if ((c = getoption(buf, "-k5service")) && (opt = strdup(c)))
397         options->k5service = opt;
398     if ((c = getoption(buf, "-k5realm")) && (opt = strdup(c)))
399         options->k5realm = opt;
400     if ((c = getoption(buf, "-k5keytab"))) {
401         if ( NULL == (options->k5keytab = (char *)malloc(sizeof(char) * (strlen(c)+14)) )) {
402             LOG(log_error, logtype_afpd, "malloc failed");
403             exit(-1);
404         }
405         snprintf(options->k5keytab, strlen(c) + 14, "KRB5_KTNAME=%s",c);
406         putenv(options->k5keytab);
407     }
408     if ((c = getoption(buf, "-authprintdir")) && (opt = strdup(c)))
409         options->authprintdir = opt;
410     if ((c = getoption(buf, "-uampath")) && (opt = strdup(c)))
411         options->uampath = opt;
412     if ((c = getoption(buf, "-uamlist")) && (opt = strdup(c)))
413         options->uamlist = opt;
414     if ((c = getoption(buf, "-nlspath")) && (opt = strdup(c)))
415         options->nlspath = opt;
416
417     if ((c = getoption(buf, "-ipaddr"))) {
418         struct in_addr inaddr;
419         if (inet_aton(c, &inaddr) && (opt = strdup(c))) {
420             if (!gethostbyaddr((const char *) &inaddr, sizeof(inaddr), AF_INET))
421                 LOG(log_info, logtype_afpd, "WARNING: can't find %s\n", opt);
422             options->ipaddr = opt;
423         }
424     }
425
426     if ((c = getoption(buf, "-port")))
427         options->port = atoi(c);
428     if ((c = getoption(buf, "-ddpaddr")))
429         atalk_aton(c, &options->ddpaddr);
430     if ((c = getoption(buf, "-signature")) && (opt = strdup(c)))
431         options->signature = opt;
432
433     /* do a little checking for the domain name. */
434     if ((c = getoption(buf, "-fqdn"))) {
435         char *p = strchr(c, ':');
436         if (p)
437             *p = '\0';
438         if (gethostbyname(c)) {
439             if (p)
440                 *p = ':';
441             if ((opt = strdup(c)))
442                 options->fqdn = opt;
443         }
444     }
445
446     return 1;
447 }
448
449 int afp_options_parse(int ac, char **av, struct afp_options *options)
450 {
451     extern char *optarg;
452     extern int optind;
453
454     char *p;
455     char *tmp;  /* Used for error checking the result of strtol */
456     int c, err = 0;
457
458     if (gethostname(options->hostname, sizeof(options->hostname )) < 0 ) {
459         perror( "gethostname" );
460         return 0;
461     }
462     if (NULL != ( p = strchr(options->hostname, '.' )) ) {
463         *p = '\0';
464     }
465
466     if (NULL == ( p = strrchr( av[ 0 ], '/' )) ) {
467         p = av[ 0 ];
468     } else {
469         p++;
470     }
471
472     while (EOF != ( c = getopt( ac, av, OPTIONS )) ) {
473         switch ( c ) {
474         case 'd' :
475             options->flags |= OPTION_DEBUG;
476             break;
477         case 'n' :
478             options->server = optarg;
479             break;
480         case 'f' :
481             options->defaultvol = optarg;
482             break;
483         case 's' :
484             options->systemvol = optarg;
485             break;
486         case 'u' :
487             options->flags |= OPTION_USERVOLFIRST;
488             break;
489         case 'c' :
490             options->connections = atoi( optarg );
491             break;
492         case 'g' :
493             options->guest = optarg;
494             break;
495
496         case 'P' :
497             options->pidfile = optarg;
498             break;
499
500         case 'p':
501             options->passwdbits |= PASSWD_NOSAVE;
502             break;
503         case 't':
504             options->passwdbits |= PASSWD_SET;
505             break;
506
507         case 'D':
508             options->transports &= ~AFPTRANS_DDP;
509             break;
510         case 'S':
511             options->port = atoi(optarg);
512             break;
513         case 'T':
514             options->transports &= ~AFPTRANS_TCP;
515             break;
516         case 'L':
517             options->loginmesg = optarg;
518             break;
519         case 'F':
520             options->configfile = optarg;
521             break;
522         case 'U':
523             options->uamlist = optarg;
524             break;
525         case 'v':       /* version */
526             printf( "afpd (version %s)\n", VERSION );
527             exit ( 1 );
528             break;
529         case 'I':
530             options->flags |= OPTION_CUSTOMICON;
531             break;
532         case 'm':
533             options->umask = strtoul(optarg, &tmp, 8);
534             if ((options->umask > 0777)) {
535                 fprintf(stderr, "%s: out of range umask setting provided\n", p);
536                 err++;
537             }
538             if (tmp[0] != '\0') {
539                 fprintf(stderr, "%s: invalid characters in umask setting provided\n", p);
540                 err++;
541             }
542             break;
543         default :
544             err++;
545         }
546     }
547     if ( err || optind != ac ) {
548         fprintf( stderr,
549                  "Usage:\t%s [ -dpDTIt ] [ -n nbpname ] [ -f defvols ] \
550                  [ -P pidfile ] [ -s sysvols ] \n", p );
551         fprintf( stderr,
552                  "\t[ -u ] [ -c maxconn ] [ -g guest ] \
553                  [ -S port ] [ -L loginmesg ] [ -F configfile ] [ -U uamlist ]\n" );
554         return 0;
555     }
556
557 #ifdef ultrix
558     openlog( p, LOG_PID ); /* ultrix only */
559 #else /* ultrix */
560     set_processname(p);
561     syslog_setup(log_debug, logtype_default, logoption_ndelay|logoption_pid, logfacility_daemon);
562 #endif /* ultrix */
563
564     return 1;
565 }