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