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