File: main.c

package info (click to toggle)
cfingerd 1.4.3-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 964 kB
  • sloc: ansic: 3,776; perl: 501; makefile: 137; sh: 73
file content (328 lines) | stat: -rw-r--r-- 9,450 bytes parent folder | download | duplicates (7)
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
/*
 * CFINGERD
 * Main Routine
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 1, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "cfingerd.h"
#include "proto.h"

#define MAIN
#include "privs.h"

CONFIG prog_config;
char *localhost, *ident_user;
char ip_address[INET6_ADDRSTRLEN] = "";
char remote_addr[INET6_ADDRSTRLEN] = "";
FILE *top_display, *bottom_display, *noname_display, *nouser_display,
     *rejected_display, *identd_display;
BOOL local_finger, emulated; 
unsigned short local_port, remote_port;
unsigned short listen_port;
unsigned long listen_addr;

/*
 * HAS_MATCH
 *
 * Check to see if there's an occurance of "match" within "string".  You're
 * probably saying that I could have used strncmp, or something else
 * similar.  Not true - because I'd need about 3 calls in here, and this
 * routine does everything I need in *1* routine.  :)
 */
BOOL has_match(char *string, char *match)
{
    int i, x;

    for (i = 0; i < strlen(string); i++)
	for (x = 0; x < strlen(match); x++)
	    if (string[i] == match[x])
		return TRUE;

    return FALSE;
}

/*
 * MAIN
 *
 * The main dispatcher for cfingerd.
 */
int main(int argc, char *argv[])
{
    /*
     * The length restrictions are limited.  If lengths are changed all
     * subsequent routines need to be investigated.
     */
    char line[100], username[80], syslog_str[200];
    int un_type;
    char *cp;
    struct sockaddr_storage local_addr;
    struct sockaddr_in *sloc4 = (struct sockaddr_in *) &local_addr;
    struct sockaddr_in6 *sloc6 = (struct sockaddr_in6 *) &local_addr;
    struct servent *serv;

    if ((serv = getservbyname("finger","tcp")) != NULL)
	listen_port = serv->s_port;
    else
	listen_port = htons(79);

    /* Initialize CFINGERD */
    start_handler();
    start_cfingerd();

    /* Clear any extraneous bits that should be cleared */
    can_log = FALSE;

    memset(line, 0, sizeof(line));
    memset(username, 0, sizeof(username));
    memset(syslog_str, 0, sizeof(syslog_str));

    /* Check for any command-line options */
    if (argc > 1) {
	(void) check_options(argc, argv);
	if (argc > 2)
	    strncpy(username, argv[2], sizeof(username));
    }

    /* Read the configuration, and check status of the program so far */
    read_configuration();
    check_blank_configurations();
    check_stats();
    open_initial_files();

    /*
     * Okay, now that all of the standard processing has taken place, we
     * can switch to the NOBODY UID/GID and run everything else as nobody
     * (to a certain degree.)  Remembering, of course, if we must switch
     * to a user that's being fingered, we MUST call USER_PRIVS(UID,GID)
     * to obtain privileges as that user.  (Read the FAQ for more info.)
     */

    NOBODY_PRIVS;

    /* Make sure there is actually data waiting in the finger port */
    if (!emulated) {
	if (!fgets(username, sizeof(username), stdin)) {
	  if (remote_addr != NULL && *remote_addr) {
	    syslog(LOG_ERR, "Null query from %s: %m", remote_addr);
	    mylog(LOG_ERROR, remote_addr, strerror(errno));
	  } else {
	    syslog(LOG_ERR, "Null query: %m");
	    mylog(LOG_ERROR, strerror(errno), strerror(0));
	  }
	    closelog();
	    exit(PROGRAM_SYSLOG);
	}
    }

    /*
     * RFC 1288 
     *
     * 2.5.4.  /W query token
     * 
     *   The token /W in the {Q1} or {Q2} query types SHOULD at best be
     *   interpreted at the last RUIP to signify a higher level of verbosity
     *   in the user information output, or at worst be ignored.
     *
     * As the normal behaviour of cfingerd is to show all information we
     * can't set a higher level of verbosity - therefore this parameter
     * is being ignored.
     */
    if ( strlen(username) && !strncmp(username, "/W ", 3) ) {
      cp = username;
      cp += 3;
      strcpy (line, cp);
      strcpy (username, line);
    }

    /* If there's a username, trim the CRLF pair */
    if (!emulated)
	sscanf(username, "%[^\r\n]\r\n", username);

    /* If we're not doing emulated stuff, we can assume that we are running
       either as a daemon, or under INETD.  In that case... */
    if (!emulated) {
	struct sockaddr_storage socket_addr;
	struct sockaddr_in *srem4 = (struct sockaddr_in *) &socket_addr;
	struct sockaddr_in6 *srem6 = (struct sockaddr_in6 *) &socket_addr;
	struct hostent *host_ent;
	socklen_t locsize = 0, remsize = 0;

	/* Can't run from command line (but this should already be checked) */
	locsize = sizeof(local_addr);

	if (getsockname(0, (struct sockaddr *) &local_addr, &locsize)) {
	    syslog(LOG_WARNING, "getsockname: %s", strerror(errno));
	    local_port = 0;
	} else
	    switch (local_addr.ss_family) {
		case AF_INET6:
		    local_port = ntohs(sloc6->sin6_port);
		    break;
		case AF_INET:
		default:
		    local_port = ntohs(sloc4->sin_port);
	    }

	remsize = sizeof(socket_addr);

	if (getpeername(0, (struct sockaddr *) &socket_addr, &remsize)) {
	    printf("Internal error - not running as either a daemon or under INETD.\n");
	    printf("Fatal - getpeername: %s\n", strerror(errno));
	    closelog();
	    mylog(LOG_ERROR, "getpeername: ", strerror(errno));
	    exit(PROGRAM_BUG);
	} else
	    switch (socket_addr.ss_family) {
		case AF_INET6:
		    remote_port = ntohs(srem6->sin6_port);
		    break;
		case AF_INET:
		default:
		    remote_port = ntohs(srem4->sin_port);
	    }

	inet_ntop(socket_addr.ss_family, &socket_addr,
			ip_address, INET6_ADDRSTRLEN);

	/* Get our host entry */
	switch (socket_addr.ss_family) {
	    case AF_INET6:
		host_ent = (struct hostent *)
				gethostbyaddr((char *) &srem6->sin6_addr,
					sizeof(struct in6_addr),
					socket_addr.ss_family);
		break;
	    case AF_INET:
	    default:
		host_ent = (struct hostent *)
				gethostbyaddr((char *) &srem4->sin_addr,
					sizeof(struct in_addr),
					socket_addr.ss_family);
	}

	/* And get our local-host name */
#ifndef	ACTUAL_HOSTNAME
	localhost = get_localhost();
#else
	localhost = ACTUAL_HOSTNAME;
#endif

	/* Make sure we can get the remote host's address name */
	if (host_ent == NULL) {
	    strncpy(remote_addr, ip_address, INET6_ADDRSTRLEN);
	    syslog(LOG_WARNING, "%s %s", 
		prog_config.p_strings[D_IP_NO_MATCH],
		remote_addr);
	    if (!(prog_config.config_bits2 & SHOW_IP_MATCH))
		CF_ERROR(E_NOIP);
	} else
	    strncpy(remote_addr, host_ent->h_name, INET6_ADDRSTRLEN);

	/* Convert any uppercase letters in the hostname to lowercase */
	for (cp = remote_addr; *cp; cp++)
	    if (isupper(*cp))
		*cp = tolower(*cp);

	/* And find out if this is a local finger */
	if (!strncasecmp(remote_addr, "127.0.0.1", 9) ||
	    !strncasecmp(remote_addr, "::1", 3) ||
	    !strncasecmp(remote_addr, "localhost", 9) ||
	    !strncasecmp(remote_addr, "ip6-localhost", 9) ||
	    !strncasecmp(remote_addr, "ip6-loopback", 9) ||
	    !strncasecmp(remote_addr, "127.0.0.0", 9) || /* KTH 07/26/96 */
	    !strncasecmp(remote_addr, localhost, strlen(localhost)))
	    local_finger = TRUE;
	else
	    local_finger = FALSE;

	ident_user = get_rfc1413_data(&local_addr, &socket_addr);
	set_time_format();

    } else
	local_finger = TRUE;

    /* We're emulated, so assume we're fingering from loopback */
    if (emulated) {
	local_finger = TRUE;
	ident_user = (char *)malloc(sizeof("emulated")+1);
	memset (ident_user, 0, sizeof (ident_user));
	strcpy (ident_user, "emulated");
#ifndef	ACTUAL_LOOPBACK
	strcpy(remote_addr,"127.0.0.1");
#else
	strcpy(remote_addr, ACTUAL_LOOPBACK);
#endif
    }

    /* Now, let's check to make sure this site is trusted */
    if ((!local_finger) && !emulated)
	local_finger = check_trusted(remote_addr);

    if ((!local_finger) && !emulated)
	check_rejected(remote_addr);

    /* Check the finger information coming in and return its type */
    un_type = process_username(username);

    /* And check the process the information here. */
    switch(un_type) {
	case U_INTERNAL:
	    if (!emulated) {
		snprintf(syslog_str, sizeof(syslog_str), "%s fingered (internal) from %s", username,
		    ident_user);
		syslog(LOG_NOTICE, "%s", (char *) syslog_str);
	    }

	    handle_internal(username);
	    break;
	case U_STANDARD:
	    if (!emulated) {
		if (strncmp(username, "root", 4))
		    snprintf(syslog_str, sizeof(syslog_str), "%s fingered from %s", username, ident_user);
		else
		    snprintf(syslog_str, sizeof(syslog_str), "%s fingered from %s",
			prog_config.p_strings[D_ROOT_FINGER], ident_user);

		syslog(LOG_NOTICE, "%s", (char *) syslog_str);
	    }

	    handle_standard(username);
	    break;
	case U_FAKEUSER:
	    if (!emulated) {
		snprintf(syslog_str, sizeof(syslog_str), "%s %s from %s", username, 
		    prog_config.p_strings[D_FAKE_USER], ident_user);

		syslog(LOG_NOTICE, "%s", (char *) syslog_str);
	    }

	    handle_fakeuser(username);
	    break;
	case U_USERLIST:
	    if (!emulated)
		syslog(LOG_NOTICE, "%s from %s", prog_config.p_strings[D_ULIST_FINGER],
		    ident_user);

	    handle_userlist(username);
	    break;
    }

    return(0);
}

/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 8
 * End:
 */