-
Notifications
You must be signed in to change notification settings - Fork 46
/
readconfig.c
289 lines (261 loc) · 6.59 KB
/
readconfig.c
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* readconfig.c - Read configuration file and
* load settings into struct.
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
// Size limits
#define MAX_LINE_LEN 256
#define MAX_VALUE_LEN 64
// Struct to hold configuration variables
struct cfg
{
// Strings
char *outfilename;
// Basic
int verbose;
int quiet;
int daemon;
int bluelive;
// Logging
int showtime;
int obfuscate;
int encode;
int showclass;
int friendlyclass;
int bluepropro;
int getname;
int amnesia;
int syslogonly;
int getmanufacturer;
// Advanced
int retry_count;
int scan_window;
int hci_device;
// Network
int udponly;
int server_port;
int prefix;
int banner;
int hangup;
char node_name[MAX_VALUE_LEN];
char server_ip[MAX_VALUE_LEN];
// System
int bt_socket;
int udp_socket;
char addr[19];
};
// Define global struct, set default values
struct cfg config =
{
.verbose = 0,
.quiet = 0,
.daemon = 0,
.bluelive = 0,
.showtime = 0,
.obfuscate = 0,
.encode = 0,
.showclass = 0,
.friendlyclass = 0,
.bluepropro = 0,
.getname = 0,
.amnesia = -1,
.syslogonly = 0,
.getmanufacturer = 0,
.retry_count = 3,
.scan_window = 8,
.hci_device = 0,
.udponly = 0,
.udp_socket = -1,
.server_port = 1234,
.banner = 0,
.prefix = 1,
.hangup = 0,
.server_ip = "NULL",
.node_name = "NULL",
.addr = "NULL",
};
// Determine if config file is present
int cfg_exists (void)
{
struct stat buffer;
return (stat(CFG_FILE, &buffer) == 0);
}
// Trim leading spaces from values
// Probably better way to do this...
char* trim_space(char* s)
{
while( *s==' ' )
memmove(s,s+1,strlen(s));
return s;
}
// Convert yes/no to 1/0
int eval_bool(char* value, int line)
{
if (!strcasecmp(value, "YES"))
return 1;
else if (!strcasecmp(value, "NO"))
return 0;
else
{
printf("Invalid value in configuration file on line %i!\n", line);
exit(1);
}
}
// Make sure everybody plays nice
static void cfg_check (void)
{
// Check for out of range values
if ((config.retry_count < 0) || ((config.amnesia < 0) && (config.amnesia != -1)))
{
printf("Error, arguments must be positive numbers!\n");
exit(1);
}
// Make sure window is reasonable
if (config.scan_window > MAX_SCAN || config.scan_window < MIN_SCAN)
{
printf("Scan window is out of range. See README.\n");
exit(1);
}
// Override some options that don't play nice with others
// If retry is different from default, assume names are on.
if (config.retry_count != 3)
config.getname = 1;
// No verbose for daemon
if (config.daemon)
config.verbose = 0;
// No Bluelog Live when running BPP, names on, syslog off
if (config.bluepropro)
{
config.bluelive = 0;
config.getname = 1;
config.syslogonly = 0;
}
// Showing raw class ID turns off friendly names
if (config.showclass)
config.friendlyclass = 0;
// No timestamps for Bluelog Live, names on, syslog off
if (config.bluelive)
{
config.showtime = 0;
config.getname = 1;
config.syslogonly = 0;
}
// No timestamps in syslog mode, disable other modes
if (config.syslogonly)
{
config.showtime = 0;
config.bluelive = 0;
config.bluepropro = 0;
}
// UDP disables other modes
if (config.udponly)
{
config.bluelive = 0;
config.bluepropro = 0;
config.syslogonly = 0;
}
// Encode trumps obfuscate
if (config.encode)
config.obfuscate = 0;
}
int cfg_read (void)
{
FILE* cfgfile;
char line[MAX_LINE_LEN + 1];
char* token;
char* value;
int linenum = 1;
// Open file, return error if something goes wrong
if ((cfgfile = fopen(CFG_FILE, "r")) == NULL)
return(1);
// Continue until file is done
while(fgets(line, MAX_LINE_LEN, cfgfile) != NULL)
{
token = strtok(line, "\t =\n\r");
if(token != NULL && token[0] != '#')
{
// Get token's associated value
value = trim_space(strtok(NULL, "\t;=\n\r"));
if (value != NULL)
{
// Is it too large?
if ((sizeof (value)) > MAX_VALUE_LEN)
{
printf("FAILED\n");
printf("Value too large on line %i!\n", linenum);
exit(1);
}
// See if token matches something
if (strcmp(token, "VERBOSE") == 0)
config.verbose = eval_bool(value, linenum);
else if (strcmp(token, "QUIET") == 0)
config.quiet = eval_bool(value, linenum);
else if (strcmp(token, "DAEMON") == 0)
config.daemon = eval_bool(value, linenum);
else if (strcmp(token, "LIVEMODE") == 0)
config.bluelive = eval_bool(value, linenum);
else if (strcmp(token, "SHOWTIME") == 0)
config.showtime = eval_bool(value, linenum);
else if (strcmp(token, "OBFUSCATE") == 0)
config.obfuscate = eval_bool(value, linenum);
else if (strcmp(token, "ENCODE") == 0)
config.encode = eval_bool(value, linenum);
else if (strcmp(token, "SHOWCLASS") == 0)
config.showclass = eval_bool(value, linenum);
else if (strcmp(token, "FRIENDLYCLASS") == 0)
config.friendlyclass = eval_bool(value, linenum);
else if (strcmp(token, "BLUEPROPRO") == 0)
config.bluepropro = eval_bool(value, linenum);
else if (strcmp(token, "GETNAME") == 0)
config.getname = eval_bool(value, linenum);
else if (strcmp(token, "AMNESIA") == 0)
config.amnesia = (atoi(value));
else if (strcmp(token, "SYSLOGONLY") == 0)
config.syslogonly = eval_bool(value, linenum);
else if (strcmp(token, "GETMANUFACTURER") == 0)
config.getmanufacturer = eval_bool(value, linenum);
else if (strcmp(token, "SCANWINDOW") == 0)
config.scan_window = (atoi(value));
else if (strcmp(token, "RETRYCOUNT") == 0)
config.retry_count = (atoi(value));
else if (strcmp(token, "HCIDEVICE") == 0)
config.hci_device = (atoi(value));
else if (strcmp(token, "UDPONLY") == 0)
config.udponly = eval_bool(value, linenum);
else if (strcmp(token, "SERVERIP") == 0)
strcpy(config.server_ip, value);
else if (strcmp(token, "SERVERPORT") == 0)
config.server_port = (atoi(value));
else if (strcmp(token, "NODENAME") == 0)
strcpy(config.node_name, value);
else if (strcmp(token, "BANNER") == 0)
config.banner = eval_bool(value, linenum);
else if (strcmp(token, "HANGUP") == 0)
config.hangup = eval_bool(value, linenum);
else if (strcmp(token, "PREFIX") == 0)
config.prefix = eval_bool(value, linenum);
else
{
printf("FAILED\n");
printf("Syntax error or unknown option in configuration file on line %i!\n", linenum);
exit(1);
}
}
else
{
printf("FAILED\n");
printf("Value missing in configuration file on line %i!\n", linenum);
exit(1);
}
}
// Increment line number
linenum++;
}
return (0);
}