3 * $Id: identify.c,v 1.1 1999/07/01 08:56:23 mdw Exp $
5 * Identifies and logs the client of a connection
7 * (c) 1999 Mark Wooding
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.1 1999/07/01 08:56:23 mdw
37 /*----- Header files ------------------------------------------------------*/
47 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
57 #include <mLib/alloc.h>
58 #include <mLib/conn.h>
59 #include <mLib/dstr.h>
60 #include <mLib/report.h>
62 #include <mLib/selbuf.h>
70 /*----- Magic numbers -----------------------------------------------------*/
72 #define TIMEOUT 15 /* Seconds to wait for answers */
74 /*----- Data structures ---------------------------------------------------*/
76 /* --- Structure to track the progress of an identification --- */
79 id_req q; /* Copy of client's request block */
80 void (*func)(void */*p*/); /* Function to call when done */
81 void *p; /* Argument to pass to function */
82 time_t when; /* When the connection occurred */
83 conn c; /* Connection selector */
84 unsigned state; /* Current state of the world */
85 bres_client r; /* Backgd resolver client block */
86 char host[64]; /* Resolved hostname */
87 char user[32]; /* Authenticated client user */
88 sel_timer t; /* Timeout selector */
89 selbuf id; /* Reader for the RFC931 client */
92 #define S_HOST 1u /* Read the hostname from resolver */
93 #define S_USER 2u /* Read the username from RFC931 */
94 #define S_UCONN 4u /* Connected to remote RFC931 */
95 #define S_TIMER 8u /* Timeout has completed */
97 /*----- Main code ---------------------------------------------------------*/
99 /* --- @id_done@ --- *
101 * Arguments: @id *i@ = pointer to identification block
105 * Use: Finishes with an identification block.
108 static void id_done(id *i)
113 /* --- Close down the various dependent bits --- */
115 if (!(i->state & S_HOST))
117 if (!(i->state & S_UCONN))
119 else if (!(i->state & S_USER))
120 selbuf_disable(&i->id);
121 if (!(i->state & S_TIMER))
124 /* --- Report the final result --- */
126 tm = localtime(&i->when);
127 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
128 syslog(LOG_NOTICE, "%s %s %s from %s@%s\n",
129 buf, i->q.desc, i->q.act, i->user, i->host);
131 /* --- Dispose of the block --- */
137 /* --- @id_res@ --- *
139 * Arguments: @const char *host@ = name of the resolved host
140 * @void *vp@ = pointer to identification block
144 * Use: Responds to a completed reverse name resolution.
147 static void id_res(const char *host, void *vp)
150 str_sanitize(i->host, host, sizeof(i->host));
152 if (i->state & S_USER)
156 /* --- @id_ident@ --- *
158 * Arguments: @char *p@ = pointer to string read from server
159 * @void *vp@ = pointer to identification block
163 * Use: Responds to a line read from the remote RFC931 server.
166 static void id_ident(char *p, void *vp)
170 /* --- Get rid of the connection --- */
173 selbuf_disable(&i->id);
174 close(i->id.reader.fd);
176 /* --- Read the information from the returned line --- */
180 ident_parse(p, &idbuf);
181 if (idbuf.type == ident_userid)
182 str_sanitize(i->user, idbuf.u.userid.user, sizeof(i->user));
185 /* --- Maybe finish off this identification --- */
187 if (i->state & S_HOST)
191 /* --- @id_conn@ --- *
193 * Arguments: @int fd@ = file descriptor connected
194 * @void *vp@ = pointer to identification block
198 * Use: Responds to a completed connection to the remote RFC931
202 static void id_conn(int fd, void *vp)
207 i->state |= S_USER | S_UCONN;
208 if (i->state & S_HOST)
212 dstr_putf(&d, "%u, %u\n",
213 ntohs(i->q.rsin.sin_port), ntohs(i->q.lsin.sin_port));
214 write(fd, d.buf, d.len);
217 selbuf_init(&i->id, sel, fd, id_ident, i);
221 /* --- @id_timer@ --- *
223 * Arguments: @struct timeval *tv@ = pointer to the current time
224 * @void *vp@ = pointer to identification block
228 * Use: Times an identification job out.
231 static void id_timer(struct timeval *tv, void *vp)
237 /* --- @identify@ --- *
239 * Arguments: @const id_req *q@ = pointer to request block
240 * @void (*func)(void *p)@ = function to call when done
241 * @void *p@ = argument to pass to function
245 * Use: Starts a background ident lookup and reverse-resolve job
246 * which will, eventually, report a message to the system log.
249 void identify(const id_req *q,
250 void (*func)(void */*p*/), void *p)
254 /* --- Initialize the block with stuff --- */
256 i = xmalloc(sizeof(*i));
261 str_sanitize(i->host, inet_ntoa(q->rsin.sin_addr), sizeof(i->host));
262 strcpy(i->user, "<ANONYMOUS>");
266 /* --- Set up the connection to the identity server --- */
270 struct sockaddr_in sin;
272 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
275 sin.sin_family = AF_INET;
276 sin.sin_addr = q->lsin.sin_addr;
278 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin))) {
282 sin.sin_family = AF_INET;
283 sin.sin_addr = q->rsin.sin_addr;
284 sin.sin_port = htons(113);
285 conn_init(&i->c, sel, fd,
286 (struct sockaddr *)&sin, sizeof(sin),
292 /* --- Set up the name resolver --- */
294 bres_resolve(&i->r, q->rsin.sin_addr, id_res, i);
296 /* --- Set up the time limiter --- */
300 gettimeofday(&tv, 0);
301 tv.tv_sec += TIMEOUT;
302 sel_addtimer(sel, &i->t, &tv, id_timer, i);
306 /*----- That's all, folks -------------------------------------------------*/