]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_options.c
199d09f6e14cd1c415dc549beb0fb4f570347c3f
[netatalk.git] / etc / afpd / afp_options.c
1 /* 
2  * $Id: afp_options.c,v 1.7 2001-06-20 18:33:04 rufustfirefly 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 #include <string.h>
18 #include <ctype.h>
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif /* HAVE_UNISTD_H */
22 #include <sys/param.h>
23 #include <sys/socket.h>
24 #include <syslog.h>
25
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #ifdef HAVE_NETDB_H
29 #include <netdb.h>
30 #endif /* HAVE_NETDB_H */
31
32 #include <atalk/paths.h>
33 #include <atalk/util.h>
34 #include "globals.h"
35 #include "status.h"
36 #include "auth.h"
37
38 #include <atalk/compat.h>
39
40 #ifdef ADMIN_GRP
41 #include <grp.h>
42 #include <sys/types.h>
43 #endif /* ADMIN_GRP */
44
45 #ifndef MIN
46 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
47 #endif /* MIN */
48
49 #define OPTIONS "dn:f:s:uc:g:P:ptDS:TL:F:U:I"
50 #define LENGTH 512
51
52 /* return an option. this uses an internal array, so it's necessary
53  * to duplicate it if you want to hold it for long. this is probably
54  * non-optimal. */
55 static char *getoption(char *buf, const char *option)
56 {
57   static char string[LENGTH + 1];
58   char *end;
59   int len;
60
61   if (option && (buf = strstr(buf, option)))
62     buf = strpbrk(buf, " \t");
63
64   while (buf && isspace(*buf))
65     buf++;
66       
67   if (!buf)
68     return NULL;
69
70   /* search for any quoted stuff */
71   if (*buf == '"' && (end = strchr(buf + 1, '"'))) { 
72     buf++;
73     len = MIN(end - buf, LENGTH);
74   } else if ((end = strpbrk(buf, " \t\n"))) /* option or eoln */
75     len = MIN(end - buf, LENGTH);
76   else
77     len = MIN(strlen(buf), LENGTH);
78   
79   strncpy(string, buf, len);
80   string[len] = '\0';
81   return string;
82 }
83
84 /* get rid of any allocated afp_option buffers. */
85 void afp_options_free(struct afp_options *opt, 
86                       const struct afp_options *save)
87 {
88   if (opt->defaultvol && (opt->defaultvol != save->defaultvol))
89     free(opt->defaultvol);
90   if (opt->systemvol && (opt->systemvol != save->systemvol))
91     free(opt->systemvol);
92   if (opt->loginmesg && (opt->loginmesg != save->loginmesg))
93     free(opt->loginmesg);
94   if (opt->guest && (opt->guest != save->guest)) 
95     free(opt->guest);
96   if (opt->server && (opt->server != save->server))
97     free(opt->server);
98   if (opt->ipaddr && (opt->ipaddr != save->ipaddr))
99     free(opt->ipaddr);
100   if (opt->fqdn && (opt->fqdn != save->fqdn))
101     free(opt->fqdn);
102   if (opt->uampath && (opt->uampath != save->uampath))
103     free(opt->uampath);
104   if (opt->uamlist && (opt->uamlist != save->uamlist))
105     free(opt->uamlist);
106   if (opt->nlspath && (opt->nlspath != save->nlspath))
107     free(opt->nlspath);
108   if (opt->passwdfile && (opt->passwdfile != save->passwdfile))
109     free(opt->passwdfile);
110 }
111
112 /* initialize options */
113 void afp_options_init(struct afp_options *options)
114 {
115   memset(options, 0, sizeof(struct afp_options));
116   options->connections = 20;
117   options->pidfile = _PATH_AFPDLOCK;
118   options->defaultvol = _PATH_AFPDDEFVOL;
119   options->systemvol = _PATH_AFPDSYSVOL;
120   options->configfile = _PATH_AFPDCONF;
121   options->nlspath = _PATH_AFPDNLSPATH;
122   options->uampath = _PATH_AFPDUAMPATH;
123   options->uamlist = "uams_guest.so,uams_clrtxt.so,uams_dhx.so";
124   options->guest = "nobody";
125   options->loginmesg = "";
126   options->transports = AFPTRANS_ALL;
127   options->passwdfile = _PATH_AFPDPWFILE;
128   options->tickleval = 30;
129   options->authprintdir = NULL;
130 #ifdef ADMIN_GRP
131   options->admingid = 0;
132 #endif /* ADMIN_GRP */
133 }
134
135 /* parse an afpd.conf line. i'm doing it this way because it's
136  * easy. it is, however, massively hokey. sample afpd.conf:
137  * server:AFPServer@zone -loginmesg "blah blah blah" -nodsi 
138  * "private machine"@zone2 -noguest -port 11012
139  * server2 -nocleartxt -nodsi
140  *
141  * NOTE: this ignores unknown options 
142  */
143 int afp_options_parseline(char *buf, struct afp_options *options)
144 {
145   char *c, *opt;
146   
147   /* handle server */
148   if (*buf != '-' && (c = getoption(buf, NULL)) && (opt = strdup(c)))
149     options->server = opt;
150
151   /* parse toggles */
152   if (strstr(buf, " -nodebug"))
153     options->flags &= ~OPTION_DEBUG;
154
155   if (strstr(buf, " -nouservolfirst"))
156     options->flags &= ~OPTION_USERVOLFIRST;
157   if (strstr(buf, " -uservolfirst"))
158     options->flags |= OPTION_USERVOLFIRST;
159   if (strstr(buf, " -nouservol"))
160     options->flags |= OPTION_NOUSERVOL;
161   if (strstr(buf, " -uservol"))
162     options->flags &= ~OPTION_NOUSERVOL;
163   if (strstr(buf, " -proxy"))
164     options->flags |= OPTION_PROXY;
165   if (strstr(buf, " -noicon"))
166     options->flags &= ~OPTION_CUSTOMICON;
167   if (strstr(buf, " -icon"))
168     options->flags |= OPTION_CUSTOMICON;
169
170   /* passwd bits */
171   if (strstr(buf, " -nosavepassword"))
172     options->passwdbits |= PASSWD_NOSAVE;
173   if (strstr(buf, " -savepassword"))
174     options->passwdbits &= ~PASSWD_NOSAVE;
175   if (strstr(buf, " -nosetpassword"))
176     options->passwdbits &= ~PASSWD_SET;
177   if (strstr(buf, " -setpassword"))
178     options->passwdbits |= PASSWD_SET;
179   
180   /* transports */
181   if (strstr(buf, " -transall"))
182     options->transports = AFPTRANS_ALL;
183   if (strstr(buf, " -notransall"))
184     options->transports = AFPTRANS_NONE;
185   if (strstr(buf, " -tcp"))
186     options->transports |= AFPTRANS_TCP;
187   if (strstr(buf, " -notcp"))
188     options->transports &= ~AFPTRANS_TCP;
189   if (strstr(buf, " -ddp"))
190     options->transports |= AFPTRANS_DDP;
191   if (strstr(buf, " -noddp"))
192     options->transports &= ~AFPTRANS_DDP;
193
194   /* figure out options w/ values. currently, this will ignore the setting
195    * if memory is lacking. */
196   if ((c = getoption(buf, "-defaultvol")) && (opt = strdup(c)))
197     options->defaultvol = opt;
198   if ((c = getoption(buf, "-systemvol")) && (opt = strdup(c)))
199     options->systemvol = opt;
200   if ((c = getoption(buf, "-loginmesg")) && (opt = strdup(c)))
201     options->loginmesg = opt;
202   if ((c = getoption(buf, "-guestname")) && (opt = strdup(c)))
203     options->guest = opt;
204   if ((c = getoption(buf, "-passwdfile")) && (opt = strdup(c)))
205     options->passwdfile = opt;
206   if ((c = getoption(buf, "-passwdminlen")))
207     options->passwdminlen = MIN(1, atoi(c));
208   if ((c = getoption(buf, "-loginmaxfail")))
209     options->loginmaxfail = atoi(c);
210   if ((c = getoption(buf, "-tickleval")))
211     options->tickleval = atoi(c);
212
213   if ((c = getoption(buf, "-server_quantum")))
214     options->server_quantum = strtoul(c, NULL, 0);
215
216
217 #ifdef ADMIN_GRP
218   if ((c = getoption(buf, "-admingroup"))) {
219     struct group *gr = getgrnam(c);
220     if (gr != NULL) { 
221       options->admingid = gr->gr_gid;
222     }
223   } 
224 #endif /* ADMIN_GRP */
225
226   if ((c = getoption(buf, "-authprintdir")) && (opt = strdup(c)))
227     options->authprintdir = opt;
228   if ((c = getoption(buf, "-uampath")) && (opt = strdup(c)))
229     options->uampath = opt;
230   if ((c = getoption(buf, "-uamlist")) && (opt = strdup(c)))
231     options->uamlist = opt;
232   if ((c = getoption(buf, "-nlspath")) && (opt = strdup(c)))
233     options->nlspath = opt;
234
235   if ((c = getoption(buf, "-ipaddr"))) {
236     struct in_addr inaddr;
237     if (inet_aton(c, &inaddr) && (opt = strdup(c))) { 
238       if (!gethostbyaddr((const char *) &inaddr, sizeof(inaddr), AF_INET)) 
239         syslog(LOG_INFO, "WARNING: can't find %s\n", opt);
240       options->ipaddr = opt;
241     }
242   }
243
244   if ((c = getoption(buf, "-port")))
245     options->port = atoi(c);
246   if ((c = getoption(buf, "-ddpaddr")))
247     atalk_aton(c, &options->ddpaddr);
248
249   /* do a little checking for the domain name. */
250   if ((c = getoption(buf, "-fqdn"))) {
251     char *p = strchr(c, ':');
252     if (p)
253       *p = '\0';
254     if (gethostbyname(c)) {
255       if (p)
256         *p = ':';
257       if ((opt = strdup(c)))
258         options->fqdn = opt;
259     }
260   }
261
262   return 1;
263 }
264
265 int afp_options_parse(int ac, char **av, struct afp_options *options)
266 {
267   extern char *optarg;
268   extern int optind;
269   
270   char *p;
271   int c, err = 0;
272
273   if (gethostname(options->hostname, sizeof(options->hostname )) < 0 ) {
274     perror( "gethostname" );
275     return 0;
276   }
277     if (( p = strchr(options->hostname, '.' )) != 0 ) {
278         *p = '\0';
279     }
280
281     if (( p = strrchr( av[ 0 ], '/' )) == NULL ) {
282         p = av[ 0 ];
283     } else {
284         p++;
285     }
286
287     while (( c = getopt( ac, av, OPTIONS )) != EOF ) {
288         switch ( c ) {
289         case 'd' :
290             options->flags |= OPTION_DEBUG;
291             break;
292         case 'n' :
293             options->server = optarg;
294             break;
295         case 'f' :
296             options->defaultvol = optarg;
297             break;
298         case 's' :
299             options->systemvol = optarg;
300             break;
301         case 'u' :
302             options->flags |= OPTION_USERVOLFIRST;
303             break;
304         case 'c' :
305             options->connections = atoi( optarg );
306             break;
307         case 'g' :
308             options->guest = optarg;
309             break;
310
311         case 'P' :
312             options->pidfile = optarg;
313             break;
314
315         case 'p':
316             options->passwdbits |= PASSWD_NOSAVE;
317             break;
318         case 't':
319             options->passwdbits |= PASSWD_SET;
320             break;
321
322         case 'D':
323             options->transports &= ~AFPTRANS_DDP;
324             break;
325         case 'S':
326             options->port = atoi(optarg);
327             break;
328         case 'T':
329             options->transports &= ~AFPTRANS_TCP;
330             break;
331         case 'L':
332             options->loginmesg = optarg;
333             break;
334         case 'F':
335             options->configfile = optarg;
336             break;
337         case 'U':
338             options->uamlist = optarg;
339             break;
340         case 'I':
341             options->flags |= OPTION_CUSTOMICON;
342         default :
343             err++;
344         }
345     }
346     if ( err || optind != ac ) {
347         fprintf( stderr,
348                 "Usage:\t%s [ -dpDTIt ] [ -n nbpname ] [ -f defvols ] \
349 [ -P pidfile ] [ -s sysvols ] \n", p );
350         fprintf( stderr,
351                 "\t[ -u ] [ -c maxconn ] [ -g guest ] \
352 [ -S port ] [ -L loginmesg ] [ -F configfile ] [ -U uamlist ]\n" );
353         return 0;
354     }
355
356 #ifdef ultrix
357     openlog( p, LOG_PID );
358 #else /* ultrix */
359     openlog( p, LOG_NDELAY|LOG_PID, LOG_DAEMON);
360 #endif /* ultrix */
361
362     return 1;
363 }