2 * $Id: afp_options.c,v 1.41 2009-03-31 11:40:26 franklahm Exp $
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.
8 * modified from main.c. this handles afp options.
13 #endif /* HAVE_CONFIG_H */
21 #else /* STDC_HEADERS */
25 #endif /* HAVE_STRCHR */
26 char *strchr (), *strrchr ();
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 */
36 #endif /* HAVE_UNISTD_H */
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <atalk/logger.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
45 #endif /* HAVE_NETDB_H */
47 #include <atalk/paths.h>
48 #include <atalk/util.h>
53 #include <atalk/compat.h>
57 #include <sys/types.h>
58 #endif /* ADMIN_GRP */
61 #define MIN(a, b) ((a) < (b) ? (a) : (b))
65 char Cnid_srv[MAXHOSTNAMELEN + 1] = "localhost";
68 #define OPTIONS "dn:f:s:uc:g:P:ptDS:TL:F:U:hIvVm:"
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
74 static char *getoption(char *buf, const char *option)
76 static char string[LENGTH + 1];
80 if (option && (buf = strstr(buf, option)))
81 buf = strpbrk(buf, " \t");
83 while (buf && isspace(*buf))
89 /* search for any quoted stuff */
90 if (*buf == '"' && (end = strchr(buf + 1, '"'))) {
92 len = MIN(end - buf, LENGTH);
93 } else if ((end = strpbrk(buf, " \t\n"))) /* option or eoln */
94 len = MIN(end - buf, LENGTH);
96 len = MIN(strlen(buf), LENGTH);
98 strncpy(string, buf, len);
103 /* get rid of any allocated afp_option buffers. */
104 void afp_options_free(struct afp_options *opt,
105 const struct afp_options *save)
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);
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);
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);
122 if (opt->loginmesg && (opt->loginmesg != save->loginmesg))
123 free(opt->loginmesg);
124 if (opt->guest && (opt->guest != save->guest))
126 if (opt->server && (opt->server != save->server))
128 if (opt->ipaddr && (opt->ipaddr != save->ipaddr))
130 if (opt->fqdn && (opt->fqdn != save->fqdn))
132 if (opt->uampath && (opt->uampath != save->uampath))
134 if (opt->uamlist && (opt->uamlist != save->uamlist))
136 if (opt->passwdfile && (opt->passwdfile != save->passwdfile))
137 free(opt->passwdfile);
138 if (opt->signature && (opt->signature != save->signature))
139 free(opt->signature);
140 if (opt->k5service && (opt->k5service != save->k5service))
141 free(opt->k5service);
142 if (opt->k5realm && (opt->k5realm != save->k5realm))
144 if (opt->k5keytab && (opt->k5keytab != save->k5keytab))
146 if (opt->unixcodepage && (opt->unixcodepage != save->unixcodepage))
147 free(opt->unixcodepage);
148 if (opt->maccodepage && (opt->maccodepage != save->maccodepage))
149 free(opt->maccodepage);
151 if (opt->ntdomain && (opt->ntdomain != save->ntdomain))
153 if (opt->ntseparator && (opt->ntseparator != save->ntseparator))
154 free(opt->ntseparator);
157 /* initialize options */
158 void afp_options_init(struct afp_options *options)
160 memset(options, 0, sizeof(struct afp_options));
161 options->connections = 20;
162 options->pidfile = _PATH_AFPDLOCK;
163 options->defaultvol.name = _PATH_AFPDDEFVOL;
164 options->systemvol.name = _PATH_AFPDSYSVOL;
165 options->configfile = _PATH_AFPDCONF;
166 options->uampath = _PATH_AFPDUAMPATH;
167 options->uamlist = "uams_clrtxt.so,uams_dhx.so";
168 options->guest = "nobody";
169 options->loginmesg = "";
170 options->transports = AFPTRANS_ALL;
171 options->passwdfile = _PATH_AFPDPWFILE;
172 options->tickleval = 30;
173 options->timeout = 4;
174 options->sleep = 10* 120; /* 10 h in 30 seconds tick */
175 options->server_notif = 1;
176 options->authprintdir = NULL;
177 options->signature = "host";
180 options->admingid = 0;
181 #endif /* ADMIN_GRP */
182 options->k5service = NULL;
183 options->k5realm = NULL;
184 options->k5keytab = NULL;
185 options->unixcharset = CH_UNIX;
186 options->unixcodepage = "LOCALE";
187 options->maccharset = CH_MAC;
188 options->maccodepage = "MAC_ROMAN";
189 options->volnamelen = 80; /* spec: 255, 10.1: 73, 10.4/10.5: 80 */
190 options->ntdomain = NULL;
191 options->ntseparator = NULL;
194 /* parse an afpd.conf line. i'm doing it this way because it's
195 * easy. it is, however, massively hokey. sample afpd.conf:
196 * server:AFPServer@zone -loginmesg "blah blah blah" -nodsi
197 * "private machine"@zone2 -noguest -port 11012
198 * server2 -nocleartxt -nodsi
200 * NOTE: this ignores unknown options
202 int afp_options_parseline(char *buf, struct afp_options *options)
207 if (*buf != '-' && (c = getoption(buf, NULL)) && (opt = strdup(c)))
208 options->server = opt;
211 if (strstr(buf, " -nodebug"))
212 options->flags &= ~OPTION_DEBUG;
214 if (strstr(buf, " -noslp"))
215 options->flags |= OPTION_NOSLP;
216 #endif /* USE_SRVLOC */
218 if (strstr(buf, " -nouservolfirst"))
219 options->flags &= ~OPTION_USERVOLFIRST;
220 if (strstr(buf, " -uservolfirst"))
221 options->flags |= OPTION_USERVOLFIRST;
222 if (strstr(buf, " -nouservol"))
223 options->flags |= OPTION_NOUSERVOL;
224 if (strstr(buf, " -uservol"))
225 options->flags &= ~OPTION_NOUSERVOL;
226 if (strstr(buf, " -proxy"))
227 options->flags |= OPTION_PROXY;
228 if (strstr(buf, " -noicon"))
229 options->flags &= ~OPTION_CUSTOMICON;
230 if (strstr(buf, " -icon"))
231 options->flags |= OPTION_CUSTOMICON;
232 if (strstr(buf, " -advertise_ssh"))
233 options->flags |= OPTION_ANNOUNCESSH;
236 if (strstr(buf, " -nosavepassword"))
237 options->passwdbits |= PASSWD_NOSAVE;
238 if (strstr(buf, " -savepassword"))
239 options->passwdbits &= ~PASSWD_NOSAVE;
240 if (strstr(buf, " -nosetpassword"))
241 options->passwdbits &= ~PASSWD_SET;
242 if (strstr(buf, " -setpassword"))
243 options->passwdbits |= PASSWD_SET;
246 if (strstr(buf, " -transall"))
247 options->transports = AFPTRANS_ALL;
248 if (strstr(buf, " -notransall"))
249 options->transports = AFPTRANS_NONE;
250 if (strstr(buf, " -tcp"))
251 options->transports |= AFPTRANS_TCP;
252 if (strstr(buf, " -notcp"))
253 options->transports &= ~AFPTRANS_TCP;
254 if (strstr(buf, " -ddp"))
255 options->transports |= AFPTRANS_DDP;
256 if (strstr(buf, " -noddp"))
257 options->transports &= ~AFPTRANS_DDP;
258 if (strstr(buf, "-client_polling"))
259 options->server_notif = 0;
261 /* figure out options w/ values. currently, this will ignore the setting
262 * if memory is lacking. */
264 if ((c = getoption(buf, "-hostname"))) {
265 int len = strlen (c);
266 if (len <= MAXHOSTNAMELEN) {
267 memcpy(options->hostname, c, len);
268 options->hostname[len] = 0;
271 LOG(log_info, logtype_afpd, "WARNING: hostname %s is too long (%d)",c,len);
274 if ((c = getoption(buf, "-defaultvol")) && (opt = strdup(c)))
275 options->defaultvol.name = opt;
276 if ((c = getoption(buf, "-systemvol")) && (opt = strdup(c)))
277 options->systemvol.name = opt;
278 if ((c = getoption(buf, "-loginmesg")) && (opt = strdup(c)))
279 options->loginmesg = opt;
280 if ((c = getoption(buf, "-guestname")) && (opt = strdup(c)))
281 options->guest = opt;
282 if ((c = getoption(buf, "-passwdfile")) && (opt = strdup(c)))
283 options->passwdfile = opt;
284 if ((c = getoption(buf, "-passwdminlen")))
285 options->passwdminlen = MIN(1, atoi(c));
286 if ((c = getoption(buf, "-loginmaxfail")))
287 options->loginmaxfail = atoi(c);
288 if ((c = getoption(buf, "-tickleval"))) {
289 options->tickleval = atoi(c);
290 if (options->tickleval <= 0) {
291 options->tickleval = 30;
294 if ((c = getoption(buf, "-timeout"))) {
295 options->timeout = atoi(c);
296 if (options->timeout <= 0) {
297 options->timeout = 4;
301 if ((c = getoption(buf, "-sleep"))) {
302 options->sleep = atoi(c) *120;
303 if (options->sleep <= 4) {
308 if ((c = getoption(buf, "-server_quantum")))
309 options->server_quantum = strtoul(c, NULL, 0);
311 if ((c = getoption(buf, "-volnamelen"))) {
312 options->volnamelen = atoi(c);
313 if (options->volnamelen < 8) {
314 options->volnamelen = 8; /* max mangled volname "???#FFFF" */
316 if (options->volnamelen > 255) {
317 options->volnamelen = 255; /* AFP3 spec */
321 /* -[no]setuplog <logtype> <loglevel> [<filename>]*/
322 if ((c = getoption(buf, "-setuplog")))
325 if ((c = getoption(buf, "-unsetuplog")))
329 if ((c = getoption(buf, "-admingroup"))) {
330 struct group *gr = getgrnam(c);
332 options->admingid = gr->gr_gid;
335 #endif /* ADMIN_GRP */
337 if ((c = getoption(buf, "-k5service")) && (opt = strdup(c)))
338 options->k5service = opt;
339 if ((c = getoption(buf, "-k5realm")) && (opt = strdup(c)))
340 options->k5realm = opt;
341 if ((c = getoption(buf, "-k5keytab"))) {
342 if ( NULL == (options->k5keytab = (char *) malloc(sizeof(char)*(strlen(c)+14)) )) {
343 LOG(log_error, logtype_afpd, "malloc failed");
346 snprintf(options->k5keytab, strlen(c)+14, "KRB5_KTNAME=%s", c);
347 putenv(options->k5keytab);
348 /* setenv( "KRB5_KTNAME", c, 1 ); */
350 if ((c = getoption(buf, "-authprintdir")) && (opt = strdup(c)))
351 options->authprintdir = opt;
352 if ((c = getoption(buf, "-uampath")) && (opt = strdup(c)))
353 options->uampath = opt;
354 if ((c = getoption(buf, "-uamlist")) && (opt = strdup(c)))
355 options->uamlist = opt;
357 if ((c = getoption(buf, "-ipaddr"))) {
358 struct in_addr inaddr;
359 if (inet_aton(c, &inaddr) && (opt = strdup(c))) {
360 if (!gethostbyaddr((const char *) &inaddr, sizeof(inaddr), AF_INET))
361 LOG(log_info, logtype_afpd, "WARNING: can't find %s", opt);
362 options->ipaddr = opt;
365 LOG(log_error, logtype_afpd, "Error parsing -ipaddr, is %s in numbers-and-dots notation?", c);
369 /* FIXME CNID Cnid_srv is a server attribute */
370 if ((c = getoption(buf, "-cnidserver"))) {
374 if (p != NULL && (len = p - c) <= MAXHOSTNAMELEN) {
375 memcpy(Cnid_srv, c, len);
377 Cnid_port = atoi(p +1);
381 if ((c = getoption(buf, "-port")))
382 options->port = atoi(c);
383 if ((c = getoption(buf, "-ddpaddr")))
384 atalk_aton(c, &options->ddpaddr);
385 if ((c = getoption(buf, "-signature")) && (opt = strdup(c)))
386 options->signature = opt;
388 /* do a little checking for the domain name. */
389 if ((c = getoption(buf, "-fqdn"))) {
390 char *p = strchr(c, ':');
393 if (gethostbyname(c)) {
396 if ((opt = strdup(c)))
400 LOG(log_error, logtype_afpd, "error parsing -fqdn, gethostbyname failed for: %s", c);
404 if ((c = getoption(buf, "-unixcodepage"))) {
405 if ((charset_t)-1 == ( options->unixcharset = add_charset(c)) ) {
406 options->unixcharset = CH_UNIX;
407 LOG(log_warning, logtype_afpd, "setting Unix codepage to '%s' failed", c);
410 if ((opt = strdup(c)))
411 options->unixcodepage = opt;
415 if ((c = getoption(buf, "-maccodepage"))) {
416 if ((charset_t)-1 == ( options->maccharset = add_charset(c)) ) {
417 options->maccharset = CH_MAC;
418 LOG(log_warning, logtype_afpd, "setting Mac codepage to '%s' failed", c);
421 if ((opt = strdup(c)))
422 options->maccodepage = opt;
426 if ((c = strstr(buf, "-closevol"))) {
427 options->closevol= 1;
430 if ((c = getoption(buf, "-ntdomain")) && (opt = strdup(c)))
431 options->ntdomain = opt;
433 if ((c = getoption(buf, "-ntseparator")) && (opt = strdup(c)))
434 options->ntseparator = opt;
440 * Show version information about afpd.
445 printf( "afpd %s - Apple Filing Protocol (AFP) daemon of Netatalk\n\n", VERSION );
447 puts( "This program is free software; you can redistribute it and/or modify it under" );
448 puts( "the terms of the GNU General Public License as published by the Free Software" );
449 puts( "Foundation; either version 2 of the License, or (at your option) any later" );
450 puts( "version. Please see the file COPYING for further information and details.\n" );
452 puts( "afpd has been compiled with support for these features:\n" );
454 printf( " AFP3.1 support:\t" );
461 printf( " Transport layers:\t" );
465 puts( "TCP/IP DDP" );
468 printf( " CNID backends:\t" );
469 #ifdef CNID_BACKEND_CDB
472 #ifdef CNID_BACKEND_DB3
475 #ifdef CNID_BACKEND_DBD
476 #ifdef CNID_BACKEND_DBD_TXN
477 printf( "dbd-txn " );
482 #ifdef CNID_BACKEND_HASH
485 #ifdef CNID_BACKEND_LAST
488 #ifdef CNID_BACKEND_MTAB
491 #ifdef CNID_BACKEND_TDB
498 * Show extended version information about afpd and Netatalk.
501 void show_version_extended( )
505 printf( " SLP support:\t" );
512 printf( " TCP wrappers support:\t" );
519 printf( " Quota support:\t" );
520 #ifndef NO_QUOTA_SUPPORT
526 printf( " Admin group support:\t" );
533 printf( " Valid shell checks:\t" );
534 #ifndef DISABLE_SHELLCHECK
540 printf( " cracklib support:\t" );
547 printf( " Dropbox kludge:\t" );
554 printf( " Force volume uid/gid:\t" );
563 * Display compiled-in default paths
565 void show_paths( void )
567 printf( " afpd.conf:\t%s\n", _PATH_AFPDCONF );
568 printf( " AppleVolumes.system:\t%s\n", _PATH_AFPDSYSVOL );
569 printf( " AppleVolumes.default:\t%s\n", _PATH_AFPDDEFVOL );
570 printf( " UAM search path:\t%s\n", _PATH_AFPDUAMPATH );
574 * Display usage information about adpd.
576 void show_usage( char *name )
578 fprintf( stderr, "Usage:\t%s [-dDIptTu] [-c maxconnections] [-f defaultvolumes] [-F config]\n", name );
579 fprintf( stderr, "\t [-g guest] [-L message] [-m umask][-n nbpname] [-P pidfile]\n" );
580 fprintf( stderr, "\t [-s systemvolumes] [-S port] [-U uams]\n" );
581 fprintf( stderr, "\t%s -h|-v|-V\n", name );
584 int afp_options_parse(int ac, char **av, struct afp_options *options)
590 char *tmp; /* Used for error checking the result of strtol */
593 if (gethostname(options->hostname, sizeof(options->hostname )) < 0 ) {
594 perror( "gethostname" );
597 if (NULL != ( p = strchr(options->hostname, '.' )) ) {
601 if (NULL == ( p = strrchr( av[ 0 ], '/' )) ) {
607 while (EOF != ( c = getopt( ac, av, OPTIONS )) ) {
610 options->flags |= OPTION_DEBUG;
613 options->server = optarg;
616 options->defaultvol.name = optarg;
619 options->systemvol.name = optarg;
622 options->flags |= OPTION_USERVOLFIRST;
625 options->connections = atoi( optarg );
628 options->guest = optarg;
632 options->pidfile = optarg;
636 options->passwdbits |= PASSWD_NOSAVE;
639 options->passwdbits |= PASSWD_SET;
643 options->transports &= ~AFPTRANS_DDP;
646 options->port = atoi(optarg);
649 options->transports &= ~AFPTRANS_TCP;
652 options->loginmesg = optarg;
655 options->configfile = optarg;
658 options->uamlist = optarg;
660 case 'v': /* version */
661 show_version( ); puts( "" );
662 show_paths( ); puts( "" );
665 case 'V': /* extended version */
666 show_version_extended( ); puts( "" );
667 show_paths( ); puts( "" );
670 case 'h': /* usage */
675 options->flags |= OPTION_CUSTOMICON;
678 options->umask = strtoul(optarg, &tmp, 8);
679 if ((options->umask > 0777)) {
680 fprintf(stderr, "%s: out of range umask setting provided\n", p);
683 if (tmp[0] != '\0') {
684 fprintf(stderr, "%s: invalid characters in umask setting provided\n", p);
692 if ( err || optind != ac ) {
698 openlog( p, LOG_PID ); /* ultrix only */