summaryrefslogtreecommitdiffstats
path: root/options.c
blob: b1c52cfacf7fd43a39304ac5c6f879ae846659a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*****************************************************************************\
**                                                                           **
** PBX4Linux                                                                 **
**                                                                           **
**---------------------------------------------------------------------------**
** Copyright: Andreas Eversberg                                              **
**                                                                           **
** reading options.conf and filling structure                                **
**                                                                           **
\*****************************************************************************/ 

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
#include "macro.h"
#include "extension.h"
#include "options.h"
#include <grp.h>
#include <pwd.h>

struct options options = {
	"/usr/local/lcr/log",		/* log file */
	0x0000,				/* debug mode */
	'a',				/* a-law */
	"0",				/* national prefix */
	"00",				/* international prefix */
	"tones_american",		/* directory of tones */
	"",				/* directories of tones to fetch */
	"",				/* dummy caller id */
	0,				/* by default use priority 0 */
	"lcr@your.machine",		/* source mail adress */
	"/var/tmp",			/* path of lock files */
	0700,				/* rights of lcr admin socket */
	-1,                             /* socket user (-1= no change) */
	-1,                             /* socket group (-1= no change) */
	1,				/* use polling of main loop */
	"mISDN_l1loop.1",		/* GSM/Asterisk side */
	"mISDN_l1loop.2",		/* LCR side */
};

char options_error[256];

/* read options
 *
 * read options from options.conf
 */
int read_options(char *options_error)
{
	FILE *fp=NULL;
	char filename[128];
	char *p;
	char option[32];
	char param[256];
	unsigned int line,i;
	char buffer[256];

	SPRINT(filename, "%s/options.conf", CONFIG_DATA);

	if (!(fp=fopen(filename,"r"))) {
		UPRINT(options_error, "Cannot open %s\n",filename);
		return(0);
	}

	line=0;
	while((fgets(buffer,sizeof(buffer),fp))) {
		line++;
		buffer[sizeof(buffer)-1]=0;
		if (buffer[0]) buffer[strlen(buffer)-1]=0;
		p=buffer;

		while(*p <= 32) { /* skip spaces */
			if (*p == 0)
				break;
			p++;
		}
		if (*p==0 || *p=='#') /* ignore comments and empty line */
			continue;

		option[0]=0;
		i=0; /* read option */
		while(*p > 32) {
			if (i+1 >= sizeof(option)) {
				UPRINT(options_error, "Error in %s (line %d): option too long.\n",filename,line);
				goto error;
			}
			option[i+1] = '\0';
			option[i++] = *p++;
		}

		while(*p <= 32) { /* skip spaces */
			if (*p == 0)
				break;
			p++;
		}

		param[0]=0;
		if (*p!=0 && *p!='#') { /* param */
			i=0; /* read param */
			while(*p > 31) {
				if (i+1 >= sizeof(param)) {
					UPRINT(options_error, "Error in %s (line %d): param too long.\n",filename,line);
					goto error;
				}
				param[i+1] = '\0';
				param[i++] = *p++;
			}
		}

		/* at this point we have option and param */

		/* check option */
		if (!strcmp(option,"nt_if") || !strcmp(option,"te_if")) {
			UPRINT(options_error, "Error in %s (line %d): obsolete option %s. Use multiple 'port' options to define ports to use.\n",filename,line,option);
			goto error;
		} else
		if (!strcmp(option,"debug")) {
			if (param[0]==0) {
				UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
				goto error;
			}
			options.deb = strtol(param, NULL, 0);

		} else
		if (!strcmp(option,"log")) {
			if (param[0]==0) {
				UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
				goto error;
			}
			SCPY(options.log, param);

		} else
		if (!strcmp(option,"alaw")) {
			options.law = 'a';

		} else
		if (!strcmp(option,"ulaw")) {
			options.law = 'u';

		} else
		if (!strcmp(option,"tones_dir")) {
			if (param[0]==0) {
				UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
				goto error;
			}
			if (param[strlen(param)-1] == '/')
				param[strlen(param)-1]=0;
			SCPY(options.tones_dir, param);

		} else
		if (!strcmp(option,"fetch_tones")) {
			if (param[0]==0) {
				UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
				goto error;
			}
			if (param[strlen(param)-1] == '/')
				param[strlen(param)-1]=0;
			SCPY(options.fetch_tones, param);

		} else
		if (!strcmp(option,"extensions_dir")) {
			// obsolete
		} else
		if (!strcmp(option,"national")) {
			SCPY(options.national, param);

		} else
		if (!strcmp(option,"international")) {
			SCPY(options.international, param);

		} else
		if (!strcmp(option,"dummyid")) {
			SCPY(options.dummyid, param);

		} else
		if (!strcmp(option,"dsptones")) {
			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);
			goto error;
		} else
		if (!strcmp(option,"schedule")) {
			options.schedule = atoi(param);
			if (options.schedule < 0) {
				UPRINT(options_error, "Error in %s (line %d): parameter for option %s must be at least '0'.\n", filename,line,option);
				goto error;
			}
			if (options.schedule > 99) {
				UPRINT(options_error, "Error in %s (line %d): parameter for option %s must be '99' or less.\n", filename,line,option);
				goto error;
			}

		} else
		if (!strcmp(option,"email")) {
			if (param[0]==0) {
				UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n", filename,line,option);
				goto error;
			}
			SCPY(options.email, param);

		} else
		if (!strcmp(option,"lock")) {
			if (param[0]==0) {
				UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
				goto error;
			}
			if (param[strlen(param)-1] == '/')
				param[strlen(param)-1]=0;
			SCPY(options.lock, param);

		} else
		if (!strcmp(option,"socketuser")) {
			char * endptr = NULL;
			options.socketuser = strtol(param, &endptr, 10);
			if (*endptr != '\0') {
				struct passwd * pwd = getpwnam(param);
				if (pwd == NULL) {
					UPRINT(options_error, "Error in %s (line %d): no such user: %s.\n",filename,line,param);
					goto error;
				}
				options.socketuser = pwd->pw_uid;
			}
		} else
		if (!strcmp(option,"socketgroup")) {
			char * endptr = NULL;
			options.socketgroup = strtol(param, &endptr, 10);
			if (*endptr != '\0') {
				struct group * grp = getgrnam(param);
				if (grp == NULL) {
					UPRINT(options_error, "Error in %s (line %d): no such group: %s.\n",filename,line,param);
					goto error;
				}
				options.socketgroup = grp->gr_gid;
			}
		} else
		if (!strcmp(option,"socketrights")) {
			options.socketrights = strtol(param, NULL, 0);
		} else
		if (!strcmp(option,"polling")) {
			options.polling = 1;
		} else
		if (!strcmp(option,"loopback-ext")) {
			if (param[0]==0) {
				UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
				goto error;
			}
			SCPY(options.loopback_ext, param);

		} else
		if (!strcmp(option,"loopback-lcr")) {
			if (param[0]==0) {
				UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
				goto error;
			}
			SCPY(options.loopback_lcr, param);

		} else {
			UPRINT(options_error, "Error in %s (line %d): wrong option keyword %s.\n", filename,line,option);
			goto error;
		}
	}

	if (!options.tones_dir[0]) {
		UPRINT(options_error, "Error in %s (line %d): option 'tones_dir' with parameter missing.\n", filename,line);
		goto error;
	}
	if (fp) fclose(fp);
	return(1);
error:
	if (fp) fclose(fp);
	return(0);
}