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