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