forked from mdornseif/dffinger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dffingerd.c
252 lines (232 loc) · 6.17 KB
/
dffingerd.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
/* $Id: dffingerd.c,v 1.7 2000/05/04 10:03:15 drt Exp $
* --drt@ailis.de
*
* a finger daemon for use with tcpserver
*
* You might find more Info at http://rc23.cx/dffingerd.html
*
* I do not belive there is a thing like copyright.
*
* $Log: dffingerd.c,v $
* Revision 1.7 2000/05/04 10:03:15 drt
* Agrr, don't drink and code.
* Fixed a really stupid Bug regarding \r\n
*
* Revision 1.6 2000/05/04 07:54:08 drt
* * dffingerd.c: Build fixes, fix at handling \W
*
* * dffingerd-data.c: Build fixes
*
* * dffingerd-conf.c: Build fixes
*
* * Makefile: Build fixes
*
* Revision 1.5 2000/04/28 19:38:02 drt
* Fixes from fefes auditing:
* * first logging, then talking
* * don't allow "/W" just "/W "
* * removed unnecessary "qptr2 = clean_query;"
* * comma style
* Thanks Fefe!
*
* Revision 1.4 2000/04/27 16:42:32 drt
* dffingerd was logging query, not clean_query.
* Cosmetic changes.
*
* Revision 1.3 2000/04/26 09:36:34 drt
* use timeoutwrite
*
* Revision 1.2 2000/04/12 16:02:49 drt
* fixed typos, compile time warnings
*
* Revision 1.1.1.1 2000/04/09 17:24:31 drt
* initial revision
*/
#include <unistd.h> /* for close */
#include "buffer.h"
#include "cdb.h"
#include "droproot.h"
#include "env.h"
#include "open.h"
#include "readwrite.h"
#include "str.h"
#include "stralloc.h"
#include "strerr.h"
#include "timeoutread.h"
#include "timeoutwrite.h"
static char rcsid[] = "$Id: dffingerd.c,v 1.7 2000/05/04 10:03:15 drt Exp $";
#define stderr 2
#define stdout 1
#define stdin 0
#define DEFAULTUSER "default"
#define NOPE "nope\n"
#define FATAL "ddfingerd: fatal: "
/* is_meta() returns 1 if c is a meta character, otherwise 0 */
int is_meta(unsigned char c)
{
/* Mask out meta characters - we better
should do whitelisting instead of blacklisting */
if(c < ' ') return 1;
if(c == '&') return 1;
if(c == ';') return 1;
if(c == '`') return 1;
if(c == '"') return 1;
if(c == '|') return 1;
if(c == '*') return 1;
if(c == '?') return 1;
if(c == '~') return 1;
if(c == '<') return 1;
if(c == '>') return 1;
if(c == '^') return 1;
if(c == '(') return 1;
if(c == ')') return 1;
if(c == '[') return 1;
if(c == ']') return 1;
if(c == '{') return 1;
if(c == '}') return 1;
if(c == '$') return 1;
if(c == '\'') return 1;
if(c == '\\') return 1;
if(c == '\n') return 1;
if(c == '\r') return 1;
return 0;
}
int main()
{
unsigned char *remotehost, *remoteinfo, *remoteip, *remoteport;
unsigned char query[256];
unsigned char clean_query[256];
unsigned char *qptr, *qptr2;
int len, query_len;
int fd, r = 0;
struct cdb c;
stralloc answer = {0};
/* chroot() to $ROOT and switch to $UID:$GID */
droproot("dffingerd: ");
/* since we run under tcpserver, we can get all info
about the remote side from the enviroment */
remotehost = env_get("TCPREMOTEHOST");
if (!remotehost) remotehost = "unknown";
remoteinfo = env_get("TCPREMOTEINFO");
if (!remoteinfo) remoteinfo = "-";
remoteip = env_get("TCPREMOTEIP");
if (!remoteip) remoteip = "unknown";
remoteport = env_get("TCPREMOTEPORT");
if (!remoteport) remoteport = "unknown";
/* now:
remotehost is the remote hostname or "unknown"
remoteinfo is some ident string or "-"
remoteip is the remote ipadress or "unknown" (?)
*/
/* Read the request from the client and \0-terminate it */
/* timeout after 60 seconds */
query_len = timeoutread(60, stdin, query, sizeof(query) - 1);
query[query_len] = '\0';
/* Handle RfC 1288 stuff */
qptr=query;
if (*qptr==' ') qptr++;
if (*qptr=='/'
&& (*(qptr+1)=='W' || *(qptr+1)=='w')
&& *(qptr+2) == ' ')
qptr+=3;
/* \0-terminate query at the first \r or \n */
for (len = 0; query[len]; len++)
{
if (query[len] == '\r' || query[len] == '\n')
{
query[len] = '\0';
break;
}
}
/* clean up query string a bit by removing chars witch could
clobber logging or so and replace them with _ -> extra Paranoia */
for(qptr2 = clean_query; *qptr; qptr++)
{
if(is_meta (*qptr))
{
*qptr2++ = '_';
}
else
{
*qptr2++ = *qptr;
}
}
*qptr2 = '\0';
/* Do logging */
buffer_puts(buffer_2, remotehost);
buffer_puts(buffer_2, " [");
buffer_puts(buffer_2, remoteip);
buffer_puts(buffer_2, ":");
buffer_puts(buffer_2, remoteport);
buffer_puts(buffer_2, "] ");
buffer_puts(buffer_2, remoteinfo);
buffer_puts(buffer_2, " ");
buffer_puts(buffer_2, clean_query);
buffer_puts(buffer_2, "\n");
buffer_flush(buffer_2);
/* If there was any data we will go on */
if (query_len > 0)
{
/* Open & init our cdb */
fd = open_read("data.cdb");
if (fd == -1)
{
/* If opening failed quit */
strerr_die2sys(111, FATAL, "can't open data.cdb");
}
cdb_init(&c, fd);
/* Search query for "user" on the database */
r = cdb_find(&c, clean_query, str_len(clean_query));
if (r == 1)
{
/* read data */
stralloc_ready(&answer, cdb_datalen(&c));
if (cdb_read(&c, answer.s, cdb_datalen(&c), cdb_datapos(&c)) == -1)
{
strerr_die2sys(111, FATAL, "can't read from data.cdb");
}
else
{
answer.len = cdb_datalen(&c);
}
}
else
{
/* We didn't find the requested user, try DEFAULTUSER */
r = cdb_find(&c,DEFAULTUSER, str_len(DEFAULTUSER));
if (r == 1)
{
/* read data */
stralloc_ready(&answer, cdb_datalen(&c));
if (cdb_read(&c, answer.s ,cdb_datalen(&c) ,cdb_datapos(&c)) == -1)
{
strerr_die2sys(111, FATAL, "can't read from data.cdb");
}
else
{
answer.len = cdb_datalen(&c);
}
}
else
{
/* no data for DEFAULTUSER either, so we don't have any data for the client */
stralloc_copys(&answer, NOPE);
}
}
/* write to the network with 120s timeout */
/* I guess the timeout isn't needed on an usual Unix */
r = timeoutwrite(120, stdout, answer.s, answer.len);
if (r <= 0)
{
strerr_die2sys(111, FATAL, "unable to write to network: ");
}
/* free database */
cdb_free(&c);
close(fd);
}
else
{
*clean_query = '\0';
}
return 0;
}