]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_options.c
Style police.
[netatalk.git] / etc / afpd / afp_options.c
1 /*
2  * $Id: afp_options.c,v 1.17 2002-01-19 21:29:55 jmarcus 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 }
126
127 /* initialize options */
128 void afp_options_init(struct afp_options *options)
129 {
130     memset(options, 0, sizeof(struct afp_options));
131     options->connections = 20;
132     options->pidfile = _PATH_AFPDLOCK;
133     options->defaultvol = _PATH_AFPDDEFVOL;
134     options->systemvol = _PATH_AFPDSYSVOL;
135     options->configfile = _PATH_AFPDCONF;
136     options->nlspath = _PATH_AFPDNLSPATH;
137     options->uampath = _PATH_AFPDUAMPATH;
138     options->uamlist = "uams_clrtxt.so,uams_dhx.so";
139     options->guest = "nobody";
140     options->loginmesg = "";
141     options->transports = AFPTRANS_ALL;
142     options->passwdfile = _PATH_AFPDPWFILE;
143     options->tickleval = 30;
144     options->timeout = 4;
145     options->authprintdir = NULL;
146     options->umask = 0;
147 #ifdef ADMIN_GRP
148     options->admingid = 0;
149 #endif /* ADMIN_GRP */
150 }
151
152 /* parse an afpd.conf line. i'm doing it this way because it's
153  * easy. it is, however, massively hokey. sample afpd.conf:
154  * server:AFPServer@zone -loginmesg "blah blah blah" -nodsi 
155  * "private machine"@zone2 -noguest -port 11012
156  * server2 -nocleartxt -nodsi
157  *
158  * NOTE: this ignores unknown options 
159  */
160 int afp_options_parseline(char *buf, struct afp_options *options)
161 {
162     char *c, *opt;
163
164     /* handle server */
165     if (*buf != '-' && (c = getoption(buf, NULL)) && (opt = strdup(c)))
166         options->server = opt;
167
168     /* parse toggles */
169     if (strstr(buf, " -nodebug"))
170         options->flags &= ~OPTION_DEBUG;
171
172     if (strstr(buf, " -nouservolfirst"))
173         options->flags &= ~OPTION_USERVOLFIRST;
174     if (strstr(buf, " -uservolfirst"))
175         options->flags |= OPTION_USERVOLFIRST;
176     if (strstr(buf, " -nouservol"))
177         options->flags |= OPTION_NOUSERVOL;
178     if (strstr(buf, " -uservol"))
179         options->flags &= ~OPTION_NOUSERVOL;
180     if (strstr(buf, " -proxy"))
181         options->flags |= OPTION_PROXY;
182     if (strstr(buf, " -noicon"))
183         options->flags &= ~OPTION_CUSTOMICON;
184     if (strstr(buf, " -icon"))
185         options->flags |= OPTION_CUSTOMICON;
186
187     /* passwd bits */
188     if (strstr(buf, " -nosavepassword"))
189         options->passwdbits |= PASSWD_NOSAVE;
190     if (strstr(buf, " -savepassword"))
191         options->passwdbits &= ~PASSWD_NOSAVE;
192     if (strstr(buf, " -nosetpassword"))
193         options->passwdbits &= ~PASSWD_SET;
194     if (strstr(buf, " -setpassword"))
195         options->passwdbits |= PASSWD_SET;
196
197     /* transports */
198     if (strstr(buf, " -transall"))
199         options->transports = AFPTRANS_ALL;
200     if (strstr(buf, " -notransall"))
201         options->transports = AFPTRANS_NONE;
202     if (strstr(buf, " -tcp"))
203         options->transports |= AFPTRANS_TCP;
204     if (strstr(buf, " -notcp"))
205         options->transports &= ~AFPTRANS_TCP;
206     if (strstr(buf, " -ddp"))
207         options->transports |= AFPTRANS_DDP;
208     if (strstr(buf, " -noddp"))
209         options->transports &= ~AFPTRANS_DDP;
210
211     /* figure out options w/ values. currently, this will ignore the setting
212      * if memory is lacking. */
213     if ((c = getoption(buf, "-defaultvol")) && (opt = strdup(c)))
214         options->defaultvol = opt;
215     if ((c = getoption(buf, "-systemvol")) && (opt = strdup(c)))
216         options->systemvol = opt;
217     if ((c = getoption(buf, "-loginmesg")) && (opt = strdup(c)))
218         options->loginmesg = opt;
219     if ((c = getoption(buf, "-guestname")) && (opt = strdup(c)))
220         options->guest = opt;
221     if ((c = getoption(buf, "-passwdfile")) && (opt = strdup(c)))
222         options->passwdfile = opt;
223     if ((c = getoption(buf, "-passwdminlen")))
224         options->passwdminlen = MIN(1, atoi(c));
225     if ((c = getoption(buf, "-loginmaxfail")))
226         options->loginmaxfail = atoi(c);
227     if ((c = getoption(buf, "-tickleval"))) {
228         options->tickleval = atoi(c);
229         if (options->tickleval <= 0) {
230             options->tickleval = 30;
231         }
232     }
233     if ((c = getoption(buf, "-timeout"))) {
234         options->timeout = atoi(c);
235         if (options->timeout <= 0) {
236             options->timeout = 4;
237         }
238     }
239
240     if ((c = getoption(buf, "-server_quantum")))
241         options->server_quantum = strtoul(c, NULL, 0);
242
243
244 #ifdef ADMIN_GRP
245     if ((c = getoption(buf, "-admingroup"))) {
246         struct group *gr = getgrnam(c);
247         if (gr != NULL) {
248             options->admingid = gr->gr_gid;
249         }
250     }
251 #endif /* ADMIN_GRP */
252
253     if ((c = getoption(buf, "-authprintdir")) && (opt = strdup(c)))
254         options->authprintdir = opt;
255     if ((c = getoption(buf, "-uampath")) && (opt = strdup(c)))
256         options->uampath = opt;
257     if ((c = getoption(buf, "-uamlist")) && (opt = strdup(c)))
258         options->uamlist = opt;
259     if ((c = getoption(buf, "-nlspath")) && (opt = strdup(c)))
260         options->nlspath = opt;
261
262     if ((c = getoption(buf, "-ipaddr"))) {
263         struct in_addr inaddr;
264         if (inet_aton(c, &inaddr) && (opt = strdup(c))) {
265             if (!gethostbyaddr((const char *) &inaddr, sizeof(inaddr), AF_INET))
266                 LOG(log_info, logtype_default, "WARNING: can't find %s\n", opt);
267             options->ipaddr = opt;
268         }
269     }
270
271     if ((c = getoption(buf, "-port")))
272         options->port = atoi(c);
273     if ((c = getoption(buf, "-ddpaddr")))
274         atalk_aton(c, &options->ddpaddr);
275
276     /* do a little checking for the domain name. */
277     if ((c = getoption(buf, "-fqdn"))) {
278         char *p = strchr(c, ':');
279         if (p)
280             *p = '\0';
281         if (gethostbyname(c)) {
282             if (p)
283                 *p = ':';
284             if ((opt = strdup(c)))
285                 options->fqdn = opt;
286         }
287     }
288
289     return 1;
290 }
291
292 int afp_options_parse(int ac, char **av, struct afp_options *options)
293 {
294     extern char *optarg;
295     extern int optind;
296
297     char *p;
298     char *tmp;  /* Used for error checking the result of strtol */
299     int c, err = 0;
300
301     if (gethostname(options->hostname, sizeof(options->hostname )) < 0 ) {
302         perror( "gethostname" );
303         return 0;
304     }
305     if (( p = strchr(options->hostname, '.' )) != 0 ) {
306         *p = '\0';
307     }
308
309     if (( p = strrchr( av[ 0 ], '/' )) == NULL ) {
310         p = av[ 0 ];
311     } else {
312         p++;
313     }
314
315     while (( c = getopt( ac, av, OPTIONS )) != EOF ) {
316         switch ( c ) {
317         case 'd' :
318             options->flags |= OPTION_DEBUG;
319             break;
320         case 'n' :
321             options->server = optarg;
322             break;
323         case 'f' :
324             options->defaultvol = optarg;
325             break;
326         case 's' :
327             options->systemvol = optarg;
328             break;
329         case 'u' :
330             options->flags |= OPTION_USERVOLFIRST;
331             break;
332         case 'c' :
333             options->connections = atoi( optarg );
334             break;
335         case 'g' :
336             options->guest = optarg;
337             break;
338
339         case 'P' :
340             options->pidfile = optarg;
341             break;
342
343         case 'p':
344             options->passwdbits |= PASSWD_NOSAVE;
345             break;
346         case 't':
347             options->passwdbits |= PASSWD_SET;
348             break;
349
350         case 'D':
351             options->transports &= ~AFPTRANS_DDP;
352             break;
353         case 'S':
354             options->port = atoi(optarg);
355             break;
356         case 'T':
357             options->transports &= ~AFPTRANS_TCP;
358             break;
359         case 'L':
360             options->loginmesg = optarg;
361             break;
362         case 'F':
363             options->configfile = optarg;
364             break;
365         case 'U':
366             options->uamlist = optarg;
367             break;
368         case 'v':       /* version */
369             printf( "afpd (version %s)\n", VERSION );
370             exit ( 1 );
371             break;
372         case 'I':
373             options->flags |= OPTION_CUSTOMICON;
374             break;
375         case 'm':
376             options->umask = strtol(optarg, &tmp, 8);
377             if ((options->umask > 0777)) {
378                 fprintf(stderr, "%s: out of range umask setting provided\n", p);
379                 err++;
380             }
381             if (tmp[0] != '\0') {
382                 fprintf(stderr, "%s: invalid characters in umask setting provided\n", p);
383                 err++;
384             }
385             break;
386         default :
387             err++;
388         }
389     }
390     if ( err || optind != ac ) {
391         fprintf( stderr,
392                  "Usage:\t%s [ -dpDTIt ] [ -n nbpname ] [ -f defvols ] \
393                  [ -P pidfile ] [ -s sysvols ] \n", p );
394         fprintf( stderr,
395                  "\t[ -u ] [ -c maxconn ] [ -g guest ] \
396                  [ -S port ] [ -L loginmesg ] [ -F configfile ] [ -U uamlist ]\n" );
397         return 0;
398     }
399
400 #ifdef ultrix
401     openlog( p, LOG_PID ); /* ultrix only */
402 #else /* ultrix */
403     set_processname(p);
404     syslog_setup(log_debug, logtype_default, logoption_ndelay|logoption_pid, logfacility_daemon);
405 #endif /* ultrix */
406
407     return 1;
408 }