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