2 conf.c -- configuration code
3 Copyright (C) 1998 Robert van der Meulen
4 Copyright (C) 1998,1999,2000 Ivo Timmermans <itimmermans@bigfoot.com>
5 2000 Guus Sliepen <guus@sliepen.warande.net>
6 2000 Cris van Pelt <tribbel@arise.dhs.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 $Id: conf.c,v 1.9.4.31 2000/12/01 12:38:42 zarq Exp $
36 #include <sys/types.h>
40 #include <utils.h> /* for cp */
43 #include "netutl.h" /* for strtoip */
47 config_t *config = NULL;
49 int timeout = 0; /* seconds before timeout */
50 char *confbase = NULL; /* directory in which all config files are */
51 char *netname = NULL; /* name of the vpn network */
53 /* Will be set if HUP signal is received. It will be processed when it is safe. */
57 These are all the possible configurable values
59 static internal_config_t hazahaza[] = {
60 /* Main configuration file keywords */
61 { "Name", config_name, TYPE_NAME },
62 { "ConnectTo", config_connectto, TYPE_NAME },
63 { "PingTimeout", config_pingtimeout, TYPE_INT },
64 { "TapDevice", config_tapdevice, TYPE_NAME },
65 { "PrivateKey", config_privatekey, TYPE_NAME },
66 { "KeyExpire", config_keyexpire, TYPE_INT },
67 { "Hostnames", config_hostnames, TYPE_BOOL },
68 { "Interface", config_interface, TYPE_NAME },
69 { "InterfaceIP", config_interfaceip, TYPE_IP },
70 /* Host configuration file keywords */
71 { "Address", config_address, TYPE_NAME },
72 { "Port", config_port, TYPE_INT },
73 { "PublicKey", config_publickey, TYPE_NAME },
74 { "Subnet", config_subnet, TYPE_IP }, /* Use IPv4 subnets only for now */
75 { "RestrictHosts", config_restricthosts, TYPE_BOOL },
76 { "RestrictSubnets", config_restrictsubnets, TYPE_BOOL },
77 { "RestrictAddress", config_restrictaddress, TYPE_BOOL },
78 { "RestrictPort", config_restrictport, TYPE_BOOL },
79 { "IndirectData", config_indirectdata, TYPE_BOOL },
80 { "TCPonly", config_tcponly, TYPE_BOOL },
85 Add given value to the list of configs cfg
88 add_config_val(config_t **cfg, int argtype, char *val)
93 p = (config_t*)xmalloc(sizeof(*p));
99 p->data.val = strtol(val, &q, 0);
104 p->data.ptr = xmalloc(strlen(val) + 1);
105 strcpy(p->data.ptr, val);
108 p->data.ip = strtoip(val);
111 if(!strcasecmp("yes", val))
112 p->data.val = stupid_true;
113 else if(!strcasecmp("no", val))
114 p->data.val = stupid_false;
119 p->argtype = argtype;
137 Read exactly one line and strip the trailing newline if any. If the
138 file was on EOF, return NULL. Otherwise, return all the data in a
139 dynamically allocated buffer.
141 If line is non-NULL, it will be used as an initial buffer, to avoid
142 unnecessary mallocing each time this function is called. If buf is
143 given, and buf needs to be expanded, the var pointed to by buflen
146 char *readline(FILE *fp, char *buf, size_t *buflen)
148 char *newline = NULL;
150 char *line; /* The array that contains everything that has been read
152 char *idx; /* Read into this pointer, which points to an offset
154 size_t size, newsize; /* The size of the current array pointed to by
156 size_t maxlen; /* Maximum number of characters that may be read with
157 fgets. This is newsize - oldsize. */
162 if((buf != NULL) && (buflen != NULL))
170 line = xmalloc(size);
179 p = fgets(idx, maxlen, fp);
180 if(p == NULL) /* EOF or error */
185 /* otherwise: error; let the calling function print an error
186 message if applicable */
191 newline = strchr(p, '\n');
193 /* We haven't yet read everything to the end of the line */
196 line = xrealloc(line, newsize);
197 idx = &line[size - 1];
198 maxlen = newsize - size + 1;
203 *newline = '\0'; /* kill newline */
208 if((buf != NULL) && (buflen != NULL))
214 Parse a configuration file and put the results in the configuration tree
217 int read_config_file(config_t **base, const char *fname)
228 if((fp = fopen (fname, "r")) == NULL)
232 buffer = xmalloc(bufsize);
236 if((line = readline(fp, buffer, &bufsize)) == NULL)
250 if((p = strtok(line, "\t =")) == NULL)
251 continue; /* no tokens on this line */
254 continue; /* comment: ignore */
256 for(i = 0; hazahaza[i].name != NULL; i++)
257 if(!strcasecmp(hazahaza[i].name, p))
260 if(!hazahaza[i].name)
262 syslog(LOG_ERR, _("Invalid variable name `%s' on line %d while reading config file %s"),
267 if(((q = strtok(NULL, "\t\n\r =")) == NULL) || q[0] == '#')
269 fprintf(stderr, _("No value for variable `%s' on line %d while reading config file %s"),
270 hazahaza[i].name, lineno, fname);
274 cfg = add_config_val(base, hazahaza[i].argtype, q);
277 fprintf(stderr, _("Invalid value for variable `%s' on line %d while reading config file %s"),
278 hazahaza[i].name, lineno, fname);
282 cfg->which = hazahaza[i].which;
293 int read_server_config()
298 asprintf(&fname, "%s/tinc.conf", confbase);
299 x = read_config_file(&config, fname);
302 fprintf(stderr, _("Failed to read `%s': %m\n"),
311 Look up the value of the config option type
313 const config_t *get_config_val(config_t *p, which_t type)
316 for(; p != NULL; p = p->next)
324 Remove the complete configuration tree.
326 void clear_config(config_t **base)
330 for(p = *base; p != NULL; p = next)
333 if(p->data.ptr && (p->argtype == TYPE_NAME))
343 int isadir(const char* f)
349 fprintf(stderr, _("Couldn't stat `%s': %m\n"),
354 return S_ISDIR(s.st_mode);
357 int is_safe_path(const char *file)
362 p = strrchr(file, '/');
363 assert(p); /* p has to contain a / */
365 if(stat(file, &s) < 0)
367 fprintf(stderr, _("Couldn't stat `%s': %m\n"),
371 if(s.st_uid != geteuid())
373 fprintf(stderr, _("`%s' is owned by UID %d instead of %d.\n"),
374 file, s.st_uid, geteuid());
377 if(S_ISLNK(s.st_mode))
379 fprintf(stderr, _("Warning: `%s' is a symlink\n"),
381 /* fixme: read the symlink and start again */
385 if(stat(file, &s) < 0 && errno != ENOENT)
387 fprintf(stderr, _("Couldn't stat `%s': %m\n"),
393 if(s.st_uid != geteuid())
395 fprintf(stderr, _("`%s' is owned by UID %d instead of %d.\n"),
396 file, s.st_uid, geteuid());
399 if(S_ISLNK(s.st_mode))
401 fprintf(stderr, _("Warning: `%s' is a symlink\n"),
403 /* fixme: read the symlink and start again */
407 /* Accessible by others */
408 fprintf(stderr, _("`%s' has unsecure permissions.\n"),
416 FILE *ask_and_safe_open(const char* filename, const char* what)
423 /* Check stdin and stdout */
424 if(!isatty(0) || !isatty(1))
426 /* Argh, they are running us from a script or something. Write
427 the files to the current directory and let them burn in hell
429 fn = xstrdup(filename);
433 /* Ask for a file and/or directory name. */
434 fprintf(stdout, _("Please enter a file to save %s to [%s]: "),
436 fflush(stdout); /* Don't wait for a newline */
437 if((fn = readline(stdin, NULL, NULL)) == NULL)
439 fprintf(stderr, _("Error while reading stdin: %m\n"));
443 /* User just pressed enter. */
444 fn = xstrdup(filename);
447 if((strchr(fn, '/') == NULL) || (fn[0] != '/'))
449 /* The directory is a relative path or a filename. */
452 directory = get_current_dir_name();
453 len = strlen(fn) + strlen(directory) + 2; /* 1 for the / */
455 snprintf(p, len, "%s/%s", directory, fn);
461 if(isadir(fn) > 0) /* -1 is error */
465 len = strlen(fn) + strlen(filename) + 2; /* 1 for the / */
467 snprintf(p, len, "%s/%s", fn, filename);
472 umask(0077); /* Disallow everything for group and other */
474 /* Open it first to keep the inode busy */
475 if((r = fopen(fn, "w")) == NULL)
477 fprintf(stderr, _("Error opening file `%s': %m\n"),
483 /* Then check the file for nasty attacks */
484 if(!is_safe_path(fn)) /* Do not permit any directories that are
485 readable or writeable by other users. */
487 fprintf(stderr, _("The file `%s' (or any of the leading directories) has unsafe permissions.\n"
488 "I will not create or overwrite this file.\n"),