LCR now runs as a user.
[lcr.git] / options.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** reading options.conf and filling structure                                **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "stdio.h"
13 #include "string.h"
14 #include "stdarg.h"
15 #include "unistd.h"
16 #include "stdlib.h"
17 #include "macro.h"
18 #include "extension.h"
19 #include "options.h"
20
21 struct options options = {
22         "/usr/local/lcr/log",           /* log file */
23         0x0000,                         /* debug mode */
24         'a',                            /* a-law */
25         "0",                            /* national prefix */
26         "00",                           /* international prefix */
27         "tones_american",               /* directory of tones */
28         "",                             /* directories of tones to fetch */
29         "extensions",                   /* directory of extensions */
30         "",                             /* dummy caller id */
31         0,                              /* use tones by dsp.o */
32         0,                              /* by default use priority 0 */
33         "lcr@your.machine",             /* source mail adress */
34         "/var/tmp",                     /* path of lock files */
35         0700                            /* rights of lcr admin socket */
36 };
37
38 char options_error[256];
39
40 /* read options
41  *
42  * read options from options.conf
43  */
44 int read_options(void)
45 {
46         FILE *fp=NULL;
47         char filename[128];
48         char *p;
49         char option[32];
50         char param[256];
51         unsigned int line,i;
52         char buffer[256];
53
54         SPRINT(filename, "%s/options.conf", INSTALL_DATA);
55
56         if (!(fp=fopen(filename,"r")))
57         {
58                 SPRINT(options_error, "Cannot open %s\n",filename);
59                 return(0);
60         }
61
62         line=0;
63         while((fgets(buffer,sizeof(buffer),fp)))
64         {
65                 line++;
66                 buffer[sizeof(buffer)-1]=0;
67                 if (buffer[0]) buffer[strlen(buffer)-1]=0;
68                 p=buffer;
69
70                 while(*p <= 32) /* skip spaces */
71                 {
72                         if (*p == 0)
73                                 break;
74                         p++;
75                 }
76                 if (*p==0 || *p=='#') /* ignore comments and empty line */
77                         continue;
78
79                 option[0]=0;
80                 i=0; /* read option */
81                 while(*p > 32)
82                 {
83                         if (i+1 >= sizeof(option))
84                         {
85                                 SPRINT(options_error, "Error in %s (line %d): option too long.\n",filename,line);
86                                 goto error;
87                         }
88                         option[i+1] = '\0';
89                         option[i++] = *p++;
90                 }
91
92                 while(*p <= 32) /* skip spaces */
93                 {
94                         if (*p == 0)
95                                 break;
96                         p++;
97                 }
98
99                 param[0]=0;
100                 if (*p!=0 && *p!='#') /* param */
101                 {
102                         i=0; /* read param */
103                         while(*p > 31)
104                         {
105                                 if (i+1 >= sizeof(param))
106                                 {
107                                         SPRINT(options_error, "Error in %s (line %d): param too long.\n",filename,line);
108                                         goto error;
109                                 }
110                                 param[i+1] = '\0';
111                                 param[i++] = *p++;
112                         }
113                 }
114
115                 /* at this point we have option and param */
116
117                 /* check option */
118                 if (!strcmp(option,"nt_if") || !strcmp(option,"te_if"))
119                 {
120                         SPRINT(options_error, "Error in %s (line %d): obsolete option %s. Use multiple 'port' options to define ports to use.\n",filename,line,option);
121                         goto error;
122                 } else
123                 if (!strcmp(option,"debug"))
124                 {
125                         if (param[0]==0)
126                         {
127                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
128                                 goto error;
129                         }
130                         options.deb = strtol(param, NULL, 0);
131
132                 } else
133                 if (!strcmp(option,"log"))
134                 {
135                         if (param[0]==0)
136                         {
137                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
138                                 goto error;
139                         }
140                         SCPY(options.log, param);
141
142                 } else
143                 if (!strcmp(option,"alaw"))
144                 {
145                         options.law = 'a';
146
147                 } else
148                 if (!strcmp(option,"ulaw"))
149                 {
150                         options.law = 'u';
151
152                 } else
153                 if (!strcmp(option,"tones_dir"))
154                 {
155                         if (param[0]==0)
156                         {
157                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
158                                 goto error;
159                         }
160                         if (param[strlen(param)-1] == '/')
161                                 param[strlen(param)-1]=0;
162                         SCPY(options.tones_dir, param);
163
164                 } else
165                 if (!strcmp(option,"fetch_tones"))
166                 {
167                         if (param[0]==0)
168                         {
169                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
170                                 goto error;
171                         }
172                         if (param[strlen(param)-1] == '/')
173                                 param[strlen(param)-1]=0;
174                         SCPY(options.fetch_tones, param);
175
176                 } else
177                 if (!strcmp(option,"extensions_dir"))
178                 {
179                         if (param[0]==0)
180                         {
181                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
182                                 goto error;
183                         }
184                         if (param[strlen(param)-1] == '/')
185                                 param[strlen(param)-1]=0;
186                         SCPY(options.extensions_dir, param);
187
188                 } else
189                 if (!strcmp(option,"national"))
190                 {
191                         SCPY(options.national, param);
192
193                 } else
194                 if (!strcmp(option,"international"))
195                 {
196                         SCPY(options.international, param);
197
198                 } else
199                 if (!strcmp(option,"dummyid"))
200                 {
201                         SCPY(options.dummyid, param);
202
203                 } else
204                 if (!strcmp(option,"dsptones"))
205                 {
206                         if (!strcasecmp(param, "american"))
207                                 options.dsptones = DSP_AMERICAN;
208                         else if (!strcasecmp(param, "german"))
209                                 options.dsptones = DSP_GERMAN;
210                         else if (!strcasecmp(param, "oldgerman"))
211                                 options.dsptones = DSP_OLDGERMAN;
212                         else if (!strcasecmp(param, "none"))
213                                 options.dsptones = DSP_NONE;
214                         else {
215                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
216                                 goto error;
217                         }
218
219                 } else
220                 if (!strcmp(option,"schedule"))
221                 {
222                         options.schedule = atoi(param);
223                         if (options.schedule < 0)
224                         {
225                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s must be at least '0'.\n", filename,line,option);
226                                 goto error;
227                         }
228                         if (options.schedule > 99)
229                         {
230                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s must be '99' or less.\n", filename,line,option);
231                                 goto error;
232                         }
233
234                 } else
235                 if (!strcmp(option,"email"))
236                 {
237                         if (param[0]==0)
238                         {
239                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n", filename,line,option);
240                                 goto error;
241                         }
242                         SCPY(options.email, param);
243
244                 } else
245                 if (!strcmp(option,"lock"))
246                 {
247                         if (param[0]==0)
248                         {
249                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
250                                 goto error;
251                         }
252                         if (param[strlen(param)-1] == '/')
253                                 param[strlen(param)-1]=0;
254                         SCPY(options.lock, param);
255
256                 } else
257                 if (!strcmp(option,"socketrights"))
258                 {
259                         options.socketrights = strtol(param, NULL, 0);
260                 } else
261                 {
262                         SPRINT(options_error, "Error in %s (line %d): wrong option keyword %s.\n", filename,line,option);
263                         goto error;
264                 }
265         }
266
267 #if 0
268         if (!options.dsptones)
269         {
270                 SPRINT(options_error, "Error in %s (line %d): option 'dsptones' missing.\n", filename);
271                 goto error;
272         }
273 #endif
274         if (!options.tones_dir[0])
275         {
276                 SPRINT(options_error, "Error in %s (line %d): option 'tones_dir' with parameter missing.\n", filename);
277                 goto error;
278         }
279         if (fp) fclose(fp);
280         return(1);
281 error:
282         if (fp) fclose(fp);
283         return(0);
284 }
285
286