Whitespace fixing.
[fwd] / identify.c
CommitLineData
e82f7154 1/* -*-c-*-
2 *
e82f7154 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
37typedef 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
64static 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);
b0805b27 85 xfree(i);
e82f7154 86}
87
88/* --- @id_res@ --- *
89 *
16dfe536 90 * Arguments: @struct hostent *h@ = name of the resolved host
e82f7154 91 * @void *vp@ = pointer to identification block
92 *
93 * Returns: ---
94 *
95 * Use: Responds to a completed reverse name resolution.
96 */
97
16dfe536 98static void id_res(struct hostent *h, void *vp)
e82f7154 99{
100 id *i = vp;
16dfe536 101 if (h)
102 str_sanitize(i->host, h->h_name, sizeof(i->host));
e82f7154 103 i->state |= S_HOST;
104 if (i->state & S_USER)
105 id_done(i);
106}
107
108/* --- @id_ident@ --- *
109 *
16dfe536 110 * Arguments: @ident_reply *i@ = pointer to string read from server
e82f7154 111 * @void *vp@ = pointer to identification block
112 *
113 * Returns: ---
114 *
115 * Use: Responds to a line read from the remote RFC931 server.
116 */
117
16dfe536 118static void id_ident(ident_reply *ir, void *vp)
e82f7154 119{
120 id *i = vp;
121
16dfe536 122 /* --- Read the information from the client --- */
e82f7154 123
16dfe536 124 if (ir && ir->type == IDENT_USERID)
125 str_sanitize(i->user, ir->u.userid.user, sizeof(i->user));
e82f7154 126
127 /* --- Maybe finish off this identification --- */
128
16dfe536 129 i->state |= S_USER;
e82f7154 130 if (i->state & S_HOST)
131 id_done(i);
132}
133
e82f7154 134/* --- @id_timer@ --- *
135 *
136 * Arguments: @struct timeval *tv@ = pointer to the current time
137 * @void *vp@ = pointer to identification block
138 *
139 * Returns: ---
140 *
141 * Use: Times an identification job out.
142 */
143
144static void id_timer(struct timeval *tv, void *vp)
145{
146 id *i = vp;
547dc0e8 147 i->state |= S_TIMER;
e82f7154 148 id_done(i);
149}
150
151/* --- @identify@ --- *
152 *
153 * Arguments: @const id_req *q@ = pointer to request block
e82f7154 154 *
155 * Returns: ---
156 *
157 * Use: Starts a background ident lookup and reverse-resolve job
158 * which will, eventually, report a message to the system log.
159 */
160
e5398e09 161void identify(const id_req *q)
e82f7154 162{
163 id *i;
164
165 /* --- Initialize the block with stuff --- */
166
167 i = xmalloc(sizeof(*i));
168 i->q = *q;
e5398e09 169 REFFD_INC(i->q.r);
e82f7154 170
171 str_sanitize(i->host, inet_ntoa(q->rsin.sin_addr), sizeof(i->host));
172 strcpy(i->user, "<ANONYMOUS>");
173 i->state = 0;
174 i->when = time(0);
175
176 /* --- Set up the connection to the identity server --- */
177
16dfe536 178 ident(&i->i, sel, &q->lsin, &q->rsin, id_ident, i);
e82f7154 179
180 /* --- Set up the name resolver --- */
181
16dfe536 182 bres_byaddr(&i->r, q->rsin.sin_addr, id_res, i);
e82f7154 183
184 /* --- Set up the time limiter --- */
185
186 {
187 struct timeval tv;
188 gettimeofday(&tv, 0);
189 tv.tv_sec += TIMEOUT;
190 sel_addtimer(sel, &i->t, &tv, id_timer, i);
191 }
192}
193
194/*----- That's all, folks -------------------------------------------------*/