identify.c: Stash a copy of the caller's description string.
[fwd] / identify.c
1 /* -*-c-*-
2 *
3 * Identifies and logs the client of a connection
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the `fwd' port forwarder.
11 *
12 * `fwd' is free software; you can redistribute it and/or modify
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.
16 *
17 * `fwd' is distributed in the hope that it will be useful,
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.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with `fwd'; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 #include "fwd.h"
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 */
39 time_t when; /* When the connection occurred */
40 conn c; /* Connection selector */
41 unsigned state; /* Current state of the world */
42 sel_timer t; /* Timeout selector */
43 bres_client r; /* Backgd resolver client block */
44 ident_request i; /* Ident client block */
45 char host[128]; /* Resolved hostname */
46 char user[64]; /* Authenticated client user */
47 } id;
48
49 #define S_HOST 1u /* Read the hostname from resolver */
50 #define S_USER 2u /* Read the username from RFC931 */
51 #define S_TIMER 4u /* Timeout has completed */
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 {
66 /* --- Close down the various dependent bits --- */
67
68 if (!(i->state & S_HOST))
69 bres_abort(&i->r);
70 if (!(i->state & S_USER))
71 ident_abort(&i->i);
72 if (!(i->state & S_TIMER))
73 sel_rmtimer(&i->t);
74
75 /* --- Report the final result --- */
76
77 fw_log(i->when, "[%s] %s from %s@%s [%s:%u]",
78 i->q.desc, i->q.act,
79 i->user, i->host,
80 inet_ntoa(i->q.rsin.sin_addr), (unsigned)ntohs(i->q.rsin.sin_port));
81
82 /* --- Dispose of the block --- */
83
84 REFFD_DEC(i->q.r);
85 xfree((/*unconst*/ char *)i->q.desc);
86 xfree(i);
87 }
88
89 /* --- @id_res@ --- *
90 *
91 * Arguments: @struct hostent *h@ = name of the resolved host
92 * @void *vp@ = pointer to identification block
93 *
94 * Returns: ---
95 *
96 * Use: Responds to a completed reverse name resolution.
97 */
98
99 static void id_res(struct hostent *h, void *vp)
100 {
101 id *i = vp;
102 if (h)
103 str_sanitize(i->host, h->h_name, sizeof(i->host));
104 i->state |= S_HOST;
105 if (i->state & S_USER)
106 id_done(i);
107 }
108
109 /* --- @id_ident@ --- *
110 *
111 * Arguments: @ident_reply *i@ = pointer to string read from server
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
119 static void id_ident(ident_reply *ir, void *vp)
120 {
121 id *i = vp;
122
123 /* --- Read the information from the client --- */
124
125 if (ir && ir->type == IDENT_USERID)
126 str_sanitize(i->user, ir->u.userid.user, sizeof(i->user));
127
128 /* --- Maybe finish off this identification --- */
129
130 i->state |= S_USER;
131 if (i->state & S_HOST)
132 id_done(i);
133 }
134
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;
148 i->state |= S_TIMER;
149 id_done(i);
150 }
151
152 /* --- @identify@ --- *
153 *
154 * Arguments: @const id_req *q@ = pointer to request block
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
162 void identify(const id_req *q)
163 {
164 id *i;
165
166 /* --- Initialize the block with stuff --- */
167
168 i = xmalloc(sizeof(*i));
169 i->q = *q;
170 i->q.desc = xstrdup(q->desc);
171 REFFD_INC(i->q.r);
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
180 ident(&i->i, sel, &q->lsin, &q->rsin, id_ident, i);
181
182 /* --- Set up the name resolver --- */
183
184 bres_byaddr(&i->r, q->rsin.sin_addr, id_res, i);
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 -------------------------------------------------*/