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