Remove crufty CVS $Id$ tags.
[fwd] / identify.c
1 /* -*-c-*-
2 *
3 * Identifies and logs the client of a connection
4 *
5 * (c) 1999 Straylight/Edgeware
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
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>
48 #include <mLib/bres.h>
49 #include <mLib/conn.h>
50 #include <mLib/dstr.h>
51 #include <mLib/ident.h>
52 #include <mLib/report.h>
53 #include <mLib/sel.h>
54 #include <mLib/selbuf.h>
55 #include <mLib/str.h>
56
57 #include "fw.h"
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
68 typedef struct id {
69 id_req q; /* Copy of client's request block */
70 time_t when; /* When the connection occurred */
71 conn c; /* Connection selector */
72 unsigned state; /* Current state of the world */
73 sel_timer t; /* Timeout selector */
74 bres_client r; /* Backgd resolver client block */
75 ident_request i; /* Ident client block */
76 char host[128]; /* Resolved hostname */
77 char user[64]; /* Authenticated client user */
78 } id;
79
80 #define S_HOST 1u /* Read the hostname from resolver */
81 #define S_USER 2u /* Read the username from RFC931 */
82 #define S_TIMER 4u /* Timeout has completed */
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
95 static void id_done(id *i)
96 {
97 /* --- Close down the various dependent bits --- */
98
99 if (!(i->state & S_HOST))
100 bres_abort(&i->r);
101 if (!(i->state & S_USER))
102 ident_abort(&i->i);
103 if (!(i->state & S_TIMER))
104 sel_rmtimer(&i->t);
105
106 /* --- Report the final result --- */
107
108 fw_log(i->when, "[%s] %s from %s@%s [%s:%u]",
109 i->q.desc, i->q.act,
110 i->user, i->host,
111 inet_ntoa(i->q.rsin.sin_addr), (unsigned)ntohs(i->q.rsin.sin_port));
112
113 /* --- Dispose of the block --- */
114
115 REFFD_DEC(i->q.r);
116 xfree(i);
117 }
118
119 /* --- @id_res@ --- *
120 *
121 * Arguments: @struct hostent *h@ = name of the resolved host
122 * @void *vp@ = pointer to identification block
123 *
124 * Returns: ---
125 *
126 * Use: Responds to a completed reverse name resolution.
127 */
128
129 static void id_res(struct hostent *h, void *vp)
130 {
131 id *i = vp;
132 if (h)
133 str_sanitize(i->host, h->h_name, sizeof(i->host));
134 i->state |= S_HOST;
135 if (i->state & S_USER)
136 id_done(i);
137 }
138
139 /* --- @id_ident@ --- *
140 *
141 * Arguments: @ident_reply *i@ = pointer to string read from server
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
149 static void id_ident(ident_reply *ir, void *vp)
150 {
151 id *i = vp;
152
153 /* --- Read the information from the client --- */
154
155 if (ir && ir->type == IDENT_USERID)
156 str_sanitize(i->user, ir->u.userid.user, sizeof(i->user));
157
158 /* --- Maybe finish off this identification --- */
159
160 i->state |= S_USER;
161 if (i->state & S_HOST)
162 id_done(i);
163 }
164
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
175 static void id_timer(struct timeval *tv, void *vp)
176 {
177 id *i = vp;
178 i->state |= S_TIMER;
179 id_done(i);
180 }
181
182 /* --- @identify@ --- *
183 *
184 * Arguments: @const id_req *q@ = pointer to request block
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
192 void identify(const id_req *q)
193 {
194 id *i;
195
196 /* --- Initialize the block with stuff --- */
197
198 i = xmalloc(sizeof(*i));
199 i->q = *q;
200 REFFD_INC(i->q.r);
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
209 ident(&i->i, sel, &q->lsin, &q->rsin, id_ident, i);
210
211 /* --- Set up the name resolver --- */
212
213 bres_byaddr(&i->r, q->rsin.sin_addr, id_res, i);
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 -------------------------------------------------*/