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