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