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
|
/* $OpenBSD: bgplg.c,v 1.4 2006/12/12 14:18:05 reyk Exp $ */
/*
* Copyright (c) 2005, 2006 Reyk Floeter <reyk@vantronix.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/param.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include "bgplg.h"
#define INC_STYLE "/conf/bgplg.css"
#define INC_HEAD "/conf/bgplg.head"
#define INC_FOOT "/conf/bgplg.foot"
#define BGPDSOCK "/logs/bgpd.rsock"
#define BGPCTL "/bin/bgpctl", "-s", BGPDSOCK
#define PING "/bin/ping"
#define TRACEROUTE "/bin/traceroute"
#define CONTENT_TYPE "text/html"
static struct cmd cmds[] = CMDS;
char *lg_getenv(const char *, int *);
void lg_urldecode(char *);
char **lg_arg2argv(char *, int *);
char **lg_argextra(char **, int, struct cmd *);
char *lg_getarg(const char *, char *, int);
int lg_incl(const char *);
void
lg_urldecode(char *str)
{
size_t i, c, len;
char code[3];
long result;
if (str && *str) {
len = strlen(str);
i = c = 0;
while (i < len) {
if (str[i] == '%' && i <= (len - 2)) {
if (isxdigit(str[i + 1]) &&
isxdigit(str[i + 2])) {
code[0] = str[i + 1];
code[1] = str[i + 2];
code[2] = 0;
result = strtol(code, NULL, 16);
/* Replace NUL chars with a space */
if (result == 0)
result = ' ';
str[c++] = result;
i += 3;
} else {
str[c++] = '%';
i++;
}
} else if (str[i] == '+') {
str[i] = ' ';
} else {
if (c != i)
str[c] = str[i];
c++;
i++;
}
}
str[c] = 0x0;
}
}
char *
lg_getenv(const char *name, int *lenp)
{
size_t len;
u_int i;
char *ptr;
if ((ptr = getenv(name)) == NULL)
return (NULL);
lg_urldecode(ptr);
if (!(len = strlen(ptr)))
return (NULL);
if (lenp != NULL)
*lenp = len;
#define allowed_in_string(_x) \
((isalnum(_x) || isprint(_x)) && \
(_x != '%' && _x != '\\' && _x != ';' && _x != '|'))
for (i = 0; i < len; i++) {
if (!allowed_in_string(ptr[i])) {
printf("invalid character in input\n");
return (NULL);
}
if (ptr[i] == '&')
ptr[i] = '\0';
}
return (ptr);
}
char *
lg_getarg(const char *name, char *arg, int len)
{
char *ptr = arg;
size_t namelen, ptrlen;
int i;
namelen = strlen(name);
for (i = 0; i < len; i++) {
if (arg[i] == '\0')
continue;
ptr = arg + i;
ptrlen = strlen(ptr);
if (namelen >= ptrlen)
continue;
if (strncmp(name, ptr, namelen) == 0)
return (ptr + namelen);
}
return (NULL);
}
char **
lg_arg2argv(char *arg, int *argc)
{
char **argv, *ptr = arg;
size_t len;
u_int i, c = 1;
len = strlen(arg);
/* Count elements */
for (i = 0; i < (len - 1); i++) {
if (isspace(arg[i])) {
/* filter out additional options */
if (arg[i + 1] == '-') {
printf("invalid input\n");
return (NULL);
}
arg[i] = '\0';
c++;
}
}
/* Generate array */
if ((argv = calloc(c + 1, sizeof(char *))) == NULL) {
printf("fatal error: %s\n", strerror(errno));
return (NULL);
}
argv[c] = NULL;
*argc = c;
/* Fill array */
for (i = c = 0; i < (len - 1); i++) {
if (arg[i] == '\0' || i == 0) {
if (i != 0)
ptr = &arg[i + 1];
argv[c++] = ptr;
}
}
return (argv);
}
char **
lg_argextra(char **argv, int argc, struct cmd *cmdp)
{
char **new_argv;
int i, c = 0;
/* Count elements */
for (i = 0; cmdp->earg[i] != NULL; i++)
c++;
/* Generate array */
if ((new_argv = calloc(c + argc + 1, sizeof(char *))) == NULL) {
printf("fatal error: %s\n", strerror(errno));
return (NULL);
}
/* Fill array */
for (i = c = 0; cmdp->earg[i] != NULL; i++)
new_argv[c++] = cmdp->earg[i];
/* Append old array */
for (i = 0; i < argc; i++)
new_argv[c++] = argv[i];
new_argv[c] = NULL;
if (argv != NULL)
free(argv);
return (new_argv);
}
int
lg_incl(const char *file)
{
char buf[BUFSIZ];
int fd, len;
if ((fd = open(file, O_RDONLY)) == -1)
return (errno);
do {
len = read(fd, buf, sizeof(buf));
fwrite(buf, len, 1, stdout);
} while(len == BUFSIZ);
return (0);
}
int
main(void)
{
char *query, *self, *cmd = NULL, *req;
char **argv = NULL;
char myname[MAXHOSTNAMELEN];
int ret = 1, argc = 0, query_length = 0;
struct stat st;
u_int i;
struct cmd *cmdp = NULL;
if (gethostname(myname, sizeof(myname)) != 0)
return (1);
printf("Content-Type: %s\n"
"Cache-Control: no-cache\n\n"
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" "
"\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
"<head>\n"
"<title>%s: %s</title>\n",
CONTENT_TYPE, NAME, myname);
if (stat(INC_STYLE, &st) == 0) {
printf("<style type='text/css'><!--\n");
lg_incl(INC_STYLE);
printf("--></style>\n");
}
if (stat(INC_HEAD, &st) != 0 || lg_incl(INC_HEAD) != 0) {
printf("</head>\n"
"<body>\n");
}
printf("<h1>%s: %s</h1>\n", NAME, myname);
printf("<h2>%s</h2>\n", BRIEF);
/* print a form with possible options */
if ((self = lg_getenv("SCRIPT_NAME", NULL)) == NULL) {
printf("fatal error: invalid request\n");
goto err;
}
if ((query = lg_getenv("QUERY_STRING", &query_length)) != NULL)
cmd = lg_getarg("cmd=", query, query_length);
printf(
"<form action='%s'>\n"
"<div class=\"command\">\n"
"<select name='cmd'>\n",
self);
for (i = 0; cmds[i].name != NULL; i++) {
if (!lg_checkperm(&cmds[i]))
continue;
if (cmd != NULL && strcmp(cmd, cmds[i].name) == 0)
printf("<option value='%s' selected='selected'>%s"
"</option>\n",
cmds[i].name, cmds[i].name);
else
printf("<option value='%s'>%s</option>\n",
cmds[i].name, cmds[i].name);
}
printf("</select>\n"
"<input type='text' name='req'/>\n"
"<input type='submit' value='submit'/>\n"
"</div>\n"
"</form>\n"
"<pre>\n");
fflush(stdout);
#ifdef DEBUG
if (close(2) == -1 || dup2(1, 2) == -1)
#else
if (close(2) == -1)
#endif
{
printf("fatal error: %s\n", strerror(errno));
goto err;
}
if (query == NULL)
goto err;
if (cmd == NULL) {
printf("unspecified command\n");
goto err;
}
if ((req = lg_getarg("req=", query, query_length)) != NULL) {
/* Could be NULL */
argv = lg_arg2argv(req, &argc);
}
for (i = 0; cmds[i].name != NULL; i++) {
if (strcmp(cmd, cmds[i].name) == 0) {
cmdp = &cmds[i];
break;
}
}
if (cmdp == NULL) {
printf("invalid command: %s\n", cmd);
goto err;
}
if (argc > cmdp->maxargs) {
printf("superfluous argument(s): %s %s\n",
cmd, cmdp->args ? cmdp->args : "");
goto err;
}
if (argc < cmdp->minargs) {
printf("missing argument(s): %s %s\n", cmd, cmdp->args);
goto err;
}
if (cmdp->func != NULL) {
ret = cmdp->func(cmds, argv);
} else {
if ((argv = lg_argextra(argv, argc, cmdp)) == NULL)
goto err;
ret = lg_exec(cmdp->earg[0], argv);
}
if (ret != 0)
printf("\nfailed%s\n", ret == 127 ? ": file not found" : ".");
else
printf("\nsuccess.\n");
err:
fflush(stdout);
if (argv != NULL);
free(argv);
printf("</pre>\n");
if (stat(INC_FOOT, &st) != 0 || lg_incl(INC_FOOT) != 0)
printf("<hr/>\n");
printf("<div class='footer'>\n"
"<small>%s - %s<br/>Copyright (c) %s</small>\n"
"</div>\n"
"</body>\n"
"</html>\n", NAME, BRIEF, COPYRIGHT);
return (ret);
}
|