-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.c
248 lines (199 loc) · 5.05 KB
/
config.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
/*
* This file is part of John the Ripper password cracker,
* Copyright (c) 1996-2002,2009,2013,2019 by Solar Designer
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted.
*
* There's ABSOLUTELY NO WARRANTY, express or implied.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "misc.h"
#include "params.h"
#include "path.h"
#include "memory.h"
#include "config.h"
#include "john.h"
char *cfg_name = NULL;
static struct cfg_section *cfg_database = NULL;
static char *trim(char *s, int right)
{
while (*s == ' ' || *s == '\t')
s++;
if (!*s)
return s;
if (right) {
char *e = s + strlen(s) - 1;
while (*e == ' ' || *e == '\t') {
*e = 0;
if (e == s)
break;
e--;
}
}
return s;
}
static void cfg_add_section(const char *name)
{
struct cfg_section *last;
last = cfg_database;
cfg_database = mem_alloc_tiny(
sizeof(struct cfg_section), MEM_ALIGN_WORD);
cfg_database->next = last;
cfg_database->name = str_alloc_copy(name);
cfg_database->params = NULL;
if (!strncmp(name, "list.", 5)) {
cfg_database->list = mem_alloc_tiny(
sizeof(struct cfg_list), MEM_ALIGN_WORD);
cfg_database->list->head = cfg_database->list->tail = NULL;
} else {
cfg_database->list = NULL;
}
}
static void cfg_add_line(const char *line, int number)
{
struct cfg_list *list;
struct cfg_line *entry;
entry = mem_alloc_tiny(sizeof(struct cfg_line), MEM_ALIGN_WORD);
entry->next = NULL;
entry->data = str_alloc_copy(line);
entry->number = number;
list = cfg_database->list;
if (list->tail)
list->tail = list->tail->next = entry;
else
list->tail = list->head = entry;
}
static void cfg_add_param(const char *name, const char *value)
{
struct cfg_param *current, *last;
last = cfg_database->params;
current = cfg_database->params = mem_alloc_tiny(
sizeof(struct cfg_param), MEM_ALIGN_WORD);
current->next = last;
current->name = str_alloc_copy(name);
current->value = str_alloc_copy(value);
}
static int cfg_process_line(char *line, int number)
{
char *p;
line = trim(line, 0);
if (!*line || *line == '#' || *line == ';')
return 0;
if (*line == '[') {
if (!(p = strchr(line, ']')))
return 1;
*p = 0;
cfg_add_section(strlwr(trim(line + 1, 1)));
} else
if (cfg_database && cfg_database->list) {
cfg_add_line(line, number);
} else
if (cfg_database && (p = strchr(line, '='))) {
*p++ = 0;
cfg_add_param(strlwr(trim(line, 1)), trim(p, 1));
} else {
return 1;
}
return 0;
}
static void cfg_error(const char *name, int number)
{
if (john_main_process)
fprintf(stderr, "Error in %s at line %d\n",
path_expand(name), number);
error();
}
void cfg_init(const char *name, int allow_missing)
{
FILE *file;
char line[LINE_BUFFER_SIZE];
int number;
if (cfg_database) return;
if (!(file = fopen(path_expand(name), "r"))) {
if (allow_missing && errno == ENOENT) return;
pexit("fopen: %s", path_expand(name));
}
number = 0;
while (fgetl(line, sizeof(line), file))
if (cfg_process_line(line, ++number)) cfg_error(name, number);
if (ferror(file)) pexit("fgets");
if (fclose(file)) pexit("fclose");
cfg_name = str_alloc_copy(path_expand(name));
}
static struct cfg_section *cfg_get_section(const char *section, const char *subsection)
{
struct cfg_section *current;
const char *p1, *p2;
if ((current = cfg_database))
do {
p1 = current->name; p2 = section;
while (*p1 && *p1 == tolower((int)(unsigned char)*p2)) {
p1++; p2++;
}
if (*p2) continue;
if ((p2 = subsection))
while (*p1 && *p1 == tolower((int)(unsigned char)*p2)) {
p1++; p2++;
}
if (*p1) continue;
if (p2) if (*p2) continue;
return current;
} while ((current = current->next));
return NULL;
}
struct cfg_list *cfg_get_list(const char *section, const char *subsection)
{
struct cfg_section *current;
if ((current = cfg_get_section(section, subsection)))
return current->list;
return NULL;
}
char *cfg_get_param(const char *section, const char *subsection, const char *param)
{
struct cfg_section *current_section;
struct cfg_param *current_param;
const char *p1, *p2;
if ((current_section = cfg_get_section(section, subsection)))
if ((current_param = current_section->params))
do {
p1 = current_param->name; p2 = param;
while (*p1 && *p1 == tolower((int)(unsigned char)*p2)) {
p1++; p2++;
}
if (*p1 || *p2) continue;
return current_param->value;
} while ((current_param = current_param->next));
return NULL;
}
int cfg_get_int(const char *section, const char *subsection, const char *param)
{
char *s_value, *error;
long l_value;
if ((s_value = cfg_get_param(section, subsection, param))) {
l_value = strtol(s_value, &error, 10);
if (!*s_value || *error || (l_value & ~0x3FFFFFFFL))
return -1;
return (int)l_value;
}
return -1;
}
int cfg_get_bool(const char *section, const char *subsection, const char *param, int def)
{
char *value;
if (!(value = cfg_get_param(section, subsection, param)))
return def;
switch (*value) {
case 'y':
case 'Y':
case 't':
case 'T':
case '1':
return 1;
}
return 0;
}