e82f7154 |
1 | /* -*-c-*- |
2 | * |
793f69cd |
3 | * $Id: identify.c,v 1.2 1999/07/03 13:56:59 mdw Exp $ |
e82f7154 |
4 | * |
5 | * Identifies and logs the client of a connection |
6 | * |
7 | * (c) 1999 Mark Wooding |
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 $ |
793f69cd |
32 | * Revision 1.2 1999/07/03 13:56:59 mdw |
33 | * Log connections to syslog or stderr as appropriate. |
34 | * |
35 | * Revision 1.1.1.1 1999/07/01 08:56:23 mdw |
36 | * Initial revision. |
e82f7154 |
37 | * |
38 | */ |
39 | |
40 | /*----- Header files ------------------------------------------------------*/ |
41 | |
42 | #include "config.h" |
43 | |
44 | #include <errno.h> |
45 | #include <stdio.h> |
46 | #include <stdlib.h> |
47 | #include <string.h> |
48 | #include <time.h> |
49 | |
50 | #include <sys/types.h> |
51 | #include <sys/time.h> |
52 | #include <unistd.h> |
53 | #include <syslog.h> |
54 | |
55 | #include <sys/socket.h> |
56 | #include <netinet/in.h> |
57 | #include <arpa/inet.h> |
58 | #include <netdb.h> |
59 | |
60 | #include <mLib/alloc.h> |
61 | #include <mLib/conn.h> |
62 | #include <mLib/dstr.h> |
63 | #include <mLib/report.h> |
64 | #include <mLib/sel.h> |
65 | #include <mLib/selbuf.h> |
66 | #include <mLib/str.h> |
67 | |
68 | #include "bres.h" |
69 | #include "fw.h" |
70 | #include "ident.h" |
71 | #include "identify.h" |
72 | |
73 | /*----- Magic numbers -----------------------------------------------------*/ |
74 | |
75 | #define TIMEOUT 15 /* Seconds to wait for answers */ |
76 | |
77 | /*----- Data structures ---------------------------------------------------*/ |
78 | |
79 | /* --- Structure to track the progress of an identification --- */ |
80 | |
81 | typedef struct id { |
82 | id_req q; /* Copy of client's request block */ |
83 | void (*func)(void */*p*/); /* Function to call when done */ |
84 | void *p; /* Argument to pass to function */ |
85 | time_t when; /* When the connection occurred */ |
86 | conn c; /* Connection selector */ |
87 | unsigned state; /* Current state of the world */ |
88 | bres_client r; /* Backgd resolver client block */ |
89 | char host[64]; /* Resolved hostname */ |
90 | char user[32]; /* Authenticated client user */ |
91 | sel_timer t; /* Timeout selector */ |
92 | selbuf id; /* Reader for the RFC931 client */ |
93 | } id; |
94 | |
95 | #define S_HOST 1u /* Read the hostname from resolver */ |
96 | #define S_USER 2u /* Read the username from RFC931 */ |
97 | #define S_UCONN 4u /* Connected to remote RFC931 */ |
98 | #define S_TIMER 8u /* Timeout has completed */ |
99 | |
100 | /*----- Main code ---------------------------------------------------------*/ |
101 | |
102 | /* --- @id_done@ --- * |
103 | * |
104 | * Arguments: @id *i@ = pointer to identification block |
105 | * |
106 | * Returns: --- |
107 | * |
108 | * Use: Finishes with an identification block. |
109 | */ |
110 | |
111 | static void id_done(id *i) |
112 | { |
113 | char buf[64]; |
114 | struct tm *tm; |
115 | |
116 | /* --- Close down the various dependent bits --- */ |
117 | |
118 | if (!(i->state & S_HOST)) |
119 | bres_abort(&i->r); |
120 | if (!(i->state & S_UCONN)) |
121 | conn_kill(&i->c); |
122 | else if (!(i->state & S_USER)) |
123 | selbuf_disable(&i->id); |
124 | if (!(i->state & S_TIMER)) |
125 | sel_rmtimer(&i->t); |
126 | |
127 | /* --- Report the final result --- */ |
128 | |
129 | tm = localtime(&i->when); |
130 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm); |
793f69cd |
131 | if (flags & FW_SYSLOG) { |
132 | syslog(LOG_NOTICE, "%s %s %s from %s@%s [%s]\n", |
133 | buf, i->q.desc, i->q.act, |
134 | i->user, i->host, inet_ntoa(i->q.rsin.sin_addr)); |
135 | } else { |
136 | fprintf(stderr, "%s %s %s from %s@%s [%s]\n", |
137 | buf, i->q.desc, i->q.act, |
138 | i->user, i->host, inet_ntoa(i->q.rsin.sin_addr)); |
139 | } |
e82f7154 |
140 | |
141 | /* --- Dispose of the block --- */ |
142 | |
143 | i->func(i->p); |
144 | free(i); |
145 | } |
146 | |
147 | /* --- @id_res@ --- * |
148 | * |
149 | * Arguments: @const char *host@ = name of the resolved host |
150 | * @void *vp@ = pointer to identification block |
151 | * |
152 | * Returns: --- |
153 | * |
154 | * Use: Responds to a completed reverse name resolution. |
155 | */ |
156 | |
157 | static void id_res(const char *host, void *vp) |
158 | { |
159 | id *i = vp; |
160 | str_sanitize(i->host, host, sizeof(i->host)); |
161 | i->state |= S_HOST; |
162 | if (i->state & S_USER) |
163 | id_done(i); |
164 | } |
165 | |
166 | /* --- @id_ident@ --- * |
167 | * |
168 | * Arguments: @char *p@ = pointer to string read from server |
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 | |
176 | static void id_ident(char *p, void *vp) |
177 | { |
178 | id *i = vp; |
179 | |
180 | /* --- Get rid of the connection --- */ |
181 | |
182 | i->state |= S_USER; |
183 | selbuf_disable(&i->id); |
184 | close(i->id.reader.fd); |
185 | |
186 | /* --- Read the information from the returned line --- */ |
187 | |
188 | if (p) { |
189 | ident idbuf; |
190 | ident_parse(p, &idbuf); |
191 | if (idbuf.type == ident_userid) |
192 | str_sanitize(i->user, idbuf.u.userid.user, sizeof(i->user)); |
193 | } |
194 | |
195 | /* --- Maybe finish off this identification --- */ |
196 | |
197 | if (i->state & S_HOST) |
198 | id_done(i); |
199 | } |
200 | |
201 | /* --- @id_conn@ --- * |
202 | * |
203 | * Arguments: @int fd@ = file descriptor connected |
204 | * @void *vp@ = pointer to identification block |
205 | * |
206 | * Returns: --- |
207 | * |
208 | * Use: Responds to a completed connection to the remote RFC931 |
209 | * server. |
210 | */ |
211 | |
212 | static void id_conn(int fd, void *vp) |
213 | { |
214 | id *i = vp; |
215 | |
216 | if (fd == -1) { |
217 | i->state |= S_USER | S_UCONN; |
218 | if (i->state & S_HOST) |
219 | id_done(i); |
220 | } else { |
221 | dstr d = DSTR_INIT; |
222 | dstr_putf(&d, "%u, %u\n", |
223 | ntohs(i->q.rsin.sin_port), ntohs(i->q.lsin.sin_port)); |
224 | write(fd, d.buf, d.len); |
225 | dstr_destroy(&d); |
226 | i->state |= S_UCONN; |
227 | selbuf_init(&i->id, sel, fd, id_ident, i); |
228 | } |
229 | } |
230 | |
231 | /* --- @id_timer@ --- * |
232 | * |
233 | * Arguments: @struct timeval *tv@ = pointer to the current time |
234 | * @void *vp@ = pointer to identification block |
235 | * |
236 | * Returns: --- |
237 | * |
238 | * Use: Times an identification job out. |
239 | */ |
240 | |
241 | static void id_timer(struct timeval *tv, void *vp) |
242 | { |
243 | id *i = vp; |
244 | id_done(i); |
245 | } |
246 | |
247 | /* --- @identify@ --- * |
248 | * |
249 | * Arguments: @const id_req *q@ = pointer to request block |
250 | * @void (*func)(void *p)@ = function to call when done |
251 | * @void *p@ = argument to pass to function |
252 | * |
253 | * Returns: --- |
254 | * |
255 | * Use: Starts a background ident lookup and reverse-resolve job |
256 | * which will, eventually, report a message to the system log. |
257 | */ |
258 | |
259 | void identify(const id_req *q, |
260 | void (*func)(void */*p*/), void *p) |
261 | { |
262 | id *i; |
263 | |
264 | /* --- Initialize the block with stuff --- */ |
265 | |
266 | i = xmalloc(sizeof(*i)); |
267 | i->q = *q; |
268 | i->func = func; |
269 | i->p = p; |
270 | |
271 | str_sanitize(i->host, inet_ntoa(q->rsin.sin_addr), sizeof(i->host)); |
272 | strcpy(i->user, "<ANONYMOUS>"); |
273 | i->state = 0; |
274 | i->when = time(0); |
275 | |
276 | /* --- Set up the connection to the identity server --- */ |
277 | |
278 | { |
279 | int fd; |
280 | struct sockaddr_in sin; |
281 | |
282 | if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) |
283 | id_conn(-1, i); |
284 | else { |
285 | sin.sin_family = AF_INET; |
286 | sin.sin_addr = q->lsin.sin_addr; |
287 | sin.sin_port = 0; |
288 | if (bind(fd, (struct sockaddr *)&sin, sizeof(sin))) { |
289 | close(fd); |
290 | id_conn(-1, i); |
291 | } else { |
292 | sin.sin_family = AF_INET; |
293 | sin.sin_addr = q->rsin.sin_addr; |
294 | sin.sin_port = htons(113); |
295 | conn_init(&i->c, sel, fd, |
296 | (struct sockaddr *)&sin, sizeof(sin), |
297 | id_conn, i); |
298 | } |
299 | } |
300 | } |
301 | |
302 | /* --- Set up the name resolver --- */ |
303 | |
304 | bres_resolve(&i->r, q->rsin.sin_addr, id_res, i); |
305 | |
306 | /* --- Set up the time limiter --- */ |
307 | |
308 | { |
309 | struct timeval tv; |
310 | gettimeofday(&tv, 0); |
311 | tv.tv_sec += TIMEOUT; |
312 | sel_addtimer(sel, &i->t, &tv, id_timer, i); |
313 | } |
314 | } |
315 | |
316 | /*----- That's all, folks -------------------------------------------------*/ |