Remove crufty CVS $Id$ tags.
[fwd] / identify.c
CommitLineData
e82f7154 1/* -*-c-*-
2 *
e82f7154 3 * Identifies and logs the client of a connection
4 *
e5398e09 5 * (c) 1999 Straylight/Edgeware
e82f7154 6 */
7
8/*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the `fw' port forwarder.
11 *
12 * `fw' is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * `fw' is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with `fw'; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
e82f7154 27/*----- Header files ------------------------------------------------------*/
28
29#include "config.h"
30
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <time.h>
36
37#include <sys/types.h>
38#include <sys/time.h>
39#include <unistd.h>
40#include <syslog.h>
41
42#include <sys/socket.h>
43#include <netinet/in.h>
44#include <arpa/inet.h>
45#include <netdb.h>
46
47#include <mLib/alloc.h>
16dfe536 48#include <mLib/bres.h>
e82f7154 49#include <mLib/conn.h>
50#include <mLib/dstr.h>
16dfe536 51#include <mLib/ident.h>
e82f7154 52#include <mLib/report.h>
53#include <mLib/sel.h>
54#include <mLib/selbuf.h>
55#include <mLib/str.h>
56
e82f7154 57#include "fw.h"
e82f7154 58#include "identify.h"
59
60/*----- Magic numbers -----------------------------------------------------*/
61
62#define TIMEOUT 15 /* Seconds to wait for answers */
63
64/*----- Data structures ---------------------------------------------------*/
65
66/* --- Structure to track the progress of an identification --- */
67
68typedef struct id {
69 id_req q; /* Copy of client's request block */
e82f7154 70 time_t when; /* When the connection occurred */
71 conn c; /* Connection selector */
72 unsigned state; /* Current state of the world */
16dfe536 73 sel_timer t; /* Timeout selector */
e82f7154 74 bres_client r; /* Backgd resolver client block */
16dfe536 75 ident_request i; /* Ident client block */
fab71203 76 char host[128]; /* Resolved hostname */
77 char user[64]; /* Authenticated client user */
e82f7154 78} id;
79
80#define S_HOST 1u /* Read the hostname from resolver */
81#define S_USER 2u /* Read the username from RFC931 */
16dfe536 82#define S_TIMER 4u /* Timeout has completed */
e82f7154 83
84/*----- Main code ---------------------------------------------------------*/
85
86/* --- @id_done@ --- *
87 *
88 * Arguments: @id *i@ = pointer to identification block
89 *
90 * Returns: ---
91 *
92 * Use: Finishes with an identification block.
93 */
94
95static void id_done(id *i)
96{
e82f7154 97 /* --- Close down the various dependent bits --- */
98
99 if (!(i->state & S_HOST))
100 bres_abort(&i->r);
16dfe536 101 if (!(i->state & S_USER))
102 ident_abort(&i->i);
e82f7154 103 if (!(i->state & S_TIMER))
104 sel_rmtimer(&i->t);
105
106 /* --- Report the final result --- */
107
ee599f55 108 fw_log(i->when, "[%s] %s from %s@%s [%s:%u]",
e5398e09 109 i->q.desc, i->q.act,
ee599f55 110 i->user, i->host,
111 inet_ntoa(i->q.rsin.sin_addr), (unsigned)ntohs(i->q.rsin.sin_port));
e82f7154 112
113 /* --- Dispose of the block --- */
114
e5398e09 115 REFFD_DEC(i->q.r);
b0805b27 116 xfree(i);
e82f7154 117}
118
119/* --- @id_res@ --- *
120 *
16dfe536 121 * Arguments: @struct hostent *h@ = name of the resolved host
e82f7154 122 * @void *vp@ = pointer to identification block
123 *
124 * Returns: ---
125 *
126 * Use: Responds to a completed reverse name resolution.
127 */
128
16dfe536 129static void id_res(struct hostent *h, void *vp)
e82f7154 130{
131 id *i = vp;
16dfe536 132 if (h)
133 str_sanitize(i->host, h->h_name, sizeof(i->host));
e82f7154 134 i->state |= S_HOST;
135 if (i->state & S_USER)
136 id_done(i);
137}
138
139/* --- @id_ident@ --- *
140 *
16dfe536 141 * Arguments: @ident_reply *i@ = pointer to string read from server
e82f7154 142 * @void *vp@ = pointer to identification block
143 *
144 * Returns: ---
145 *
146 * Use: Responds to a line read from the remote RFC931 server.
147 */
148
16dfe536 149static void id_ident(ident_reply *ir, void *vp)
e82f7154 150{
151 id *i = vp;
152
16dfe536 153 /* --- Read the information from the client --- */
e82f7154 154
16dfe536 155 if (ir && ir->type == IDENT_USERID)
156 str_sanitize(i->user, ir->u.userid.user, sizeof(i->user));
e82f7154 157
158 /* --- Maybe finish off this identification --- */
159
16dfe536 160 i->state |= S_USER;
e82f7154 161 if (i->state & S_HOST)
162 id_done(i);
163}
164
e82f7154 165/* --- @id_timer@ --- *
166 *
167 * Arguments: @struct timeval *tv@ = pointer to the current time
168 * @void *vp@ = pointer to identification block
169 *
170 * Returns: ---
171 *
172 * Use: Times an identification job out.
173 */
174
175static void id_timer(struct timeval *tv, void *vp)
176{
177 id *i = vp;
547dc0e8 178 i->state |= S_TIMER;
e82f7154 179 id_done(i);
180}
181
182/* --- @identify@ --- *
183 *
184 * Arguments: @const id_req *q@ = pointer to request block
e82f7154 185 *
186 * Returns: ---
187 *
188 * Use: Starts a background ident lookup and reverse-resolve job
189 * which will, eventually, report a message to the system log.
190 */
191
e5398e09 192void identify(const id_req *q)
e82f7154 193{
194 id *i;
195
196 /* --- Initialize the block with stuff --- */
197
198 i = xmalloc(sizeof(*i));
199 i->q = *q;
e5398e09 200 REFFD_INC(i->q.r);
e82f7154 201
202 str_sanitize(i->host, inet_ntoa(q->rsin.sin_addr), sizeof(i->host));
203 strcpy(i->user, "<ANONYMOUS>");
204 i->state = 0;
205 i->when = time(0);
206
207 /* --- Set up the connection to the identity server --- */
208
16dfe536 209 ident(&i->i, sel, &q->lsin, &q->rsin, id_ident, i);
e82f7154 210
211 /* --- Set up the name resolver --- */
212
16dfe536 213 bres_byaddr(&i->r, q->rsin.sin_addr, id_res, i);
e82f7154 214
215 /* --- Set up the time limiter --- */
216
217 {
218 struct timeval tv;
219 gettimeofday(&tv, 0);
220 tv.tv_sec += TIMEOUT;
221 sel_addtimer(sel, &i->t, &tv, id_timer, i);
222 }
223}
224
225/*----- That's all, folks -------------------------------------------------*/