Kill the parent after any error conditions in detach().
[tinc] / src / conf.c
1 /*
2     conf.c -- configuration code
3     Copyright (C) 1998 Emphyrio,
4     Copyright (C) 1998,99 Ivo Timmermans <zarq@iname.com>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /* foute config read code, GPL, emphyrio 1998 */
22 /* Mutilated by me -- Ivo */
23
24 #include "config.h"
25
26 #include <ctype.h>
27 #include <errno.h>
28 #include <netdb.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <xalloc.h>
34
35 #include "conf.h"
36 #include "netutl.h" /* for strtoip */
37
38 config_t *config;
39 int debug_lvl = 0;
40 int timeout = 0; /* seconds before timeout */
41
42 typedef struct internal_config_t {
43   char *name;
44   enum which_t which;
45   int argtype;
46 } internal_config_t;
47
48 /*
49   These are all the possible configurable values
50 */
51 static internal_config_t hazahaza[] = {
52   { "AllowConnect", allowconnect,   TYPE_BOOL },
53   { "ConnectTo",    upstreamip,     TYPE_IP },
54   { "ConnectPort",  upstreamport,   TYPE_INT },
55   { "ListenPort",   listenport,     TYPE_INT },
56   { "MyOwnVPNIP",   myvpnip,        TYPE_IP },
57   { "MyVirtualIP",  myvpnip,        TYPE_IP },   /* an alias */
58   { "Passphrases",  passphrasesdir, TYPE_NAME },
59   { "PingTimeout",  pingtimeout,    TYPE_INT },
60   { "TapDevice",    tapdevice,      TYPE_NAME },
61   { "KeyExpire",    keyexpire,      TYPE_INT },
62   { NULL, 0, 0 }
63 };
64
65 /*
66   Add given value to the list of configs cfg
67 */
68 config_t *
69 add_config_val(config_t **cfg, int argtype, char *val)
70 {
71   config_t *p;
72   char *q;
73
74   p = (config_t*)xmalloc(sizeof(config_t));
75   p->data.val = 0;
76   
77   switch(argtype)
78     {
79     case TYPE_INT:
80       p->data.val = strtol(val, &q, 0);
81       if(q && *q)
82         p->data.val = 0;
83       break;
84     case TYPE_NAME:
85       p->data.ptr = xmalloc(strlen(val) + 1);
86       strcpy(p->data.ptr, val);
87       break;
88     case TYPE_IP:
89       p->data.ip = strtoip(val);
90       break;
91     case TYPE_BOOL:
92       if(!strcasecmp("yes", val))
93         p->data.val = stupid_true;
94       else if(!strcasecmp("no", val))
95         p->data.val = stupid_false;
96       else
97         p->data.val = 0;
98     }
99
100   if(p->data.val)
101     {
102       p->next = *cfg;
103       *cfg = p;
104       return p;
105     }
106
107   free(p);
108   return NULL;
109 }
110
111 /*
112   Get variable from a section in a configfile. returns -1 on failure.
113 */
114 int
115 readconfig(const char *fname, FILE *fp)
116 {
117   char line[81];
118   char *p, *q;
119   int i, lineno = 0;
120   config_t *cfg;
121
122   for(;;)
123     {
124       if(fgets(line, 80, fp) == NULL)
125         return 0;
126       lineno++;
127
128       if((p = strtok(line, "\t\n\r =")) == NULL)
129         continue; /* no tokens on this line */
130
131       if(p[0] == '#')
132         continue; /* comment: ignore */
133
134       for(i = 0; hazahaza[i].name != NULL; i++)
135         if(!strcasecmp(hazahaza[i].name, p))
136           break;
137
138       if(!hazahaza[i].name)
139         {
140           fprintf(stderr, "%s: %d: Invalid variable name `%s'.\n",
141                   fname, lineno, p);
142           return -1;
143         }
144
145       if(((q = strtok(NULL, "\t\n\r =")) == NULL) || q[0] == '#')
146         {
147           fprintf(stderr, "%s: %d: No value given for `%s'.\n",
148                   fname, lineno, hazahaza[i].name);
149           return -1;
150         }
151
152       cfg = add_config_val(&config, hazahaza[i].argtype, q);
153       if(cfg == NULL)
154         {
155           fprintf(stderr, "%s: %d: Invalid value `%s' for variable `%s'.\n",
156                   fname, lineno, q, hazahaza[i].name);
157           return -1;
158         }
159
160       cfg->which = hazahaza[i].which;
161       if(!config)
162         config = cfg;
163     }
164 }
165
166 /*
167   wrapper function for readconfig
168 */
169 int
170 read_config_file(const char *fname)
171 {
172   FILE *fp;
173
174   if((fp = fopen (fname, "r")) == NULL)
175     {
176       fprintf(stderr, "Could not open %s: %s\n", fname, sys_errlist[errno]);
177       return 1;
178     }
179
180   if(readconfig(fname, fp))
181     return -1;
182
183   fclose (fp);
184
185   return 0;
186 }
187
188 /*
189   Look up the value of the config option type
190 */
191 const config_t *
192 get_config_val(which_t type)
193 {
194   config_t *p;
195
196   for(p = config; p != NULL; p = p->next)
197     if(p->which == type)
198       return p;
199
200   /* Not found */
201   return NULL;
202 }
203