Author: Run <carlo@alinoe.com>
[ircu2.10.12-pk.git] / ircd / s_auth.c
1 /*
2  * IRC - Internet Relay Chat, ircd/s_auth.c
3  * Copyright (C) 1992 Darren Reed
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 1, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include "sys.h"
21 #include <sys/socket.h>
22 #if HAVE_SYS_FILE_H
23 #include <sys/file.h>
24 #endif
25 #if HAVE_SYS_IOCTL_H
26 #include <sys/ioctl.h>
27 #endif
28 #ifdef  UNIXPORT
29 #include <sys/un.h>
30 #endif
31 #if HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #ifdef HPUX
35 #include <arpa/inet.h>
36 #endif /* HPUX */
37 #if HAVE_FCNTL_H
38 #include <fcntl.h>
39 #endif
40 #ifdef USE_SYSLOG
41 #include <syslog.h>
42 #endif
43 #include "h.h"
44 #include "res.h"
45 #include "struct.h"
46 #include "common.h"
47 #include "send.h"
48 #include "s_bsd.h"
49 #include "s_misc.h"
50 #include "support.h"
51 #include "ircd.h"
52 #include "s_auth.h"
53 #include "sprintf_irc.h"
54
55 RCSTAG_CC("$Id$");
56
57 /*
58  * start_auth
59  *
60  * Flag the client to show that an attempt to contact the ident server on
61  * the client's host.  The connect and subsequently the socket are all put
62  * into 'non-blocking' mode.  Should the connect or any later phase of the
63  * identifing process fail, it is aborted and the user is given a username
64  * of "unknown".
65  */
66 void start_auth(aClient *cptr)
67 {
68   struct sockaddr_in sock;
69   int err, len;
70
71   Debug((DEBUG_NOTICE, "start_auth(%p) fd %d status %d",
72       cptr, cptr->fd, cptr->status));
73
74   alarm(2);
75   cptr->authfd = socket(AF_INET, SOCK_STREAM, 0);
76   err = errno;
77   alarm(0);
78   if (cptr->authfd < 0 && err == EAGAIN)
79     sendto_ops("Can't allocate fd for auth on %s : socket: No more sockets",
80         get_client_name(cptr, TRUE));
81
82   if (cptr->authfd < 0)
83   {
84 #ifdef  USE_SYSLOG
85     syslog(LOG_ERR, "Unable to create auth socket for %s:%m",
86         get_client_name(cptr, TRUE));
87 #endif
88     Debug((DEBUG_ERROR, "Unable to create auth socket for %s:%s",
89         get_client_name(cptr, TRUE), strerror(get_sockerr(cptr))));
90     if (!DoingDNS(cptr))
91       SetAccess(cptr);
92     ircstp->is_abad++;
93     return;
94   }
95   if (cptr->authfd >= (MAXCONNECTIONS - 2))
96   {
97     sendto_ops("Can't allocate fd for auth on %s", get_client_name(cptr, TRUE));
98     close(cptr->authfd);
99     return;
100   }
101
102   set_non_blocking(cptr->authfd, cptr);
103
104   if (getsockname(cptr->fd, (struct sockaddr *)&sock, &len) == -1
105       || (sock.sin_port = 0)    /* Reset sin_port and let OS choose the port */
106       || bind(cptr->authfd, (struct sockaddr *)&sock, len) == -1)
107   {
108     report_error("binding auth stream socket %s: %s", cptr);
109     close(cptr->fd);
110     return;
111   }
112
113   memcpy(&sock.sin_addr, &cptr->ip, sizeof(struct in_addr));
114
115   sock.sin_port = htons(113);
116   sock.sin_family = AF_INET;
117
118   alarm((unsigned)4);
119   if (connect(cptr->authfd, (struct sockaddr *)&sock,
120       sizeof(sock)) == -1 && errno != EINPROGRESS)
121   {
122     ircstp->is_abad++;
123     /*
124      * No error report from this...
125      */
126     alarm((unsigned)0);
127     close(cptr->authfd);
128     cptr->authfd = -1;
129     if (!DoingDNS(cptr))
130       SetAccess(cptr);
131     return;
132   }
133   alarm((unsigned)0);
134   cptr->flags |= (FLAGS_WRAUTH | FLAGS_AUTH);
135   if (cptr->authfd > highest_fd)
136     highest_fd = cptr->authfd;
137   return;
138 }
139
140 /*
141  * send_authports
142  *
143  * Send the ident server a query giving "theirport , ourport".
144  * The write is only attempted *once* so it is deemed to be a fail if the
145  * entire write doesn't write all the data given.  This shouldnt be a
146  * problem since the socket should have a write buffer far greater than
147  * this message to store it in should problems arise. -avalon
148  */
149 void send_authports(aClient *cptr)
150 {
151   struct sockaddr_in us, them;
152   char authbuf[32];
153   size_t ulen, tlen;
154
155   Debug((DEBUG_NOTICE, "write_authports(%p) fd %d authfd %d stat %d",
156       cptr, cptr->fd, cptr->authfd, cptr->status));
157   tlen = ulen = sizeof(us);
158   if (getsockname(cptr->fd, (struct sockaddr *)&us, &ulen) ||
159       getpeername(cptr->fd, (struct sockaddr *)&them, &tlen))
160   {
161 #ifdef  USE_SYSLOG
162     syslog(LOG_ERR, "auth get{sock,peer}name error for %s:%m",
163         get_client_name(cptr, TRUE));
164 #endif
165     goto authsenderr;
166   }
167
168   sprintf_irc(authbuf, "%u , %u\r\n", (unsigned int)ntohs(them.sin_port),
169       (unsigned int)ntohs(us.sin_port));
170
171   Debug((DEBUG_SEND, "sending [%s] to auth port %s.113",
172       authbuf, inetntoa(them.sin_addr)));
173   if (write(cptr->authfd, authbuf, strlen(authbuf)) != (int)strlen(authbuf))
174   {
175   authsenderr:
176     ircstp->is_abad++;
177     close(cptr->authfd);
178     if (cptr->authfd == highest_fd)
179       while (!loc_clients[highest_fd])
180         highest_fd--;
181     cptr->authfd = -1;
182     cptr->flags &= ~FLAGS_AUTH;
183     if (!DoingDNS(cptr))
184       SetAccess(cptr);
185   }
186   cptr->flags &= ~FLAGS_WRAUTH;
187   return;
188 }
189
190 /*
191  * read_authports
192  *
193  * read the reply (if any) from the ident server we connected to.
194  * The actual read processijng here is pretty weak - no handling of the reply
195  * if it is fragmented by IP.
196  */
197 void read_authports(aClient *cptr)
198 {
199   Reg1 char *s, *t;
200   Reg2 int len;
201   char ruser[USERLEN + 1], system[8];
202   unsigned short int remp = 0, locp = 0;
203
204   *system = *ruser = '\0';
205   Debug((DEBUG_NOTICE, "read_authports(%p) fd %d authfd %d stat %d",
206       cptr, cptr->fd, cptr->authfd, cptr->status));
207   /*
208    * Nasty.  Cant allow any other reads from client fd while we're
209    * waiting on the authfd to return a full valid string.  Use the
210    * client's input buffer to buffer the authd reply.
211    * Oh. this is needed because an authd reply may come back in more
212    * than 1 read! -avalon
213    */
214   if ((len = read(cptr->authfd, cptr->buffer + cptr->count,
215       sizeof(cptr->buffer) - 1 - cptr->count)) >= 0)
216   {
217     cptr->count += len;
218     cptr->buffer[cptr->count] = '\0';
219   }
220
221   cptr->lasttime = now;
222   if ((len > 0) && (cptr->count != (sizeof(cptr->buffer) - 1)) &&
223       (sscanf(cptr->buffer, "%hd , %hd : USERID : %*[^:]: %10s",
224       &remp, &locp, ruser) == 3))
225   {
226     s = strrchr(cptr->buffer, ':');
227     *s++ = '\0';
228     for (t = (strrchr(cptr->buffer, ':') + 1); *t; t++)
229       if (!isSpace(*t))
230         break;
231     strncpy(system, t, sizeof(system) - 1);
232     system[sizeof(system) - 1] = 0;
233     for (t = ruser; *s && (t < ruser + sizeof(ruser)); s++)
234       if (!isSpace(*s) && *s != ':' && *s != '@')
235         *t++ = *s;
236     *t = '\0';
237     Debug((DEBUG_INFO, "auth reply ok [%s] [%s]", system, ruser));
238   }
239   else if (len != 0)
240   {
241     if (!strchr(cptr->buffer, '\n') && !strchr(cptr->buffer, '\r'))
242       return;
243     Debug((DEBUG_ERROR, "local %d remote %d", locp, remp));
244     Debug((DEBUG_ERROR, "bad auth reply in [%s]", cptr->buffer));
245     *ruser = '\0';
246   }
247   close(cptr->authfd);
248   if (cptr->authfd == highest_fd)
249     while (!loc_clients[highest_fd])
250       highest_fd--;
251   cptr->count = 0;
252   cptr->authfd = -1;
253   ClearAuth(cptr);
254   if (!DoingDNS(cptr))
255     SetAccess(cptr);
256   if (len > 0)
257     Debug((DEBUG_INFO, "ident reply: [%s]", cptr->buffer));
258
259   if (!locp || !remp || !*ruser)
260   {
261     ircstp->is_abad++;
262     return;
263   }
264   ircstp->is_asuc++;
265   strncpy(cptr->username, ruser, USERLEN);
266   cptr->username[USERLEN] = 0;  /* This is the LA bug --Run */
267   if (strncmp(system, "OTHER", 5))
268     cptr->flags |= FLAGS_GOTID;
269   Debug((DEBUG_INFO, "got username [%s]", ruser));
270   return;
271 }