chiark / gitweb /
Use `sel_force' to avoid a `select' call between reads and writes.
[fwd] / identify.c
1 /* -*-c-*-
2  *
3  * $Id: identify.c,v 1.4 1999/07/27 18:30:53 mdw Exp $
4  *
5  * Identifies and logs the client of a connection
6  *
7  * (c) 1999 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the `fw' port forwarder.
13  *
14  * `fw' is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  * 
19  * `fw' is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with `fw'; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 /*----- Revision history --------------------------------------------------* 
30  *
31  * $Log: identify.c,v $
32  * Revision 1.4  1999/07/27 18:30:53  mdw
33  * Various minor portability fixes.
34  *
35  * Revision 1.3  1999/07/26 23:26:21  mdw
36  * Minor modifications for new design.
37  *
38  * Revision 1.2  1999/07/03 13:56:59  mdw
39  * Log connections to syslog or stderr as appropriate.
40  *
41  * Revision 1.1.1.1  1999/07/01 08:56:23  mdw
42  * Initial revision.
43  *
44  */
45
46 /*----- Header files ------------------------------------------------------*/
47
48 #include "config.h"
49
50 #include <errno.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <time.h>
55
56 #include <sys/types.h>
57 #include <sys/time.h>
58 #include <unistd.h>
59 #include <syslog.h>
60
61 #include <sys/socket.h>
62 #include <netinet/in.h>
63 #include <arpa/inet.h>
64 #include <netdb.h>
65
66 #include <mLib/alloc.h>
67 #include <mLib/conn.h>
68 #include <mLib/dstr.h>
69 #include <mLib/report.h>
70 #include <mLib/sel.h>
71 #include <mLib/selbuf.h>
72 #include <mLib/str.h>
73
74 #include "bres.h"
75 #include "fw.h"
76 #include "ident.h"
77 #include "identify.h"
78
79 /*----- Magic numbers -----------------------------------------------------*/
80
81 #define TIMEOUT 15                      /* Seconds to wait for answers */
82
83 /*----- Data structures ---------------------------------------------------*/
84
85 /* --- Structure to track the progress of an identification --- */
86
87 typedef struct id {
88   id_req q;                             /* Copy of client's request block */
89   time_t when;                          /* When the connection occurred */
90   conn c;                               /* Connection selector */
91   unsigned state;                       /* Current state of the world */
92   bres_client r;                        /* Backgd resolver client block */
93   char host[64];                        /* Resolved hostname */
94   char user[32];                        /* Authenticated client user */
95   sel_timer t;                          /* Timeout selector */
96   selbuf id;                            /* Reader for the RFC931 client */
97 } id;
98
99 #define S_HOST 1u                       /* Read the hostname from resolver */
100 #define S_USER 2u                       /* Read the username from RFC931 */
101 #define S_UCONN 4u                      /* Connected to remote RFC931 */
102 #define S_TIMER 8u                      /* Timeout has completed */
103
104 /*----- Main code ---------------------------------------------------------*/
105
106 /* --- @id_done@ --- *
107  *
108  * Arguments:   @id *i@ = pointer to identification block
109  *
110  * Returns:     ---
111  *
112  * Use:         Finishes with an identification block.
113  */
114
115 static void id_done(id *i)
116 {
117   /* --- Close down the various dependent bits --- */
118
119   if (!(i->state & S_HOST))
120     bres_abort(&i->r);
121   if (!(i->state & S_UCONN))
122     conn_kill(&i->c);
123   else if (!(i->state & S_USER))
124     selbuf_disable(&i->id);
125   if (!(i->state & S_TIMER))
126     sel_rmtimer(&i->t);
127
128   /* --- Report the final result --- */
129
130   fw_log(i->when, "[%s] %s from %s@%s [%s]",
131          i->q.desc, i->q.act,
132          i->user, i->host, inet_ntoa(i->q.rsin.sin_addr));
133
134   /* --- Dispose of the block --- */
135
136   REFFD_DEC(i->q.r);
137   free(i);
138 }
139
140 /* --- @id_res@ --- *
141  *
142  * Arguments:   @const char *host@ = name of the resolved host
143  *              @void *vp@ = pointer to identification block
144  *
145  * Returns:     ---
146  *
147  * Use:         Responds to a completed reverse name resolution.
148  */
149
150 static void id_res(const char *host, void *vp)
151 {
152   id *i = vp;
153   str_sanitize(i->host, host, sizeof(i->host));
154   i->state |= S_HOST;
155   if (i->state & S_USER)
156     id_done(i);
157 }
158
159 /* --- @id_ident@ --- *
160  *
161  * Arguments:   @char *p@ = pointer to string read from server
162  *              @void *vp@ = pointer to identification block
163  *
164  * Returns:     ---
165  *
166  * Use:         Responds to a line read from the remote RFC931 server.
167  */
168
169 static void id_ident(char *p, void *vp)
170 {
171   id *i = vp;
172
173   /* --- Get rid of the connection --- */
174
175   i->state |= S_USER;
176   selbuf_disable(&i->id);
177   close(i->id.reader.fd);
178
179   /* --- Read the information from the returned line --- */
180
181   if (p) {
182     ident idbuf;
183     ident_parse(p, &idbuf);
184     if (idbuf.type == ident_userid)
185       str_sanitize(i->user, idbuf.u.userid.user, sizeof(i->user));
186   }
187
188   /* --- Maybe finish off this identification --- */
189
190   if (i->state & S_HOST)
191     id_done(i);
192 }
193
194 /* --- @id_conn@ --- *
195  *
196  * Arguments:   @int fd@ = file descriptor connected
197  *              @void *vp@ = pointer to identification block
198  *
199  * Returns:     ---
200  *
201  * Use:         Responds to a completed connection to the remote RFC931
202  *              server.
203  */
204
205 static void id_conn(int fd, void *vp)
206 {
207   id *i = vp;
208
209   if (fd == -1) {
210     i->state |= S_USER | S_UCONN;
211     if (i->state & S_HOST)
212       id_done(i);
213   } else {
214     dstr d = DSTR_INIT;
215     dstr_putf(&d, "%u, %u\n",
216               ntohs(i->q.rsin.sin_port), ntohs(i->q.lsin.sin_port));
217     write(fd, d.buf, d.len);
218     dstr_destroy(&d);
219     i->state |= S_UCONN;
220     selbuf_init(&i->id, sel, fd, id_ident, i);
221   }
222 }
223
224 /* --- @id_timer@ --- *
225  *
226  * Arguments:   @struct timeval *tv@ = pointer to the current time
227  *              @void *vp@ = pointer to identification block
228  *
229  * Returns:     ---
230  *
231  * Use:         Times an identification job out.
232  */
233
234 static void id_timer(struct timeval *tv, void *vp)
235 {
236   id *i = vp;
237   id_done(i);
238 }
239
240 /* --- @identify@ --- *
241  *
242  * Arguments:   @const id_req *q@ = pointer to request block
243  *
244  * Returns:     ---
245  *
246  * Use:         Starts a background ident lookup and reverse-resolve job
247  *              which will, eventually, report a message to the system log.
248  */
249
250 void identify(const id_req *q)
251 {
252   id *i;
253
254   /* --- Initialize the block with stuff --- */
255
256   i = xmalloc(sizeof(*i));
257   i->q = *q;
258   REFFD_INC(i->q.r);
259
260   str_sanitize(i->host, inet_ntoa(q->rsin.sin_addr), sizeof(i->host));
261   strcpy(i->user, "<ANONYMOUS>");
262   i->state = 0;
263   i->when = time(0);
264
265   /* --- Set up the connection to the identity server --- */
266
267   {
268     int fd;
269     struct sockaddr_in sin;
270
271     if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
272       id_conn(-1, i);
273     else {
274       memset(&sin, 0, sizeof(sin));
275       sin.sin_family = AF_INET;
276       sin.sin_addr = q->lsin.sin_addr;
277       sin.sin_port = 0;
278       if (bind(fd, (struct sockaddr *)&sin, sizeof(sin))) {
279         close(fd);
280         id_conn(-1, i);
281       } else {
282         int opt = 1;
283         sin.sin_family = AF_INET;
284         sin.sin_addr = q->rsin.sin_addr;
285         sin.sin_port = htons(113);
286         setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
287         conn_init(&i->c, sel, fd,
288                   (struct sockaddr *)&sin, sizeof(sin),
289                   id_conn, i);
290       }
291     }
292   }
293
294   /* --- Set up the name resolver --- */
295
296   bres_resolve(&i->r, q->rsin.sin_addr, id_res, i);
297
298   /* --- Set up the time limiter --- */
299
300   {
301     struct timeval tv;
302     gettimeofday(&tv, 0);
303     tv.tv_sec += TIMEOUT;
304     sel_addtimer(sel, &i->t, &tv, id_timer, i);
305   }
306 }
307
308 /*----- That's all, folks -------------------------------------------------*/