e82f7154 |
1 | /* -*-c-*- |
e82f7154 |
2 | * |
3 | * Identifies and logs the client of a connection |
4 | * |
e5398e09 |
5 | * (c) 1999 Straylight/Edgeware |
e82f7154 |
6 | */ |
7 | |
206212ca |
8 | /*----- Licensing notice --------------------------------------------------* |
e82f7154 |
9 | * |
9155ea97 |
10 | * This file is part of the `fwd' port forwarder. |
e82f7154 |
11 | * |
9155ea97 |
12 | * `fwd' is free software; you can redistribute it and/or modify |
e82f7154 |
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. |
206212ca |
16 | * |
9155ea97 |
17 | * `fwd' is distributed in the hope that it will be useful, |
e82f7154 |
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. |
206212ca |
21 | * |
e82f7154 |
22 | * You should have received a copy of the GNU General Public License |
9155ea97 |
23 | * along with `fwd'; if not, write to the Free Software Foundation, |
e82f7154 |
24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | */ |
26 | |
9155ea97 |
27 | #include "fwd.h" |
e82f7154 |
28 | |
29 | /*----- Magic numbers -----------------------------------------------------*/ |
30 | |
31 | #define TIMEOUT 15 /* Seconds to wait for answers */ |
32 | |
33 | /*----- Data structures ---------------------------------------------------*/ |
34 | |
35 | /* --- Structure to track the progress of an identification --- */ |
36 | |
37 | typedef struct id { |
38 | id_req q; /* Copy of client's request block */ |
e82f7154 |
39 | time_t when; /* When the connection occurred */ |
40 | conn c; /* Connection selector */ |
41 | unsigned state; /* Current state of the world */ |
16dfe536 |
42 | sel_timer t; /* Timeout selector */ |
e82f7154 |
43 | bres_client r; /* Backgd resolver client block */ |
16dfe536 |
44 | ident_request i; /* Ident client block */ |
fab71203 |
45 | char host[128]; /* Resolved hostname */ |
46 | char user[64]; /* Authenticated client user */ |
e82f7154 |
47 | } id; |
48 | |
49 | #define S_HOST 1u /* Read the hostname from resolver */ |
50 | #define S_USER 2u /* Read the username from RFC931 */ |
16dfe536 |
51 | #define S_TIMER 4u /* Timeout has completed */ |
e82f7154 |
52 | |
53 | /*----- Main code ---------------------------------------------------------*/ |
54 | |
55 | /* --- @id_done@ --- * |
56 | * |
57 | * Arguments: @id *i@ = pointer to identification block |
58 | * |
59 | * Returns: --- |
60 | * |
61 | * Use: Finishes with an identification block. |
62 | */ |
63 | |
64 | static void id_done(id *i) |
65 | { |
e82f7154 |
66 | /* --- Close down the various dependent bits --- */ |
67 | |
68 | if (!(i->state & S_HOST)) |
69 | bres_abort(&i->r); |
16dfe536 |
70 | if (!(i->state & S_USER)) |
71 | ident_abort(&i->i); |
e82f7154 |
72 | if (!(i->state & S_TIMER)) |
73 | sel_rmtimer(&i->t); |
74 | |
75 | /* --- Report the final result --- */ |
76 | |
ee599f55 |
77 | fw_log(i->when, "[%s] %s from %s@%s [%s:%u]", |
e5398e09 |
78 | i->q.desc, i->q.act, |
ee599f55 |
79 | i->user, i->host, |
80 | inet_ntoa(i->q.rsin.sin_addr), (unsigned)ntohs(i->q.rsin.sin_port)); |
e82f7154 |
81 | |
82 | /* --- Dispose of the block --- */ |
83 | |
e5398e09 |
84 | REFFD_DEC(i->q.r); |
858a73af |
85 | xfree((/*unconst*/ char *)i->q.desc); |
b0805b27 |
86 | xfree(i); |
e82f7154 |
87 | } |
88 | |
89 | /* --- @id_res@ --- * |
90 | * |
16dfe536 |
91 | * Arguments: @struct hostent *h@ = name of the resolved host |
e82f7154 |
92 | * @void *vp@ = pointer to identification block |
93 | * |
94 | * Returns: --- |
95 | * |
96 | * Use: Responds to a completed reverse name resolution. |
97 | */ |
98 | |
16dfe536 |
99 | static void id_res(struct hostent *h, void *vp) |
e82f7154 |
100 | { |
101 | id *i = vp; |
16dfe536 |
102 | if (h) |
103 | str_sanitize(i->host, h->h_name, sizeof(i->host)); |
e82f7154 |
104 | i->state |= S_HOST; |
105 | if (i->state & S_USER) |
106 | id_done(i); |
107 | } |
108 | |
109 | /* --- @id_ident@ --- * |
110 | * |
16dfe536 |
111 | * Arguments: @ident_reply *i@ = pointer to string read from server |
e82f7154 |
112 | * @void *vp@ = pointer to identification block |
113 | * |
114 | * Returns: --- |
115 | * |
116 | * Use: Responds to a line read from the remote RFC931 server. |
117 | */ |
118 | |
16dfe536 |
119 | static void id_ident(ident_reply *ir, void *vp) |
e82f7154 |
120 | { |
121 | id *i = vp; |
122 | |
16dfe536 |
123 | /* --- Read the information from the client --- */ |
e82f7154 |
124 | |
16dfe536 |
125 | if (ir && ir->type == IDENT_USERID) |
126 | str_sanitize(i->user, ir->u.userid.user, sizeof(i->user)); |
e82f7154 |
127 | |
128 | /* --- Maybe finish off this identification --- */ |
129 | |
16dfe536 |
130 | i->state |= S_USER; |
e82f7154 |
131 | if (i->state & S_HOST) |
132 | id_done(i); |
133 | } |
134 | |
e82f7154 |
135 | /* --- @id_timer@ --- * |
136 | * |
137 | * Arguments: @struct timeval *tv@ = pointer to the current time |
138 | * @void *vp@ = pointer to identification block |
139 | * |
140 | * Returns: --- |
141 | * |
142 | * Use: Times an identification job out. |
143 | */ |
144 | |
145 | static void id_timer(struct timeval *tv, void *vp) |
146 | { |
147 | id *i = vp; |
547dc0e8 |
148 | i->state |= S_TIMER; |
e82f7154 |
149 | id_done(i); |
150 | } |
151 | |
152 | /* --- @identify@ --- * |
153 | * |
154 | * Arguments: @const id_req *q@ = pointer to request block |
e82f7154 |
155 | * |
156 | * Returns: --- |
157 | * |
158 | * Use: Starts a background ident lookup and reverse-resolve job |
159 | * which will, eventually, report a message to the system log. |
160 | */ |
161 | |
e5398e09 |
162 | void identify(const id_req *q) |
e82f7154 |
163 | { |
164 | id *i; |
165 | |
166 | /* --- Initialize the block with stuff --- */ |
167 | |
168 | i = xmalloc(sizeof(*i)); |
169 | i->q = *q; |
858a73af |
170 | i->q.desc = xstrdup(q->desc); |
e5398e09 |
171 | REFFD_INC(i->q.r); |
e82f7154 |
172 | |
173 | str_sanitize(i->host, inet_ntoa(q->rsin.sin_addr), sizeof(i->host)); |
174 | strcpy(i->user, "<ANONYMOUS>"); |
175 | i->state = 0; |
176 | i->when = time(0); |
177 | |
178 | /* --- Set up the connection to the identity server --- */ |
179 | |
16dfe536 |
180 | ident(&i->i, sel, &q->lsin, &q->rsin, id_ident, i); |
e82f7154 |
181 | |
182 | /* --- Set up the name resolver --- */ |
183 | |
16dfe536 |
184 | bres_byaddr(&i->r, q->rsin.sin_addr, id_res, i); |
e82f7154 |
185 | |
186 | /* --- Set up the time limiter --- */ |
187 | |
188 | { |
189 | struct timeval tv; |
190 | gettimeofday(&tv, 0); |
191 | tv.tv_sec += TIMEOUT; |
192 | sel_addtimer(sel, &i->t, &tv, id_timer, i); |
193 | } |
194 | } |
195 | |
196 | /*----- That's all, folks -------------------------------------------------*/ |