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