summaryrefslogtreecommitdiff
path: root/snmplib/snmp_service.c
blob: 54cb4ac88a13197bb547a9bdc3999c14d40f0c99 (plain)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <net-snmp/net-snmp-config.h>

#include <stdlib.h>
#include <string.h>

#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/library/snmp_transport.h>

static char**
create_word_array_helper(const char* cptr, size_t idx, char* tmp, size_t tmplen)
{
    char* item;
    char** res;
    cptr = copy_nword_const(cptr, tmp, tmplen);
    item = strdup(tmp);
    if (cptr)
        res = create_word_array_helper(cptr, idx + 1, tmp, tmplen);
    else {
        res = (char**)malloc(sizeof(char*) * (idx + 2));
        res[idx + 1] = NULL;
    }
    res[idx] = item;
    return res;
}

static char**
create_word_array(const char* cptr)
{
    size_t tmplen = strlen(cptr);
    char* tmp = (char*)malloc(tmplen + 1);
    char** res = create_word_array_helper(cptr, 0, tmp, tmplen + 1);
    free(tmp);
    return res;
}

static void
destroy_word_array(char** arr)
{
    if (arr) {
        char** run = arr;
        while(*run) {
            free(*run);
            ++run;
        }
        free(arr);
    }
}

struct netsnmp_lookup_domain {
    char* application;
    char** userDomain;
    char** domain;
    struct netsnmp_lookup_domain* next;
};

static struct netsnmp_lookup_domain* domains = NULL;

int
netsnmp_register_default_domain(const char* application, const char* domain)
{
    struct netsnmp_lookup_domain *run = domains, *prev = NULL;
    int res = 0;

    while (run != NULL && strcmp(run->application, application) < 0) {
	prev = run;
	run = run->next;
    }
    if (run && strcmp(run->application, application) == 0) {
      if (run->domain != NULL) {
          destroy_word_array(run->domain);
	  run->domain = NULL;
	  res = 1;
      }
    } else {
	run = SNMP_MALLOC_STRUCT(netsnmp_lookup_domain);
	run->application = strdup(application);
	run->userDomain = NULL;
	if (prev) {
	    run->next = prev->next;
	    prev->next = run;
	} else {
	    run->next = domains;
	    domains = run;
	}
    }
    if (domain) {
        run->domain = create_word_array(domain);
    } else if (run->userDomain == NULL) {
	if (prev)
	    prev->next = run->next;
	else
	    domains = run->next;
	free(run->application);
	free(run);
    }
    return res;
}

void
netsnmp_clear_default_domain(void)
{
    while (domains) {
	struct netsnmp_lookup_domain *tmp = domains;
	domains = domains->next;
	free(tmp->application);
        destroy_word_array(tmp->userDomain);
        destroy_word_array(tmp->domain);
	free(tmp);
    }
}

static void
netsnmp_register_user_domain(const char* token, char* cptr)
{
    struct netsnmp_lookup_domain *run = domains, *prev = NULL;
    size_t len = strlen(cptr) + 1;
    char* application = (char*)malloc(len);
    char** domain;

    {
        char* cp = copy_nword(cptr, application, len);
        if (cp == NULL) {
            netsnmp_config_error("No domain(s) in registration of "
                                 "defDomain \"%s\"", application);
            free(application);
            return;
        }
        domain = create_word_array(cp);
    }

    while (run != NULL && strcmp(run->application, application) < 0) {
	prev = run;
	run = run->next;
    }
    if (run && strcmp(run->application, application) == 0) {
	if (run->userDomain != NULL) {
	    config_perror("Default transport already registered for this "
			  "application");
            destroy_word_array(domain);
            free(application);
	    return;
	}
    } else {
	run = SNMP_MALLOC_STRUCT(netsnmp_lookup_domain);
	run->application = strdup(application);
	run->domain = NULL;
	if (prev) {
	    run->next = prev->next;
	    prev->next = run;
	} else {
	    run->next = domains;
	    domains = run;
	}
    }
    run->userDomain = domain;
    free(application);
}

static void
netsnmp_clear_user_domain(void)
{
    struct netsnmp_lookup_domain *run = domains, *prev = NULL;

    while (run) {
	if (run->userDomain != NULL) {
            destroy_word_array(run->userDomain);
	    run->userDomain = NULL;
	}
	if (run->domain == NULL) {
	    struct netsnmp_lookup_domain *tmp = run;
	    if (prev)
		run = prev->next = run->next;
	    else
		run = domains = run->next;
	    free(tmp->application);
	    free(tmp);
	} else {
	    prev = run;
	    run = run->next;
	}
    }
}

const char* const *
netsnmp_lookup_default_domains(const char* application)
{
    const char * const * res;

    if (application == NULL)
	res = NULL;
    else {
        struct netsnmp_lookup_domain *run = domains;

	while (run && strcmp(run->application, application) < 0)
	    run = run->next;
	if (run && strcmp(run->application, application) == 0)
	    if (run->userDomain)
                res = (const char * const *)run->userDomain;
	    else
                res = (const char * const *)run->domain;
	else
	    res = NULL;
    }
    DEBUGMSGTL(("defaults",
                "netsnmp_lookup_default_domain(\"%s\") ->",
                application ? application : "[NIL]"));
    if (res) {
        const char * const * r = res;
        while(*r) {
            DEBUGMSG(("defaults", " \"%s\"", *r));
            ++r;
        }
        DEBUGMSG(("defaults", "\n"));
    } else
        DEBUGMSG(("defaults", " \"[NIL]\"\n"));
    return res;
}

const char*
netsnmp_lookup_default_domain(const char* application)
{
    const char * const * res = netsnmp_lookup_default_domains(application);
    return (res ? *res : NULL);
}

struct netsnmp_lookup_target {
    char* application;
    char* domain;
    char* userTarget;
    char* target;
    struct netsnmp_lookup_target* next;
};

static struct netsnmp_lookup_target* targets = NULL;

/**
 * Add an (application, domain, target) triplet to the targets list if target
 * != NULL. Remove an entry if target == NULL and the userTarget pointer for
 * the entry found is also NULL. Keep at most one target per (application,
 * domain) pair.
 *
 * @return 1 if an entry for (application, domain) was already present in the
 *   targets list or 0 if such an entry was not yet present in the targets list.
 */
int
netsnmp_register_default_target(const char* application, const char* domain,
				const char* target)
{
    struct netsnmp_lookup_target *run = targets, *prev = NULL;
    int i = 0, res = 0;
    while (run && ((i = strcmp(run->application, application)) < 0 ||
		   (i == 0 && strcmp(run->domain, domain) < 0))) {
	prev = run;
	run = run->next;
    }
    if (run && i == 0 && strcmp(run->domain, domain) == 0) {
      if (run->target != NULL) {
	    free(run->target);
	    run->target = NULL;
	    res = 1;
      }
    } else {
	run = SNMP_MALLOC_STRUCT(netsnmp_lookup_target);
	run->application = strdup(application);
	run->domain = strdup(domain);
	run->userTarget = NULL;
	if (prev) {
	    run->next = prev->next;
	    prev->next = run;
	} else {
	    run->next = targets;
	    targets = run;
	}
    }
    if (target) {
	run->target = strdup(target);
    } else if (run->userTarget == NULL) {
	if (prev)
	    prev->next = run->next;
	else
	    targets = run->next;
	free(run->domain);
	free(run->application);
	free(run);
    }
    return res;
}

/**
 * Clear the targets list.
 */
void
netsnmp_clear_default_target(void)
{
    while (targets) {
	struct netsnmp_lookup_target *tmp = targets;
	targets = targets->next;
	free(tmp->application);
	free(tmp->domain);
	free(tmp->userTarget);
	free(tmp->target);
	free(tmp);
    }
}

static void
netsnmp_register_user_target(const char* token, char* cptr)
{
    struct netsnmp_lookup_target *run = targets, *prev = NULL;
    size_t len = strlen(cptr) + 1;
    char* application = (char*)malloc(len);
    char* domain = (char*)malloc(len);
    char* target = (char*)malloc(len);
    int i = 0;

    {
	char* cp = copy_nword(cptr, application, len);
        if (cp == NULL) {
            netsnmp_config_error("No domain and target in registration of "
                                 "defTarget \"%s\"", application);
            goto done;
        }
	cp = copy_nword(cp, domain, len);
        if (cp == NULL) {
            netsnmp_config_error("No target in registration of "
                                 "defTarget \"%s\" \"%s\"",
                                 application, domain);
            goto done;
        }
	cp = copy_nword(cp, target, len);
	if (cp)
	    config_pwarn("Trailing junk found");
    }

    while (run && ((i = strcmp(run->application, application)) < 0 ||
		   (i == 0 && strcmp(run->domain, domain) < 0))) {
	prev = run;
	run = run->next;
    }
    if (run && i == 0 && strcmp(run->domain, domain) == 0) {
	if (run->userTarget != NULL) {
	    config_perror("Default target already registered for this "
			  "application-domain combination");
	    goto done;
	}
    } else {
	run = SNMP_MALLOC_STRUCT(netsnmp_lookup_target);
	run->application = strdup(application);
	run->domain = strdup(domain);
	run->target = NULL;
	if (prev) {
	    run->next = prev->next;
	    prev->next = run;
	} else {
	    run->next = targets;
	    targets = run;
	}
    }
    run->userTarget = strdup(target);
 done:
    free(target);
    free(domain);
    free(application);
}

static void
netsnmp_clear_user_target(void)
{
    struct netsnmp_lookup_target *run = targets, *prev = NULL;

    while (run) {
	if (run->userTarget != NULL) {
	    free(run->userTarget);
	    run->userTarget = NULL;
	}
	if (run->target == NULL) {
	    struct netsnmp_lookup_target *tmp = run;
	    if (prev)
		run = prev->next = run->next;
	    else
		run = targets = run->next;
	    free(tmp->application);
	    free(tmp->domain);
	    free(tmp);
	} else {
	    prev = run;
	    run = run->next;
	}
    }
}

const char*
netsnmp_lookup_default_target(const char* application, const char* domain)
{
    int i = 0;
    struct netsnmp_lookup_target *run = targets;
    const char *res;

    if (application == NULL || domain == NULL)
	res = NULL;
    else {
	while (run && ((i = strcmp(run->application, application)) < 0 ||
		       (i == 0 && strcmp(run->domain, domain) < 0)))
	    run = run->next;
	if (run && i == 0 && strcmp(run->domain, domain) == 0)
	    if (run->userTarget != NULL)
		res = run->userTarget;
	    else
		res = run->target;
	else
	    res = NULL;
    }
    DEBUGMSGTL(("defaults",
		"netsnmp_lookup_default_target(\"%s\", \"%s\") -> \"%s\"\n",
		application ? application : "[NIL]",
		domain ? domain : "[NIL]",
		res ? res : "[NIL]"));
    return res;
}

void
netsnmp_register_service_handlers(void)
{
    register_config_handler("snmp:", "defDomain",
			    netsnmp_register_user_domain,
			    netsnmp_clear_user_domain,
			    "application domain");
    register_config_handler("snmp:", "defTarget",
			    netsnmp_register_user_target,
			    netsnmp_clear_user_target,
			    "application domain target");
}