chiark / gitweb /
Handle select errors more robustly.
[fwd] / identify.c
1 /* -*-c-*-
2  *
3  * $Id: identify.c,v 1.5 1999/10/10 16:45:34 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.5  1999/10/10 16:45:34  mdw
33  * Modified to use new mLib resolver and ident client.
34  *
35  * Revision 1.4  1999/07/27 18:30:53  mdw
36  * Various minor portability fixes.
37  *
38  * Revision 1.3  1999/07/26 23:26:21  mdw
39  * Minor modifications for new design.
40  *
41  * Revision 1.2  1999/07/03 13:56:59  mdw
42  * Log connections to syslog or stderr as appropriate.
43  *
44  * Revision 1.1.1.1  1999/07/01 08:56:23  mdw
45  * Initial revision.
46  *
47  */
48
49 /*----- Header files ------------------------------------------------------*/
50
51 #include "config.h"
52
53 #include <errno.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <time.h>
58
59 #include <sys/types.h>
60 #include <sys/time.h>
61 #include <unistd.h>
62 #include <syslog.h>
63
64 #include <sys/socket.h>
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
67 #include <netdb.h>
68
69 #include <mLib/alloc.h>
70 #include <mLib/bres.h>
71 #include <mLib/conn.h>
72 #include <mLib/dstr.h>
73 #include <mLib/ident.h>
74 #include <mLib/report.h>
75 #include <mLib/sel.h>
76 #include <mLib/selbuf.h>
77 #include <mLib/str.h>
78
79 #include "fw.h"
80 #include "identify.h"
81
82 /*----- Magic numbers -----------------------------------------------------*/
83
84 #define TIMEOUT 15                      /* Seconds to wait for answers */
85
86 /*----- Data structures ---------------------------------------------------*/
87
88 /* --- Structure to track the progress of an identification --- */
89
90 typedef struct id {
91   id_req q;                             /* Copy of client's request block */
92   time_t when;                          /* When the connection occurred */
93   conn c;                               /* Connection selector */
94   unsigned state;                       /* Current state of the world */
95   sel_timer t;                          /* Timeout selector */
96   bres_client r;                        /* Backgd resolver client block */
97   ident_request i;                      /* Ident client block */
98   char host[64];                        /* Resolved hostname */
99   char user[32];                        /* Authenticated client user */
100 } id;
101
102 #define S_HOST 1u                       /* Read the hostname from resolver */
103 #define S_USER 2u                       /* Read the username from RFC931 */
104 #define S_TIMER 4u                      /* Timeout has completed */
105
106 /*----- Main code ---------------------------------------------------------*/
107
108 /* --- @id_done@ --- *
109  *
110  * Arguments:   @id *i@ = pointer to identification block
111  *
112  * Returns:     ---
113  *
114  * Use:         Finishes with an identification block.
115  */
116
117 static void id_done(id *i)
118 {
119   /* --- Close down the various dependent bits --- */
120
121   if (!(i->state & S_HOST))
122     bres_abort(&i->r);
123   if (!(i->state & S_USER))
124     ident_abort(&i->i);
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:   @struct hostent *h@ = 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(struct hostent *h, void *vp)
151 {
152   id *i = vp;
153   if (h)
154     str_sanitize(i->host, h->h_name, sizeof(i->host));
155   i->state |= S_HOST;
156   if (i->state & S_USER)
157     id_done(i);
158 }
159
160 /* --- @id_ident@ --- *
161  *
162  * Arguments:   @ident_reply *i@ = pointer to string read from server
163  *              @void *vp@ = pointer to identification block
164  *
165  * Returns:     ---
166  *
167  * Use:         Responds to a line read from the remote RFC931 server.
168  */
169
170 static void id_ident(ident_reply *ir, void *vp)
171 {
172   id *i = vp;
173
174   /* --- Read the information from the client --- */
175
176   if (ir && ir->type == IDENT_USERID)
177     str_sanitize(i->user, ir->u.userid.user, sizeof(i->user));
178
179   /* --- Maybe finish off this identification --- */
180
181   i->state |= S_USER;
182   if (i->state & S_HOST)
183     id_done(i);
184 }
185
186 /* --- @id_timer@ --- *
187  *
188  * Arguments:   @struct timeval *tv@ = pointer to the current time
189  *              @void *vp@ = pointer to identification block
190  *
191  * Returns:     ---
192  *
193  * Use:         Times an identification job out.
194  */
195
196 static void id_timer(struct timeval *tv, void *vp)
197 {
198   id *i = vp;
199   id_done(i);
200 }
201
202 /* --- @identify@ --- *
203  *
204  * Arguments:   @const id_req *q@ = pointer to request block
205  *
206  * Returns:     ---
207  *
208  * Use:         Starts a background ident lookup and reverse-resolve job
209  *              which will, eventually, report a message to the system log.
210  */
211
212 void identify(const id_req *q)
213 {
214   id *i;
215
216   /* --- Initialize the block with stuff --- */
217
218   i = xmalloc(sizeof(*i));
219   i->q = *q;
220   REFFD_INC(i->q.r);
221
222   str_sanitize(i->host, inet_ntoa(q->rsin.sin_addr), sizeof(i->host));
223   strcpy(i->user, "<ANONYMOUS>");
224   i->state = 0;
225   i->when = time(0);
226
227   /* --- Set up the connection to the identity server --- */
228
229   ident(&i->i, sel, &q->lsin, &q->rsin, id_ident, i);
230
231   /* --- Set up the name resolver --- */
232
233   bres_byaddr(&i->r, q->rsin.sin_addr, id_res, i);
234
235   /* --- Set up the time limiter --- */
236
237   {
238     struct timeval tv;
239     gettimeofday(&tv, 0);
240     tv.tv_sec += TIMEOUT;
241     sel_addtimer(sel, &i->t, &tv, id_timer, i);
242   }
243 }
244
245 /*----- That's all, folks -------------------------------------------------*/