e35799cf6067d7bb276ef7ce7c561c548b7d546c
3 * $Id: identify.c,v 1.9 2003/11/29 22:13:43 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 /*----- Revision history --------------------------------------------------*
31 * $Log: identify.c,v $
32 * Revision 1.9 2003/11/29 22:13:43 mdw
33 * Fix bug in identification timout handling.
35 * Revision 1.8 2003/11/29 20:36:07 mdw
36 * Privileged outgoing connections.
38 * Revision 1.7 2002/02/22 23:43:32 mdw
39 * Call @xfree@ rather than @free@.
41 * Revision 1.6 2001/06/22 19:36:49 mdw
42 * Enlarge the identity buffer.
44 * Revision 1.5 1999/10/10 16:45:34 mdw
45 * Modified to use new mLib resolver and ident client.
47 * Revision 1.4 1999/07/27 18:30:53 mdw
48 * Various minor portability fixes.
50 * Revision 1.3 1999/07/26 23:26:21 mdw
51 * Minor modifications for new design.
53 * Revision 1.2 1999/07/03 13:56:59 mdw
54 * Log connections to syslog or stderr as appropriate.
56 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
61 /*----- Header files ------------------------------------------------------*/
71 #include <sys/types.h>
76 #include <sys/socket.h>
77 #include <netinet/in.h>
78 #include <arpa/inet.h>
81 #include <mLib/alloc.h>
82 #include <mLib/bres.h>
83 #include <mLib/conn.h>
84 #include <mLib/dstr.h>
85 #include <mLib/ident.h>
86 #include <mLib/report.h>
88 #include <mLib/selbuf.h>
94 /*----- Magic numbers -----------------------------------------------------*/
96 #define TIMEOUT 15 /* Seconds to wait for answers */
98 /*----- Data structures ---------------------------------------------------*/
100 /* --- Structure to track the progress of an identification --- */
103 id_req q
; /* Copy of client's request block */
104 time_t when
; /* When the connection occurred */
105 conn c
; /* Connection selector */
106 unsigned state
; /* Current state of the world */
107 sel_timer t
; /* Timeout selector */
108 bres_client r
; /* Backgd resolver client block */
109 ident_request i
; /* Ident client block */
110 char host
[128]; /* Resolved hostname */
111 char user
[64]; /* Authenticated client user */
114 #define S_HOST 1u /* Read the hostname from resolver */
115 #define S_USER 2u /* Read the username from RFC931 */
116 #define S_TIMER 4u /* Timeout has completed */
118 /*----- Main code ---------------------------------------------------------*/
120 /* --- @id_done@ --- *
122 * Arguments: @id *i@ = pointer to identification block
126 * Use: Finishes with an identification block.
129 static void id_done(id
*i
)
131 /* --- Close down the various dependent bits --- */
133 if (!(i
->state
& S_HOST
))
135 if (!(i
->state
& S_USER
))
137 if (!(i
->state
& S_TIMER
))
140 /* --- Report the final result --- */
142 fw_log(i
->when
, "[%s] %s from %s@%s [%s:%u]",
145 inet_ntoa(i
->q
.rsin
.sin_addr
), (unsigned)ntohs(i
->q
.rsin
.sin_port
));
147 /* --- Dispose of the block --- */
153 /* --- @id_res@ --- *
155 * Arguments: @struct hostent *h@ = name of the resolved host
156 * @void *vp@ = pointer to identification block
160 * Use: Responds to a completed reverse name resolution.
163 static void id_res(struct hostent
*h
, void *vp
)
167 str_sanitize(i
->host
, h
->h_name
, sizeof(i
->host
));
169 if (i
->state
& S_USER
)
173 /* --- @id_ident@ --- *
175 * Arguments: @ident_reply *i@ = pointer to string read from server
176 * @void *vp@ = pointer to identification block
180 * Use: Responds to a line read from the remote RFC931 server.
183 static void id_ident(ident_reply
*ir
, void *vp
)
187 /* --- Read the information from the client --- */
189 if (ir
&& ir
->type
== IDENT_USERID
)
190 str_sanitize(i
->user
, ir
->u
.userid
.user
, sizeof(i
->user
));
192 /* --- Maybe finish off this identification --- */
195 if (i
->state
& S_HOST
)
199 /* --- @id_timer@ --- *
201 * Arguments: @struct timeval *tv@ = pointer to the current time
202 * @void *vp@ = pointer to identification block
206 * Use: Times an identification job out.
209 static void id_timer(struct timeval
*tv
, void *vp
)
216 /* --- @identify@ --- *
218 * Arguments: @const id_req *q@ = pointer to request block
222 * Use: Starts a background ident lookup and reverse-resolve job
223 * which will, eventually, report a message to the system log.
226 void identify(const id_req
*q
)
230 /* --- Initialize the block with stuff --- */
232 i
= xmalloc(sizeof(*i
));
236 str_sanitize(i
->host
, inet_ntoa(q
->rsin
.sin_addr
), sizeof(i
->host
));
237 strcpy(i
->user
, "<ANONYMOUS>");
241 /* --- Set up the connection to the identity server --- */
243 ident(&i
->i
, sel
, &q
->lsin
, &q
->rsin
, id_ident
, i
);
245 /* --- Set up the name resolver --- */
247 bres_byaddr(&i
->r
, q
->rsin
.sin_addr
, id_res
, i
);
249 /* --- Set up the time limiter --- */
253 gettimeofday(&tv
, 0);
254 tv
.tv_sec
+= TIMEOUT
;
255 sel_addtimer(sel
, &i
->t
, &tv
, id_timer
, i
);
259 /*----- That's all, folks -------------------------------------------------*/