-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy paths_auth.c
373 lines (327 loc) · 9.15 KB
/
s_auth.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
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
/************************************************************************
* IRC - Internet Relay Chat, src/s_auth.c
* Copyright (C) 1992 Darren Reed
*
* 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.
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "struct.h"
#include "common.h"
#include "sys.h"
#include "res.h"
#include "numeric.h"
#include "patchlevel.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#if defined(__hpux)
#include "inet.h"
#endif
#include <fcntl.h>
#include "sock.h" /* If FD_ZERO isn't define up to this point */
/* define it (BSD4.2 needs this) */
#include "h.h"
#include "fdlist.h"
#include "fds.h"
static void authsenderr(aClient *);
/*
* start_auth
*
* Flag the client to show that an attempt to contact the ident server on
* the client's host. The connect and subsequently the socket are all
* put into 'non-blocking' mode. Should the connect or any later phase
* of the identifing process fail, it is aborted and the user is given
* a username of "unknown".
*/
void start_auth(aClient *cptr)
{
union
{
struct sockaddr sa;
struct sockaddr_in addr4;
struct sockaddr_in6 addr6;
} sock;
union
{
struct sockaddr sa;
struct sockaddr_in addr4;
struct sockaddr_in6 addr6;
} localaddr;
unsigned int locallen;
Debug((DEBUG_NOTICE, "start_auth(%x) fd %d status %d",
cptr, cptr->fd, cptr->status));
if ((cptr->authfd = socket(cptr->ip_family, SOCK_STREAM, 0)) == -1)
{
#ifdef USE_SYSLOG
syslog(LOG_ERR, "Unable to create auth socket for %s:%m",
get_client_name(cptr, IsServer(cptr) ? HIDEME : TRUE));
#endif
ircstp->is_abad++;
return;
}
if (cptr->authfd >= MAXCONNECTIONS)
{
sendto_realops_lev(DEBUG_LEV,"Can't allocate fd for auth on %s",
get_client_name(cptr, (IsServer(cptr) ? HIDEME : TRUE)));
close(cptr->authfd);
cptr->authfd = -1;
return;
}
#ifdef SHOW_HEADERS
sendto_one(cptr, "%s", REPORT_DO_ID);
#endif
set_non_blocking(cptr->authfd, cptr);
/*
* get the local address of the client and bind to that to make the
* auth request. This used to be done only for ifdef VIRTTUAL_HOST,
* but needs to be done for all clients since the ident request must
* originate from that same address-- and machines with multiple IP
* addresses are common now
*/
locallen = sizeof(localaddr);
memset(&localaddr, '\0', sizeof(localaddr));
getsockname(cptr->fd, &localaddr.sa, &locallen);
if (localaddr.sa.sa_family == AF_INET)
{
localaddr.addr4.sin_port = htons(0);
locallen = sizeof(localaddr.addr4);
}
else if (localaddr.sa.sa_family == AF_INET6)
{
localaddr.addr6.sin6_port = htons(0);
locallen = sizeof(localaddr.addr6);
}
if (bind(cptr->authfd, &localaddr.sa, locallen) == -1)
{
report_error("binding auth stream socket %s:%s", cptr);
close(cptr->authfd);
cptr->authfd = -1;
return;
}
memset(&sock, '\0', sizeof(sock));
if (cptr->ip_family == AF_INET)
{
memcpy((char *) &sock.addr4.sin_addr, (char *) &cptr->ip.ip4,
sizeof(struct in_addr));
sock.addr4.sin_port = htons(113);
sock.addr4.sin_family = AF_INET;
}
else if (cptr->ip_family == AF_INET6)
{
memcpy((char *) &sock.addr6.sin6_addr, (char *) &cptr->ip.ip6,
sizeof(struct in6_addr));
sock.addr6.sin6_port = htons(113);
sock.addr6.sin6_family = AF_INET6;
}
if (connect(cptr->authfd, &sock.sa,
locallen) == -1 && errno != EINPROGRESS)
{
ircstp->is_abad++;
/* No error report from this... */
close(cptr->authfd);
cptr->authfd = -1;
#ifdef SHOW_HEADERS
sendto_one(cptr, "%s", REPORT_FAIL_ID);
#endif
return;
}
cptr->flags |= (FLAGS_WRAUTH | FLAGS_AUTH);
if (cptr->authfd > highest_fd)
highest_fd = cptr->authfd;
add_fd(cptr->authfd, FDT_AUTH, cptr);
return;
}
/*
* send_authports
*
* Send the ident server a query giving "theirport , ourport". The write
* is only attempted *once* so it is deemed to be a fail if the entire
* write doesn't write all the data given. This shouldnt be a problem
* since the socket should have a write buffer far greater than this
* message to store it in should problems arise. -avalon
*/
void send_authports(aClient *cptr)
{
union
{
struct sockaddr sa;
struct sockaddr_in addr4;
struct sockaddr_in6 addr6;
} us;
union
{
struct sockaddr sa;
struct sockaddr_in addr4;
struct sockaddr_in6 addr6;
} them;
char authbuf[32];
unsigned int ulen = sizeof(us), tlen = sizeof(them);
int slen;
Debug((DEBUG_NOTICE, "write_authports(%x) fd %d authfd %d stat %d",
cptr, cptr->fd, cptr->authfd, cptr->status));
if (getsockname(cptr->fd, &us.sa, &ulen) ||
getpeername(cptr->fd, &them.sa, &tlen))
{
#ifdef USE_SYSLOG
syslog(LOG_DEBUG, "auth get{sock,peer}name error for %s:%m",
get_client_name(cptr, IsServer(cptr) ? HIDEME : TRUE));
#endif
authsenderr(cptr);
return;
}
if (us.sa.sa_family == AF_INET)
{
(void) ircsprintf(authbuf, "%u , %u\r\n",
(unsigned int) ntohs(them.addr4.sin_port),
(unsigned int) ntohs(us.addr4.sin_port));
Debug((DEBUG_SEND, "sending [%s] to auth port %s.113",
authbuf, inetntoa((char *) &them.sin_addr)));
}
else if (us.sa.sa_family == AF_INET6)
{
(void) ircsprintf(authbuf, "%u , %u\r\n",
(unsigned int) ntohs(them.addr6.sin6_port),
(unsigned int) ntohs(us.addr6.sin6_port));
Debug((DEBUG_SEND, "sending [%s] to auth port %s.113",
authbuf, inet6ntoa((char *) &them.sin_addr)));
}
slen = strlen(authbuf);
if (send(cptr->authfd, authbuf, slen, 0) != slen) {
authsenderr(cptr);
return;
}
cptr->flags &= ~FLAGS_WRAUTH;
return;
}
/*
* authsenderr() *
* input - pointer to aClient output
*/
static void authsenderr(aClient *cptr)
{
ircstp->is_abad++;
del_fd(cptr->authfd);
close(cptr->authfd);
if (cptr->authfd == highest_fd)
while (!local[highest_fd])
highest_fd--;
cptr->authfd = -1;
cptr->flags &= ~(FLAGS_AUTH | FLAGS_WRAUTH);
#ifdef SHOW_HEADERS
sendto_one(cptr, "%s", REPORT_FAIL_ID);
#endif
return;
}
/*
* read_authports
*
* read the reply (if any) from the ident server we connected to. The
* actual read processing here is pretty weak - no handling of the
* reply if it is fragmented by IP.
*
* Whoever wrote this code should be shot.
* Looks like it's trouncing on memory it shouldn't be.
* Rewriting, some credit goes to wd for saving me time with his code.
* - lucas
*/
#define AUTHBUFLEN 128
void read_authports(aClient *cptr)
{
char buf[AUTHBUFLEN], usern[USERLEN + 1];
int len, userncnt;
char *userid = "", *s, *reply, *os, *tmp;
int rejected = 0;
len = recv(cptr->authfd, buf, AUTHBUFLEN, 0);
if(len > 0)
{
do
{
if(buf[len - 1] != '\n')
break;
buf[--len] = '\0';
if(len == 0)
break;
if(buf[len - 1] == '\r')
buf[--len] = '\0';
if(len == 0)
break;
s = strchr(buf, ':');
if(!s)
break;
s++;
while(IsSpace(*s))
s++;
reply = s;
if(strncmp(reply, "USERID", 6))
break;
s = strchr(reply, ':');
if(!s)
break;
s++;
while(IsSpace(*s))
s++;
os = s;
s = strchr(os, ':');
if(!s)
break;
s++;
while(IsSpace(*s))
s++;
/* hack to reject pidentd encryption */
if (strlen(s) == 34)
{
rejected = 1;
break;
}
userid = tmp = usern;
/* s is the pointer to the beginning of the userid field */
for(userncnt = USERLEN; *s && userncnt; s++)
{
if(*s == '@')
break;
if(!IsSpace(*s) && *s != ':')
{
*tmp++ = *s;
userncnt--;
}
}
*tmp = '\0';
} while(0);
}
del_fd(cptr->authfd);
close(cptr->authfd);
if (cptr->authfd == highest_fd)
while (!local[highest_fd])
highest_fd--;
cptr->authfd = -1;
ClearAuth(cptr);
if (rejected || !*userid)
{
ircstp->is_abad++;
strcpy(cptr->username, "unknown");
#ifdef SHOW_HEADERS
sendto_one(cptr, "%s", rejected ? REPORT_REJECT_ID : REPORT_FAIL_ID);
#endif
return;
}
#ifdef SHOW_HEADERS
else
sendto_one(cptr, "%s", REPORT_FIN_ID);
#endif
ircstp->is_asuc++;
strncpyzt(cptr->username, userid, USERLEN + 1);
cptr->flags |= FLAGS_GOTID;
return;
}