chiark / gitweb /
Privileged outgoing connections.
[fwd] / identify.c
1 /* -*-c-*-
2  *
3  * $Id: identify.c,v 1.8 2003/11/29 20:36:07 mdw Exp $
4  *
5  * Identifies and logs the client of a connection
6  *
7  * (c) 1999 Straylight/Edgeware
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 $
32  * Revision 1.8  2003/11/29 20:36:07  mdw
33  * Privileged outgoing connections.
34  *
35  * Revision 1.7  2002/02/22 23:43:32  mdw
36  * Call @xfree@ rather than @free@.
37  *
38  * Revision 1.6  2001/06/22 19:36:49  mdw
39  * Enlarge the identity buffer.
40  *
41  * Revision 1.5  1999/10/10 16:45:34  mdw
42  * Modified to use new mLib resolver and ident client.
43  *
44  * Revision 1.4  1999/07/27 18:30:53  mdw
45  * Various minor portability fixes.
46  *
47  * Revision 1.3  1999/07/26 23:26:21  mdw
48  * Minor modifications for new design.
49  *
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.
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>
79 #include <mLib/bres.h>
80 #include <mLib/conn.h>
81 #include <mLib/dstr.h>
82 #include <mLib/ident.h>
83 #include <mLib/report.h>
84 #include <mLib/sel.h>
85 #include <mLib/selbuf.h>
86 #include <mLib/str.h>
87
88 #include "fw.h"
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 */
101   time_t when;                          /* When the connection occurred */
102   conn c;                               /* Connection selector */
103   unsigned state;                       /* Current state of the world */
104   sel_timer t;                          /* Timeout selector */
105   bres_client r;                        /* Backgd resolver client block */
106   ident_request i;                      /* Ident client block */
107   char host[128];                       /* Resolved hostname */
108   char user[64];                        /* Authenticated client user */
109 } id;
110
111 #define S_HOST 1u                       /* Read the hostname from resolver */
112 #define S_USER 2u                       /* Read the username from RFC931 */
113 #define S_TIMER 4u                      /* Timeout has completed */
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 {
128   /* --- Close down the various dependent bits --- */
129
130   if (!(i->state & S_HOST))
131     bres_abort(&i->r);
132   if (!(i->state & S_USER))
133     ident_abort(&i->i);
134   if (!(i->state & S_TIMER))
135     sel_rmtimer(&i->t);
136
137   /* --- Report the final result --- */
138
139   fw_log(i->when, "[%s] %s from %s@%s [%s:%u]",
140          i->q.desc, i->q.act,
141          i->user, i->host,
142          inet_ntoa(i->q.rsin.sin_addr), (unsigned)ntohs(i->q.rsin.sin_port));
143
144   /* --- Dispose of the block --- */
145
146   REFFD_DEC(i->q.r);
147   xfree(i);
148 }
149
150 /* --- @id_res@ --- *
151  *
152  * Arguments:   @struct hostent *h@ = name of the resolved host
153  *              @void *vp@ = pointer to identification block
154  *
155  * Returns:     ---
156  *
157  * Use:         Responds to a completed reverse name resolution.
158  */
159
160 static void id_res(struct hostent *h, void *vp)
161 {
162   id *i = vp;
163   if (h)
164     str_sanitize(i->host, h->h_name, sizeof(i->host));
165   i->state |= S_HOST;
166   if (i->state & S_USER)
167     id_done(i);
168 }
169
170 /* --- @id_ident@ --- *
171  *
172  * Arguments:   @ident_reply *i@ = pointer to string read from server
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
180 static void id_ident(ident_reply *ir, void *vp)
181 {
182   id *i = vp;
183
184   /* --- Read the information from the client --- */
185
186   if (ir && ir->type == IDENT_USERID)
187     str_sanitize(i->user, ir->u.userid.user, sizeof(i->user));
188
189   /* --- Maybe finish off this identification --- */
190
191   i->state |= S_USER;
192   if (i->state & S_HOST)
193     id_done(i);
194 }
195
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
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
222 void identify(const id_req *q)
223 {
224   id *i;
225
226   /* --- Initialize the block with stuff --- */
227
228   i = xmalloc(sizeof(*i));
229   i->q = *q;
230   REFFD_INC(i->q.r);
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
239   ident(&i->i, sel, &q->lsin, &q->rsin, id_ident, i);
240
241   /* --- Set up the name resolver --- */
242
243   bres_byaddr(&i->r, q->rsin.sin_addr, id_res, i);
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 -------------------------------------------------*/