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