-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfingerd.c
More file actions
477 lines (461 loc) · 11.9 KB
/
fingerd.c
File metadata and controls
477 lines (461 loc) · 11.9 KB
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* Copyright 1994 Michael Shanzer. All rights reserved.
* Copyright 2000-2019 Greg A. Woods <[email protected]. All rights reserved.
*
* Permission is granted to anyone to use this software for any purpose on any
* computer system, and to alter it and redistribute it freely, subject to the
* following restrictions:
*
* Use of this software constitutes acceptance for use in an AS IS condition.
* There are NO warranties with regard to this software. In no event shall the
* authors be held liable for any damages whatsoever arising out of, or in
* connection with, the use or performance of this software. Any use of this
* software is strictly at the user's own risk.
*
* The origin of this software must not be misrepresented, either by explicit
* claim or by omission. Since few users ever read sources, credits must also
* appear in the documentation.
*
* If you make modifications to this software that you feel increases it
* usefulness for the rest of the community, please email the changes,
* enhancements, bug fixes as well as any and all ideas to the authors. This
* software is going to be maintained and enhanced as deemed necessary by the
* community.
*
* Michael S. Shanzer
*
* Greg A. Woods
*/
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifdef HAVE_LIBWRAP
# include <tcpd.h> /* libwrap, for rfc931() & STRING_UNKNOWN */
#endif
#include "fingerd.h"
const char *argv0 = PACKAGE_NAME;
char cd[] = _PATH_SYSCONFDIR;
char *confdir = &cd[0];
extern char *optarg;
extern int optind;
extern int opterr;
#ifdef HAVE_LIBWRAP
/*
* We have to supply these variables for the library in case the program is
* linked dynamically as libwrap.so will require these symbols be available even
* if this program doesn't make use of any function in libwrap which needs those
* symbols.
*
* xxx this is kind of stupid, but a "normal" side-effect of dynamic linking to
* a library which requires the caller to define some global symbol(s).
*/
int allow_severity;
int deny_severity;
#endif
static void usage __P((void)) __dead;
int main __P((int, char *[]));
int
main(argc, argv)
int argc;
char *argv[];
{
struct hostent *hp;
struct sockaddr_in sin;
struct sockaddr_in laddr;
unsigned long perm = ACCESS_GRANTED;
FILE *fp;
char *lp;
bool eightbit = false;
bool logging = false;
bool forward = false;
bool nogecos = false;
bool nohome = false;
bool nolist = false;
bool noplan = false;
bool showhost = false;
bool doident = false;
bool forceident = false;
bool forceresolv = false;
bool forceshort = false;
bool defaultshort = false;
bool nomatch = false;
int ac = 1;
int ch;
socklen_t sval;
char *ap;
char *prog_finger;
char *rhost;
char *ruser = NULL;
char line[BUFSIZ];
char *av[BUFSIZ];
argv0 = (argv0 = strrchr(argv[0], '/')) ? argv0 + 1 : argv[0];
prog_finger = strdup(_PATH_FINGER);
openlog(argv0, LOG_PID | LOG_PERROR, FINGERD_SYSLOG_FACILITY);
optind = 1; /* Start options parsing */
opterr = 0;
while ((ch = getopt(argc, argv, "8bc:fghiIlmpP:rsSuV")) != EOF) {
switch (ch) {
case '8':
eightbit = true;
break;
case 'b':
nohome = true;
break;
case 'c':
confdir = optarg;
break;
case 'f':
forward = true;
break;
case 'g':
nogecos = true;
break;
case 'h':
showhost = true;
break;
case 'i':
doident = true;
#ifndef HAVE_LIBWRAP
syslog(LOG_WARN, "libwrap not supported -- ident impossible!");
#endif
break;
case 'I':
#ifndef HAVE_LIBWRAP
syslog(LOG_ERR, "libwrap not supported -- ident impossible!");
#endif
forceident = true;
break;
case 'l':
logging = true;
break;
case 'm':
nomatch = true;
break;
case 'p':
noplan = true;
break;
case 'P':
prog_finger = optarg;
break;
case 'r':
forceresolv = true;
break;
case 's':
defaultshort = true;
break;
case 'S':
forceshort = true;
break;
case 'u':
nolist = true;
break;
case 'V':
puts(version);
exit(0);
case '?':
usage();
/* NOTREACHED */
default:
syslog(LOG_ERR, "illegal option -- %c", ch);
usage();
/* NOTREACHED */
}
}
if (optind != argc) {
usage();
/* NOTREACHED */
}
sval = sizeof(sin);
if (getpeername(0, (struct sockaddr *) &sin, &sval) < 0) {
syslog(LOG_ERR, "getpeername: %m");
exit(1);
/* NOTREACHED */
}
if (getsockname(0, (struct sockaddr *) &laddr, &sval) < 0) {
syslog(LOG_ERR, "getsockname: %m");
exit(1);
/* NOTREACHED */
}
#ifdef HAVE_LIBWRAP
if (doident || forceident) {
char idreply[STRING_LENGTH + 1];
rfc931((struct sockaddr *) &sin, (struct sockaddr *) &laddr, idreply); /* XXX should use ident(3) et al */
if ((ruser = strdup(idreply)) == NULL) {
syslog(LOG_ERR, "strdup: %m");
exit(1);
/* NOTREACHED */
}
} else
#endif
{
if ((ruser = strdup(NO_IDENT_DONE)) == NULL) {
syslog(LOG_ERR, "strdup: %m");
exit(1);
/* NOTREACHED */
}
}
if (fgets(line, sizeof(line), stdin) == NULL) { /* do not use getline(3)! */
#ifdef DEBUG
/* can be too noisy for regular use? */
/* usually just indicates EOF? */
syslog(LOG_ERR, "fgets: %m");
#endif
exit(1);
/* NOTREACHED */
}
ap = strtok(line, "\r\n"); /* trim EOL */
if ((hp = gethostbyaddr((char *) &sin.sin_addr.s_addr,
sizeof(sin.sin_addr.s_addr), AF_INET))) {
/*
* The following is here to try and prevent people
* from putting in fake IN-ADDR records.
*/
if (!(rhost = strdup(hp->h_name))) {
syslog(LOG_ERR, "strdup: %m");
exit(1);
/* NOTREACHED */
}
if (!(hp = gethostbyname(rhost))) {
if (forceresolv) {
syslog(LOG_NOTICE,
"from=%s@[%s] to='%s'; Cannot re-resolve %s",
ruser, ap, inet_ntoa(sin.sin_addr), rhost);
printf("Possible name server mis-configuration.\nSorry, I cannot resolve your hostname '%s' [%s].",
rhost, inet_ntoa(sin.sin_addr));
exit(1);
/* NOTREACHED */
}
if (!(rhost = strdup(inet_ntoa(sin.sin_addr)))) {
syslog(LOG_ERR, "strdup: %m");
exit(1);
/* NOTREACHED */
}
} else {
struct in_addr *i;
int match = false;
while ((i = (struct in_addr *) *hp->h_addr_list++)) {
if (i->s_addr == sin.sin_addr.s_addr) {
match = true;
break;
}
}
if (!match) {
syslog(LOG_NOTICE,
"from=%s@[%s] to='%s'; host %s does not resolve to [%s].",
ruser, ap, inet_ntoa(sin.sin_addr), rhost, inet_ntoa(sin.sin_addr));
printf("Name server mis-configuration.\nYour host '%s' does not have the address [%s].\n",
rhost, inet_ntoa(sin.sin_addr));
exit(1);
/* NOTREACHED */
}
}
} else {
if (!(rhost = strdup(inet_ntoa(sin.sin_addr)))) {
syslog(LOG_ERR, "strdup: %m");
exit(1);
/* NOTREACHED */
}
if (forceresolv) {
syslog(LOG_NOTICE, "from=%s@[%s] to=[unknown] stat=Cannot resolve hostname",
ruser, rhost);
printf("Sorry, I cannot resolve your hostname from your address [%s].",
rhost);
exit(1);
/* NOTREACHED */
}
}
/* N.B.: Note we expect your inetd(8) to have already called hosts_access(3)!!! */
perm = access_check(ruser, rhost);
if (perm & ACCESS_DENIED) {
if (logging) {
syslog(LOG_NOTICE,
"from=%s@%s to=%s stat=Refused",
ruser, rhost, (ap ? ap : "[user list]"));
}
puts("Access to finger service denied.");
exit(1);
/* NOTREACHED */
}
/*
* Now we adjust options based on potentially more specific refinements
* in the access file. Note that the logic is designed in such a way
* as to make it impossible to override command-line options from the
* access file.
*/
if (perm & ACCESS_NOLIST)
nolist = true;
if (perm & ACCESS_NOPLAN)
noplan = true;
if (perm & ACCESS_FORWARD)
forward = true;
if (perm & ACCESS_NOGECOS)
nogecos = true;
if (perm & ACCESS_NOHOME)
nohome = true;
if (perm & ACCESS_NOMATCH)
nomatch = true;
if (perm & ACCESS_FORCEIDENT)
forceident = true;
if (perm & ACCESS_SHOWHOST)
showhost = true;
if (perm & ACCESS_FORCESHORT)
forceshort = true;
if (perm & ACCESS_DEFAULTSHORT)
defaultshort = true;
if (forceident && (strcmp(ruser, STRING_UNKNOWN) == 0 ||
strcmp(ruser, NO_IDENT_DONE) == 0)) {
if (logging) {
syslog(LOG_NOTICE,
"from=%s@%s to=%s; no identification available",
ruser, rhost, (ap ? ap : "[user list]"));
}
if (strcmp(ruser, NO_IDENT_DONE) == 0)
puts("Access to finger services denied. Valid identification not available.");
else
puts("Access to finger services denied. Valid identification reply required.");
exit(1);
/* NOTREACHED */
}
if ((fp = fopen(conf_file_path(FINGERD_MOTD_FILE), "r"))) {
char mline[BUFSIZ];
while (fgets(mline, sizeof(line), fp))
fputs(mline, stdout);
puts("----");
fclose(fp);
}
lp = ap; /* keep pointer to first token */
ap = strtok(lp, " "); /* split on possible space separator */
if (forceshort) {
/* ignore any '/W' request for verbose output */
if (!(av[ac++] = strdup("-s"))) {
syslog(LOG_ERR, "strdup: %m");
exit(1);
/* NOTREACHED */
}
} else if (ap && ap[0] == '/' && toupper(ap[1]) == 'W') {
if (!(av[ac++] = strdup("-l"))) {
syslog(LOG_ERR, "strdup: %m");
exit(1);
/* NOTREACHED */
}
ap = strtok((char *) NULL, " "); /* look for more tokens */
} else {
if (defaultshort) {
if (!(av[ac++] = strdup("-s"))) {
syslog(LOG_ERR, "strdup: %m");
exit(1);
/* NOTREACHED */
}
}
}
if (eightbit) {
if (!(av[ac++] = strdup("-8"))) {
syslog(LOG_ERR, "strdup: %m");
exit(1);
/* NOTREACHED */
}
}
if (nomatch) {
if (!(av[ac++] = strdup("-m"))) {
syslog(LOG_ERR, "strdup: %m");
exit(1);
/* NOTREACHED */
}
}
if (nogecos) {
if (!(av[ac++] = strdup("-g"))) {
syslog(LOG_ERR, "strdup: %m");
exit(1);
/* NOTREACHED */
}
}
if (nohome) {
if (!(av[ac++] = strdup("-b"))) { /* not in all finger(1)s */
syslog(LOG_ERR, "strdup: %m");
exit(1);
/* NOTREACHED */
}
}
if (noplan) {
if (!(av[ac++] = strdup("-p"))) {
syslog(LOG_ERR, "strdup: %m");
exit(1);
/* NOTREACHED */
}
}
if (showhost) {
if (!(av[ac++] = strdup("-h"))) {
syslog(LOG_ERR, "strdup: %m");
exit(1);
/* NOTREACHED */
}
}
if (!forward && (ap && strchr(ap, '@'))) {
if (logging) {
syslog(LOG_NOTICE,
"from=%s@%s to=%s stat=Refused Forwarding",
ruser, rhost, ap);
}
puts("Finger fowarding service denied.");
exit(1);
/* NOTREACHED */
}
if (nolist && (!ap || (ap && !*ap))) {
if (logging) {
syslog(LOG_NOTICE,
"from=%s@%s to=%s stat=Refused",
ruser, rhost, "[user list]");
}
puts("Finger of user list denied.");
exit(1);
/* NOTREACHED */
}
if (logging) {
syslog(LOG_NOTICE, "from=%s@%s to=%s stat=OK",
ruser, rhost, (ap ? ap : "[user list]"));
}
if (run_user_cmd(ap, ruser, rhost)) {
exit(0);
/* NOTREACHED */
} else {
if (ap) {
if (!(av[ac++] = strdup(ap))) {
syslog(LOG_ERR, "strdup: %m");
exit(1);
/* NOTREACHED */
}
}
}
av[ac] = NULL; /* no more arguments! ;-) */
lp = (lp = strrchr(prog_finger, '/')) ? (lp + 1) : prog_finger;
if (!(av[0] = strdup(lp))) {
syslog(LOG_ERR, "strdup: %m");
exit(1);
/* NOTREACHED */
}
run_program(prog_finger, av);
/* NOTREACHED */
}
static void
usage()
{
#ifdef HAVE_LIBWRAP
syslog(LOG_WARNING, "Usage: fingerd [-c -confdir] [-P -finger] [-bfglmpru] [-h | -H] [-i | -I] [-s | -S]");
#else
syslog(LOG_WARNING, "Usage: fingerd [-c -confdir] [-P -finger] [-bfglmpru] [-h | -H] [-s | -S]");
#endif
exit(2);
/* NOTREACHED */
}