-
Notifications
You must be signed in to change notification settings - Fork 1
/
ndb.c
225 lines (191 loc) · 4.32 KB
/
ndb.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
/*
* ndb.c
*
* Copyright (c) 2002 Dug Song <[email protected]>
*
* $Id: ndb.c,v 1.4 2002/12/10 05:45:05 dugsong Exp $
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <sys/types.h>
#include <dnet.h>
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ndb.h"
struct strtab {
char *buf;
int off;
int size;
};
static struct ndb {
struct strtab st;
int *ip;
int *tcp;
int *udp;
} *ndb;
static int
strtab_put(struct strtab *st, const char *string)
{
char *p;
int off, i = strlen(string);
while (st->size - st->off < i + 1) {
if (st->buf == NULL) {
st->buf = malloc(BUFSIZ);
st->off = 0;
st->size = BUFSIZ;
} else if ((p = realloc(st->buf, st->size << 1)) != NULL) {
st->buf = p;
st->size <<= 1;
} else
err(1, "realloc");
}
strcpy(st->buf + st->off, string);
off = st->off;
st->off += i + 1;
return (off);
}
static char *
strtab_get(struct strtab *st, int off)
{
return (st->buf + off);
}
static void
strtab_free(struct strtab *st)
{
free(st->buf);
memset(st, 0, sizeof(*st));
}
static void
ndb_load_protocols(struct ndb *ndb, FILE *fp)
{
char *name, *num, buf[BUFSIZ];
int i;
while (fgets(buf, sizeof(buf), fp) != NULL) {
if (buf[0] == '#' || isspace((int)buf[0]))
continue;
name = strtok(buf, " \t");
num = strtok(NULL, " \t\r\n");
if (name && num && (i = atoi(num)) != 0)
ndb->ip[i] = strtab_put(&ndb->st, name);
}
}
static void
ndb_load_services(struct ndb *ndb, FILE *fp)
{
char *name, *port, *proto, buf[BUFSIZ];
int i;
while (fgets(buf, sizeof(buf), fp) != NULL) {
if (buf[0] == '#' || isspace((int)buf[0]))
continue;
name = strtok(buf, " \t");
port = strtok(NULL, " \t/");
proto = strtok(NULL, " \t\r\n");
if (name && port && proto && (i = atoi(port)) != 0) {
if (strcmp(proto, "tcp") == 0) {
if (ndb->udp[i] && strcmp(name,
strtab_get(&ndb->st, ndb->udp[i])) == 0)
ndb->tcp[i] = ndb->udp[i];
else
ndb->tcp[i] = strtab_put(&ndb->st,
name);
} else if (strcmp(proto, "udp") == 0) {
if (ndb->tcp[i] && strcmp(name,
strtab_get(&ndb->st, ndb->tcp[i])) == 0)
ndb->udp[i] = ndb->tcp[i];
else
ndb->udp[i] = strtab_put(&ndb->st,
name);
}
}
}
}
static FILE *
path_fopen(const char *dirpath, const char *filepath, const char *mode)
{
FILE *fp = NULL;
char *p, *q, *dir, *file;
char dpath[BUFSIZ], fpath[BUFSIZ], fname[MAXPATHLEN];
strlcpy(dpath, dirpath, sizeof(dpath));
strlcpy(fpath, filepath, sizeof(fpath));
for (p = dpath; !fp && (dir = strsep(&p, ":")) != NULL; ) {
for (q = fpath; !fp && (file = strsep(&q, ":")) != NULL; ) {
snprintf(fname, sizeof(fname), "%s/%s", dir, file);
fp = fopen(fname, mode);
}
}
return (fp);
}
void
ndb_open(const char *dirpath)
{
FILE *fp;
if ((ndb = calloc(1, sizeof(*ndb))) != NULL) {
ndb->ip = calloc(1, sizeof(ndb->ip[0]) * IP_PROTO_MAX);
ndb->tcp = calloc(1, sizeof(ndb->tcp[0]) * TCP_PORT_MAX);
ndb->udp = calloc(1, sizeof(ndb->udp[0]) * UDP_PORT_MAX);
if ((fp = path_fopen(dirpath,
"protocols:nmap-protocols", "r")) != NULL) {
ndb_load_protocols(ndb, fp);
fclose(fp);
}
if ((fp = path_fopen(dirpath,
"services:nmap-services", "r")) != NULL) {
ndb_load_services(ndb, fp);
fclose(fp);
}
}
}
char *
ndb_proto_name(int proto)
{
char *name, buf[32];
if ((name = strtab_get(&ndb->st, ndb->ip[proto])) == NULL) {
snprintf(buf, sizeof(buf), "proto#%d", proto);
name = buf;
}
return (name);
}
char *
ndb_serv_name(int proto, int port)
{
char *name = NULL;
if (proto == IP_PROTO_TCP && ndb->tcp[port])
name = strtab_get(&ndb->st, ndb->tcp[port]);
else if (proto == IP_PROTO_UDP && ndb->udp[port])
name = strtab_get(&ndb->st, ndb->udp[port]);
return (name ? name : "unknown");
}
int
ndb_serv_num(int proto, const char *name)
{
int i, port = 0;
if (proto == IP_PROTO_TCP) {
for (i = 0; i < TCP_PORT_MAX; i++) {
if (ndb->tcp[i] && strcasecmp(name,
strtab_get(&ndb->st, ndb->tcp[i])) == 0) {
port = i;
break;
}
}
} else if (proto == IP_PROTO_UDP) {
for (i = 0; i < UDP_PORT_MAX; i++) {
if (ndb->udp[i] && strcasecmp(name,
strtab_get(&ndb->st, ndb->udp[i])) == 0) {
port = i;
break;
}
}
}
return (port);
}
void
ndb_close(void)
{
strtab_free(&ndb->st);
free(ndb);
ndb = NULL;
}