88f64246fe932a027736e617ffc787e75168f908
[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 #include <grp.h>
21 #include <pwd.h>
22
23 struct options options = {
24         "/usr/local/lcr/log",           /* log file */
25         0x0000,                         /* debug mode */
26         'a',                            /* a-law */
27         "0",                            /* national prefix */
28         "00",                           /* international prefix */
29         "tones_american",               /* directory of tones */
30         "",                             /* directories of tones to fetch */
31         "",                             /* dummy caller id */
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         -1,                             /* socket user (-1= no change) */
37         -1,                             /* socket group (-1= no change) */
38         0,                              /* enable gsm */
39         1                               /* use polling of main loop */
40 };
41
42 char options_error[256];
43
44 /* read options
45  *
46  * read options from options.conf
47  */
48 int read_options(char *options_error)
49 {
50         FILE *fp=NULL;
51         char filename[128];
52         char *p;
53         char option[32];
54         char param[256];
55         unsigned int line,i;
56         char buffer[256];
57
58         SPRINT(filename, "%s/options.conf", CONFIG_DATA);
59
60         if (!(fp=fopen(filename,"r"))) {
61                 UPRINT(options_error, "Cannot open %s\n",filename);
62                 return(0);
63         }
64
65         line=0;
66         while((fgets(buffer,sizeof(buffer),fp))) {
67                 line++;
68                 buffer[sizeof(buffer)-1]=0;
69                 if (buffer[0]) buffer[strlen(buffer)-1]=0;
70                 p=buffer;
71
72                 while(*p <= 32) { /* skip spaces */
73                         if (*p == 0)
74                                 break;
75                         p++;
76                 }
77                 if (*p==0 || *p=='#') /* ignore comments and empty line */
78                         continue;
79
80                 option[0]=0;
81                 i=0; /* read option */
82                 while(*p > 32) {
83                         if (i+1 >= sizeof(option)) {
84                                 UPRINT(options_error, "Error in %s (line %d): option too long.\n",filename,line);
85                                 goto error;
86                         }
87                         option[i+1] = '\0';
88                         option[i++] = *p++;
89                 }
90
91                 while(*p <= 32) { /* skip spaces */
92                         if (*p == 0)
93                                 break;
94                         p++;
95                 }
96
97                 param[0]=0;
98                 if (*p!=0 && *p!='#') { /* param */
99                         i=0; /* read param */
100                         while(*p > 31) {
101                                 if (i+1 >= sizeof(param)) {
102                                         UPRINT(options_error, "Error in %s (line %d): param too long.\n",filename,line);
103                                         goto error;
104                                 }
105                                 param[i+1] = '\0';
106                                 param[i++] = *p++;
107                         }
108                 }
109
110                 /* at this point we have option and param */
111
112                 /* check option */
113                 if (!strcmp(option,"nt_if") || !strcmp(option,"te_if")) {
114                         UPRINT(options_error, "Error in %s (line %d): obsolete option %s. Use multiple 'port' options to define ports to use.\n",filename,line,option);
115                         goto error;
116                 } else
117                 if (!strcmp(option,"debug")) {
118                         if (param[0]==0) {
119                                 UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
120                                 goto error;
121                         }
122                         options.deb = strtol(param, NULL, 0);
123
124                 } else
125                 if (!strcmp(option,"log")) {
126                         if (param[0]==0) {
127                                 UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
128                                 goto error;
129                         }
130                         SCPY(options.log, param);
131
132                 } else
133                 if (!strcmp(option,"alaw")) {
134                         options.law = 'a';
135
136                 } else
137                 if (!strcmp(option,"ulaw")) {
138                         options.law = 'u';
139
140                 } else
141                 if (!strcmp(option,"tones_dir")) {
142                         if (param[0]==0) {
143                                 UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
144                                 goto error;
145                         }
146                         if (param[strlen(param)-1] == '/')
147                                 param[strlen(param)-1]=0;
148                         SCPY(options.tones_dir, param);
149
150                 } else
151                 if (!strcmp(option,"fetch_tones")) {
152                         if (param[0]==0) {
153                                 UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
154                                 goto error;
155                         }
156                         if (param[strlen(param)-1] == '/')
157                                 param[strlen(param)-1]=0;
158                         SCPY(options.fetch_tones, param);
159
160                 } else
161                 if (!strcmp(option,"extensions_dir")) {
162                         // obsolete
163                 } else
164                 if (!strcmp(option,"national")) {
165                         SCPY(options.national, param);
166
167                 } else
168                 if (!strcmp(option,"international")) {
169                         SCPY(options.international, param);
170
171                 } else
172                 if (!strcmp(option,"dummyid")) {
173                         SCPY(options.dummyid, param);
174
175                 } else
176                 if (!strcmp(option,"dsptones")) {
177                         UPRINT(options_error, "Error in %s (line %d): parameter 'dsptones' is obsolete. Just define the tones (american,german,oldgerman) at 'tones_dir' option.\n",filename,line);
178                         goto error;
179                 } else
180                 if (!strcmp(option,"schedule")) {
181                         options.schedule = atoi(param);
182                         if (options.schedule < 0) {
183                                 UPRINT(options_error, "Error in %s (line %d): parameter for option %s must be at least '0'.\n", filename,line,option);
184                                 goto error;
185                         }
186                         if (options.schedule > 99) {
187                                 UPRINT(options_error, "Error in %s (line %d): parameter for option %s must be '99' or less.\n", filename,line,option);
188                                 goto error;
189                         }
190
191                 } else
192                 if (!strcmp(option,"email")) {
193                         if (param[0]==0) {
194                                 UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n", filename,line,option);
195                                 goto error;
196                         }
197                         SCPY(options.email, param);
198
199                 } else
200                 if (!strcmp(option,"lock")) {
201                         if (param[0]==0) {
202                                 UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
203                                 goto error;
204                         }
205                         if (param[strlen(param)-1] == '/')
206                                 param[strlen(param)-1]=0;
207                         SCPY(options.lock, param);
208
209                 } else
210                 if (!strcmp(option,"socketuser")) {
211                         char * endptr = NULL;
212                         options.socketuser = strtol(param, &endptr, 10);
213                         if (*endptr != '\0') {
214                                 struct passwd * pwd = getpwnam(param);
215                                 if (pwd == NULL) {
216                                         UPRINT(options_error, "Error in %s (line %d): no such user: %s.\n",filename,line,param);
217                                         goto error;
218                                 }
219                                 options.socketuser = pwd->pw_uid;
220                         }
221                 } else
222                 if (!strcmp(option,"socketgroup")) {
223                         char * endptr = NULL;
224                         options.socketgroup = strtol(param, &endptr, 10);
225                         if (*endptr != '\0') {
226                                 struct group * grp = getgrnam(param);
227                                 if (grp == NULL) {
228                                         UPRINT(options_error, "Error in %s (line %d): no such group: %s.\n",filename,line,param);
229                                         goto error;
230                                 }
231                                 options.socketgroup = grp->gr_gid;
232                         }
233                 } else
234                 if (!strcmp(option,"socketrights")) {
235                         options.socketrights = strtol(param, NULL, 0);
236                 } else
237                 if (!strcmp(option,"gsm")) {
238                         options.gsm = 1;
239                 } else
240                 if (!strcmp(option,"polling")) {
241                         options.polling = 1;
242                 } else {
243                         UPRINT(options_error, "Error in %s (line %d): wrong option keyword %s.\n", filename,line,option);
244                         goto error;
245                 }
246         }
247
248         if (!options.tones_dir[0]) {
249                 UPRINT(options_error, "Error in %s (line %d): option 'tones_dir' with parameter missing.\n", filename,line);
250                 goto error;
251         }
252         if (fp) fclose(fp);
253         return(1);
254 error:
255         if (fp) fclose(fp);
256         return(0);
257 }
258
259