]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_options.c
big merge for db frontend and unicode.
[netatalk.git] / etc / afpd / afp_options.c
1 /*
2  * $Id: afp_options.c,v 1.30.2.2.2.1 2003-09-09 16:42:19 didg 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 /* FIXME CNID */
65 char             *Cnid_srv;
66 int              Cnid_port;
67
68 #define OPTIONS "dn:f:s:uc:g:P:ptDS:TL:F:U:Ivm:"
69 #define LENGTH 512
70
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
73  * non-optimal. */
74 static char *getoption(char *buf, const char *option)
75 {
76     static char string[LENGTH + 1];
77     char *end;
78     int len;
79
80     if (option && (buf = strstr(buf, option)))
81         buf = strpbrk(buf, " \t");
82
83     while (buf && isspace(*buf))
84         buf++;
85
86     if (!buf)
87         return NULL;
88
89     /* search for any quoted stuff */
90     if (*buf == '"' && (end = strchr(buf + 1, '"'))) {
91         buf++;
92         len = MIN(end - buf, LENGTH);
93     } else if ((end = strpbrk(buf, " \t\n"))) /* option or eoln */
94         len = MIN(end - buf, LENGTH);
95     else
96         len = MIN(strlen(buf), LENGTH);
97
98     strncpy(string, buf, len);
99     string[len] = '\0';
100     return string;
101 }
102
103 /* get rid of any allocated afp_option buffers. */
104 void afp_options_free(struct afp_options *opt,
105                       const struct afp_options *save)
106 {
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);
111
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);
116
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);
121
122     if (opt->loginmesg && (opt->loginmesg != save->loginmesg))
123         free(opt->loginmesg);
124     if (opt->guest && (opt->guest != save->guest))
125         free(opt->guest);
126     if (opt->server && (opt->server != save->server))
127         free(opt->server);
128     if (opt->ipaddr && (opt->ipaddr != save->ipaddr))
129         free(opt->ipaddr);
130     if (opt->fqdn && (opt->fqdn != save->fqdn))
131         free(opt->fqdn);
132     if (opt->uampath && (opt->uampath != save->uampath))
133         free(opt->uampath);
134     if (opt->uamlist && (opt->uamlist != save->uamlist))
135         free(opt->uamlist);
136     if (opt->nlspath && (opt->nlspath != save->nlspath))
137         free(opt->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))
145         free(opt->k5realm);
146     if (opt->k5keytab && (opt->k5keytab != save->k5keytab))
147         free(opt->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);
152 }
153
154 /* initialize options */
155 void afp_options_init(struct afp_options *options)
156 {
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";
176     options->umask = 0;
177 #ifdef ADMIN_GRP
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";
187 }
188
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
194  *
195  * NOTE: this ignores unknown options 
196  */
197 int afp_options_parseline(char *buf, struct afp_options *options)
198 {
199     char *c, *opt;
200
201     /* handle server */
202     if (*buf != '-' && (c = getoption(buf, NULL)) && (opt = strdup(c)))
203         options->server = opt;
204
205     /* parse toggles */
206     if (strstr(buf, " -nodebug"))
207         options->flags &= ~OPTION_DEBUG;
208 #ifdef USE_SRVLOC
209     if (strstr(buf, " -noslp"))
210         options->flags |= OPTION_NOSLP;
211 #endif /* USE_SRVLOC */
212
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;
227
228     /* passwd bits */
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;
237
238     /* transports */
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;
253
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;
274         }
275     }
276     if ((c = getoption(buf, "-timeout"))) {
277         options->timeout = atoi(c);
278         if (options->timeout <= 0) {
279             options->timeout = 4;
280         }
281     }
282
283     if ((c = getoption(buf, "-sleep"))) {
284         options->sleep = atoi(c) *120;
285         if (options->sleep <= 4) {
286             options->sleep = 4;
287         }
288     }
289
290     if ((c = getoption(buf, "-server_quantum")))
291         options->server_quantum = strtoul(c, NULL, 0);
292
293 #ifndef DISABLE_LOGGER
294     /* -setuplogtype <syslog|filelog> <logtype> <loglevel> <filename>*/
295     /* -[no]setuplog <logtype> <loglevel> [<filename>]*/
296     if ((c = getoption(buf, "-setuplog")))
297     {
298       char *ptr, *logsource, *logtype, *loglevel, *filename;
299
300       LOG(log_debug6, logtype_afpd, "setting up logtype, c is %s", c);
301       ptr = c;
302       
303       /* 
304       logsource = ptr = c;
305       if (ptr)
306       {
307         ptr = strpbrk(ptr, " \t");
308         if (ptr) 
309         {
310           *ptr++ = 0;
311           while (*ptr && isspace(*ptr))
312             ptr++;
313         }
314       }
315       */
316
317       logtype = ptr; 
318       if (ptr)
319       {
320         ptr = strpbrk(ptr, " \t");
321         if (ptr) 
322         {
323           *ptr++ = 0;
324           while (*ptr && isspace(*ptr))
325             ptr++;
326         }
327       }
328
329       loglevel = ptr;
330       if (ptr)
331       {
332         ptr = strpbrk(ptr, " \t");
333         if (ptr) 
334         {
335           *ptr++ = 0;
336           while (*ptr && isspace(*ptr))
337             ptr++;
338         }
339       }
340
341       filename = ptr;
342       if (ptr)
343       {
344         ptr = strpbrk(ptr, " \t");
345         if (ptr) 
346         {
347           *ptr++ = 0;
348           while (*ptr && isspace(*ptr))
349             ptr++;
350         }
351       }
352
353       LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s", 
354           logtype, loglevel, filename);
355
356       setuplog(logtype, loglevel, filename);
357     }
358
359     if ((c = getoption(buf, "-unsetuplog")))
360     {
361       char *ptr, *logtype, *loglevel, *filename;
362
363       LOG(log_debug6, logtype_afpd, "unsetting up logtype, c is %s", c);
364
365       ptr = c;
366       logtype = ptr;
367       if (ptr)
368       {
369         ptr = strpbrk(ptr, " \t");
370         if (ptr)
371         {
372           *ptr++ = 0;
373           while (*ptr && isspace(*ptr))
374             ptr++;
375         }
376       }
377
378       loglevel = ptr;
379       if (ptr)
380       {
381         ptr = strpbrk(ptr, " \t");
382         if (ptr)
383         {
384           *ptr++ = 0;
385            while (*ptr && isspace(*ptr))
386              ptr++;
387         }
388       }
389
390       filename = ptr;
391       if (ptr)
392       {
393         ptr = strpbrk(ptr, " \t");
394         if (ptr)
395         {
396           *ptr++ = 0;
397           while (*ptr && isspace(*ptr))
398             ptr++;
399         }
400       }
401       
402       LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s",
403               logtype, NULL, filename);
404
405       setuplog(logtype, NULL, filename);
406     }
407 #endif /* DISABLE_LOGGER */
408 #ifdef ADMIN_GRP
409     if ((c = getoption(buf, "-admingroup"))) {
410         struct group *gr = getgrnam(c);
411         if (gr != NULL) {
412             options->admingid = gr->gr_gid;
413         }
414     }
415 #endif /* ADMIN_GRP */
416
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");
424                 exit(-1);
425         }
426         snprintf(options->k5keytab, strlen(c)+14, "KRB5_KTNAME=%s", c);
427         putenv(options->k5keytab);
428         /* setenv( "KRB5_KTNAME", c, 1 ); */
429     }
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;
438
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;
445         }
446     }
447
448     /* FIXME CNID */
449     if ((c = getoption(buf, "-cnidserver"))) {
450         char *p;
451         
452         Cnid_srv = strdup(c);
453         p = strchr(Cnid_srv, ':');
454         *p = 0;
455         Cnid_port = atoi(p +1);
456     }
457
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;
464
465     /* do a little checking for the domain name. */
466     if ((c = getoption(buf, "-fqdn"))) {
467         char *p = strchr(c, ':');
468         if (p)
469             *p = '\0';
470         if (gethostbyname(c)) {
471             if (p)
472                 *p = ':';
473             if ((opt = strdup(c)))
474                 options->fqdn = opt;
475         }
476     }
477
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;
483         }
484     }
485         
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;
491         }
492         LOG(log_debug, logtype_afpd, "Setting Mac Codepage to '%s'", options->maccodepage);
493     }
494
495     return 1;
496 }
497
498 int afp_options_parse(int ac, char **av, struct afp_options *options)
499 {
500     extern char *optarg;
501     extern int optind;
502
503     char *p;
504     char *tmp;  /* Used for error checking the result of strtol */
505     int c, err = 0;
506
507     if (gethostname(options->hostname, sizeof(options->hostname )) < 0 ) {
508         perror( "gethostname" );
509         return 0;
510     }
511     if (NULL != ( p = strchr(options->hostname, '.' )) ) {
512         *p = '\0';
513     }
514
515     if (NULL == ( p = strrchr( av[ 0 ], '/' )) ) {
516         p = av[ 0 ];
517     } else {
518         p++;
519     }
520
521     while (EOF != ( c = getopt( ac, av, OPTIONS )) ) {
522         switch ( c ) {
523         case 'd' :
524             options->flags |= OPTION_DEBUG;
525             break;
526         case 'n' :
527             options->server = optarg;
528             break;
529         case 'f' :
530             options->defaultvol.name = optarg;
531             break;
532         case 's' :
533             options->systemvol.name = optarg;
534             break;
535         case 'u' :
536             options->flags |= OPTION_USERVOLFIRST;
537             break;
538         case 'c' :
539             options->connections = atoi( optarg );
540             break;
541         case 'g' :
542             options->guest = optarg;
543             break;
544
545         case 'P' :
546             options->pidfile = optarg;
547             break;
548
549         case 'p':
550             options->passwdbits |= PASSWD_NOSAVE;
551             break;
552         case 't':
553             options->passwdbits |= PASSWD_SET;
554             break;
555
556         case 'D':
557             options->transports &= ~AFPTRANS_DDP;
558             break;
559         case 'S':
560             options->port = atoi(optarg);
561             break;
562         case 'T':
563             options->transports &= ~AFPTRANS_TCP;
564             break;
565         case 'L':
566             options->loginmesg = optarg;
567             break;
568         case 'F':
569             options->configfile = optarg;
570             break;
571         case 'U':
572             options->uamlist = optarg;
573             break;
574         case 'v':       /* version */
575             printf( "afpd (version %s)\n", VERSION );
576             exit ( 1 );
577             break;
578         case 'I':
579             options->flags |= OPTION_CUSTOMICON;
580             break;
581         case 'm':
582             options->umask = strtoul(optarg, &tmp, 8);
583             if ((options->umask > 0777)) {
584                 fprintf(stderr, "%s: out of range umask setting provided\n", p);
585                 err++;
586             }
587             if (tmp[0] != '\0') {
588                 fprintf(stderr, "%s: invalid characters in umask setting provided\n", p);
589                 err++;
590             }
591             break;
592         default :
593             err++;
594         }
595     }
596     if ( err || optind != ac ) {
597         fprintf( stderr,
598                  "Usage:\t%s [ -dpDTIt ] [ -n nbpname ] [ -f defvols ] \
599                  [ -P pidfile ] [ -s sysvols ] \n", p );
600         fprintf( stderr,
601                  "\t[ -u ] [ -c maxconn ] [ -g guest ] \
602                  [ -S port ] [ -L loginmesg ] [ -F configfile ] [ -U uamlist ]\n" );
603         return 0;
604     }
605
606 #ifdef ultrix
607     openlog( p, LOG_PID ); /* ultrix only */
608 #else /* ultrix */
609     set_processname(p);
610     syslog_setup(log_debug, logtype_default, logoption_ndelay|logoption_pid, logfacility_daemon);
611 #endif /* ultrix */
612
613     return 1;
614 }