File: common.c

package info (click to toggle)
ident2 1.05-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 220 kB
  • ctags: 75
  • sloc: ansic: 833; sh: 199; makefile: 80
file content (311 lines) | stat: -rw-r--r-- 7,080 bytes parent folder | download | duplicates (6)
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
/*
 * Ident-2 - an Identity server for UNIX
 * Copyright (C) 1998-2001 Michael Bacarella
 * Copyright (C) 2003 Netgraft Corporation
 *
 * 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 2
 * of the License, 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Please view the file README for program information.
 *
 *	Common function definitions.
 *	Used without regulation by other modules
 */

#include "ident2.h"

void *
xmalloc (size_t nb)
{
	void *p = malloc (nb);
	
	if (p == NULL) {
		fprintf (stderr, "ident2: terminating on memory allocation error\n");
		syslog (LOG_ERR, "error: memory allocation error");
		exit (1);
	}
	return p;
}

	/*
	 *	a (skewed) fgets() that works on file descriptors
	 *	the '\r' charecter is ignored
	 *	returns the number of bytes written into the given
	 *	 buffer, including the terminating NUL
	 */
static int
_getl (int d, char *begin, u_short len)
{
	char *p, *end;

	end = &begin[len-1]; /* leave room for terminating NUL */
	for (p = begin; p < end; ++p) {
		if (read (d, p, 1) != 1)
			break;
		if (*p == '\n')
			break;
		if (*p == '\r')
			p--;	/* ignore \r */
	}
	*p++ = 0;
	return p-begin;
}

	/*
	 *	this function is called directly as an inetd
	 *	handler as well as a fork()ed into by the daemon
	 *	service of the server..
	 */
void 
child_service (int in, int out)
{
	char buf[ID_BUF_SIZE];

	signal (SIGALRM, exit);
	alarm (Client_Timeout);

	if (m_reduce_rights () == -1)
		syslog (LOG_ERR, "error: cannot reduce self's rights "
			"[m_reduce_rights()]");
	else
		while (_getl (in, buf, ID_BUF_SIZE) != 0)
			nexus (out, buf);

	close (in);
	if (out != in)
		close (out);
}


typedef struct cl_t {
	int			sd;
	uid_t			uid;
	unsigned short		lp, rp;
	struct sockaddr_in	laddr, raddr;
	struct passwd		*pw;
} cl_t;

	/*
	 *      send an ident formatted reply to client
	 */
static void
_clreply (cl_t *cl, char *rslt, char *info)
{
	char buf[ID_BUF_SIZE+1];

	buf[ID_BUF_SIZE] = 0;
	
	snprintf (buf, ID_BUF_SIZE-1, "%d , %d : %s : %s\r\n",
		cl->lp, cl->rp, rslt, info);
	write (cl->sd, buf, strlen (buf));
}


static int
_check_noident (cl_t *cl)
{
	char p[ID_BUF_SIZE+1];
	
	p[ID_BUF_SIZE] = 0;
	
	snprintf (p, ID_BUF_SIZE-1, "%s/%s", cl->pw->pw_dir, NOIDENT_FILE);
	
	if (access (p, F_OK) == 0) {
		_clreply (cl, "ERROR", "HIDDEN-USER");
		syslog (LOG_INFO, "no reply to %s (query %d, %d) due to %s",
				inet_ntoa (cl->raddr.sin_addr),
				cl->lp, cl->rp, p);
		return 0;
 	}
	return -1;
}

static int
_check_user_ident (cl_t *cl)
{
	FILE *fp;
	int retval = -1;
	char idfile[ID_BUF_SIZE+1];
	
	idfile[ID_BUF_SIZE] = 0;
	snprintf (idfile, ID_BUF_SIZE-1, "%s/%s",
		cl->pw->pw_dir, User_Ident_File);
	
	if (access (idfile, R_OK) == 0) {
		if ((fp = fopen (idfile, "r")) != NULL) {
			char *p, buf[4096];
			
			memset (buf, 0, 4096);	
			fgets (buf, 4095, fp);
			strtok (buf, "\r\n");
			
			if ((p = strstr (buf, "ident "))) {
				char rply[ID_BUF_SIZE+1];
				rply[ID_BUF_SIZE] = 0;
				
				snprintf (rply, ID_BUF_SIZE-1, "UNIX : %s", p+6);
				_clreply (cl, "USERID", rply);
				
				syslog (LOG_INFO, "sent reply `%s' to query %s "
					"(%d, %d), uid = %d", p,
					inet_ntoa (cl->raddr.sin_addr),
					cl->lp, cl->rp, cl->uid);
				retval = 0;
			}
			fclose (fp);		
		}
	}
	return retval;
}	

	/*
	 * handy for users who use an ip masqueraded setup that just
	 * want to friggin get on IRC and don't care what their ident
	 * is. Or if you want to annoy your IRC admins, because they
	 * totally deserve it.
	 */
static void
_send_random_reply (cl_t *cl)
{
	char randstr[RAND_STRING_LENGTH+1];
	char buf[ID_BUF_SIZE+1];
	size_t i;

	buf[ID_BUF_SIZE] = 0;
	
	srand (time(NULL));
	for (i = 0; i < RAND_STRING_LENGTH; i++) {
		randstr[i] = rand();
		while (randstr[i] > 'z')
			randstr[i] -= 26;
		while (randstr[i] < 'a')
			randstr[i] += 26;
	}
	randstr[RAND_STRING_LENGTH] = 0;
 	
 	snprintf (buf, ID_BUF_SIZE-1, "UNIX : %s", randstr);

	_clreply (cl, "USERID", buf);
	syslog (LOG_INFO, "sent random reply for query %s (%d, %d)",
		inet_ntoa (cl->raddr.sin_addr), cl->lp, cl->rp);
	
	return;
}
	/*
	 *	determines and collects client information
	 *	and returns it in a neat and conveniant package
	 */	
static cl_t *
_new_cl (int sd, char *line)
{
	int uid, ssiz = sizeof (struct sockaddr);
	cl_t *p, cl;
	char *s;
	
	cl.sd = sd;
	cl.lp = cl.rp = 0;
	
	if (!(s = strchr (line, ','))) {
		_clreply (&cl, "ERROR", "INVALID-PORT");
		return NULL;
	}		
	*s = 0;
	cl.lp = (u_short) atoi (line);
	cl.rp = (u_short) atoi (s+1);

	if (getsockname (sd, (struct sockaddr *)&cl.laddr, &ssiz) == -1) {
		syslog (LOG_WARNING, "warning: getsockname(): %s\n",
			strerror (errno));
                _clreply (&cl, "ERROR", "UNKNOWN-ERROR");
                return NULL;
	}
	if (getpeername (sd, (struct sockaddr *)&cl.raddr, &ssiz) == -1) {
		syslog (LOG_WARNING, "warning: getpeername(): %s\n",
			strerror (errno));
		_clreply (&cl, "ERROR", "UNKNOWN-ERROR");
		return NULL;
        }

	if (Reply_Always_Random == TRUE) {
		_send_random_reply (&cl);
		return NULL;
	}

	uid = m_get_uid (&cl.laddr.sin_addr, cl.lp, &cl.raddr.sin_addr, cl.rp);
	if (uid == -1) {
		syslog (LOG_WARNING, "warning: bad request: %d/%d, from %s",
			cl.lp, cl.rp, inet_ntoa (cl.raddr.sin_addr));
		_clreply (&cl, "ERROR", "NO-USER");
		return NULL;
	}
	cl.uid = uid;
		
	if ((cl.pw = getpwuid (cl.uid)) == NULL) {
		syslog (LOG_ERR, "warning: cannot map %d to a username: %s "
			"requested by query %s (%d, %d)", cl.uid,
				strerror (errno), inet_ntoa (cl.raddr.sin_addr),
				cl.lp, cl.rp);
		_clreply (&cl, "ERROR", "UNKNOWN-ERROR");
		return NULL;
	}
	p = xmalloc (sizeof (cl_t));
	*p = cl;
	
	return p;				
}

	/**
	 **	nexus
	 **
	 **	this is called by all service modes if
	 **	any reply is to be returned to the
	 **	client.
	 **/
void
nexus (int sd, char *line)
{
	cl_t *cl;
	char buf[ID_BUF_SIZE+1];

	buf[ID_BUF_SIZE] = 0;
	
	if ((cl = _new_cl (sd, line)) == NULL)
		return;

		/* if user configs are allowed, use the user's (if they
		 * have one, else just use the global config */
	if (Allow_NOIDENT == TRUE)
		if (!_check_noident (cl)) {
			free (cl);
			return;
		}
		
	if (Use_User_Ident == TRUE)
		if (!_check_user_ident (cl)) {
			free (cl);
			return;
		}
	
	snprintf (buf, ID_BUF_SIZE-1, "UNIX : %s", cl->pw->pw_name);
	
	_clreply (cl, "USERID", buf);
	syslog (LOG_INFO, "sent reply `%s' to query %s "
			"(%d, %d), uid = %d", cl->pw->pw_name,
			inet_ntoa (cl->raddr.sin_addr), cl->lp,
			cl->rp, cl->uid);

	free (cl);
	return;		
}