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