3 * $Id: identify.c,v 1.11 2004/04/08 01:36:25 mdw Exp $
5 * Identifies and logs the client of a connection
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
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.
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.
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.
29 /*----- Header files ------------------------------------------------------*/
39 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
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>
56 #include <mLib/selbuf.h>
62 /*----- Magic numbers -----------------------------------------------------*/
64 #define TIMEOUT 15 /* Seconds to wait for answers */
66 /*----- Data structures ---------------------------------------------------*/
68 /* --- Structure to track the progress of an identification --- */
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 */
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 */
86 /*----- Main code ---------------------------------------------------------*/
88 /* --- @id_done@ --- *
90 * Arguments: @id *i@ = pointer to identification block
94 * Use: Finishes with an identification block.
97 static void id_done(id
*i
)
99 /* --- Close down the various dependent bits --- */
101 if (!(i
->state
& S_HOST
))
103 if (!(i
->state
& S_USER
))
105 if (!(i
->state
& S_TIMER
))
108 /* --- Report the final result --- */
110 fw_log(i
->when
, "[%s] %s from %s@%s [%s:%u]",
113 inet_ntoa(i
->q
.rsin
.sin_addr
), (unsigned)ntohs(i
->q
.rsin
.sin_port
));
115 /* --- Dispose of the block --- */
121 /* --- @id_res@ --- *
123 * Arguments: @struct hostent *h@ = name of the resolved host
124 * @void *vp@ = pointer to identification block
128 * Use: Responds to a completed reverse name resolution.
131 static void id_res(struct hostent
*h
, void *vp
)
135 str_sanitize(i
->host
, h
->h_name
, sizeof(i
->host
));
137 if (i
->state
& S_USER
)
141 /* --- @id_ident@ --- *
143 * Arguments: @ident_reply *i@ = pointer to string read from server
144 * @void *vp@ = pointer to identification block
148 * Use: Responds to a line read from the remote RFC931 server.
151 static void id_ident(ident_reply
*ir
, void *vp
)
155 /* --- Read the information from the client --- */
157 if (ir
&& ir
->type
== IDENT_USERID
)
158 str_sanitize(i
->user
, ir
->u
.userid
.user
, sizeof(i
->user
));
160 /* --- Maybe finish off this identification --- */
163 if (i
->state
& S_HOST
)
167 /* --- @id_timer@ --- *
169 * Arguments: @struct timeval *tv@ = pointer to the current time
170 * @void *vp@ = pointer to identification block
174 * Use: Times an identification job out.
177 static void id_timer(struct timeval
*tv
, void *vp
)
184 /* --- @identify@ --- *
186 * Arguments: @const id_req *q@ = pointer to request block
190 * Use: Starts a background ident lookup and reverse-resolve job
191 * which will, eventually, report a message to the system log.
194 void identify(const id_req
*q
)
198 /* --- Initialize the block with stuff --- */
200 i
= xmalloc(sizeof(*i
));
204 str_sanitize(i
->host
, inet_ntoa(q
->rsin
.sin_addr
), sizeof(i
->host
));
205 strcpy(i
->user
, "<ANONYMOUS>");
209 /* --- Set up the connection to the identity server --- */
211 ident(&i
->i
, sel
, &q
->lsin
, &q
->rsin
, id_ident
, i
);
213 /* --- Set up the name resolver --- */
215 bres_byaddr(&i
->r
, q
->rsin
.sin_addr
, id_res
, i
);
217 /* --- Set up the time limiter --- */
221 gettimeofday(&tv
, 0);
222 tv
.tv_sec
+= TIMEOUT
;
223 sel_addtimer(sel
, &i
->t
, &tv
, id_timer
, i
);
227 /*----- That's all, folks -------------------------------------------------*/