1 /*
2  * ident_socket2buffer.c
3  *
4  * (C)1999-2011 by Marc Huber <Marc.Huber@web.de>
5  * All rights reserved.
6  *
7  * $Id: ident_socket2buffer.c,v 1.12 2015/03/14 06:11:26 marc Exp marc $
8  *
9  */
10 
11 #include "headers.h"
12 #include "foobar.h"
13 
14 static const char rcsid[] __attribute__ ((used)) = "$Id: ident_socket2buffer.c,v 1.12 2015/03/14 06:11:26 marc Exp marc $";
15 
ident_socket2buffer(struct context * ctx,int cur)16 void ident_socket2buffer(struct context *ctx, int cur)
17 {
18     ssize_t l;
19 
20     DebugIn(DEBUG_NET);
21 
22     l = read(cur, ctx->ident_buf + ctx->ident_buflen, MAXBUFSIZE1413 - ctx->ident_buflen - 1);
23 
24     if (l > 0) {
25 	char *t;
26 
27 	ctx->ident_buflen += l;
28 	ctx->ident_buf[ctx->ident_buflen] = 0;
29 	if ((t = strstr(ctx->ident_buf, "\r\n"))) {
30 	    int rp, lp;
31 	    *t = 0;
32 	    Debug((DEBUG_PROC, "RFC1413 answer: \"%s\"\n", ctx->ident_buf));
33 
34 	    if (2 == sscanf(ctx->ident_buf, " %d , %d :", &rp, &lp) && su_get_port(&ctx->sa_c_local) == lp && su_get_port(&ctx->sa_c_remote) == rp) {
35 		char buf[160];
36 		char *u = rfc2428_str(&ctx->sa_c_remote, buf, sizeof(buf));
37 		if (u) {
38 		    t = alloca(strlen(u) + 1);
39 		    strcpy(t, u);
40 		}
41 		u = strchr(ctx->ident_buf, ':');
42 		if (u)
43 		    do
44 			u++;
45 		    while (*u && isspace((int) *u));
46 
47 		if (ctx->loglevel & LOG_IDENT)
48 		    logmsg("%s->%s: %s", t ? t : "", rfc2428_str(&ctx->sa_c_local, buf, sizeof(buf)), u ? u : ctx->ident_buf);
49 
50 		if (u && !strncasecmp("USERID", u, 6)) {
51 		    u += 6;
52 		    while (*u && isspace((int) *u))
53 			u++;
54 		    if (*u == ':') {
55 			do
56 			    u++;
57 			while (*u && *u != ':');
58 			if (*u) {
59 			    do
60 				u++;
61 			    while (*u && isspace((int) *u));
62 			    if (*u)
63 				strset(&ctx->ident_user, u);
64 			}
65 		    }
66 		}
67 	    }
68 
69 	    cleanup_ident(ctx, cur);
70 	} else if (ctx->ident_buflen == MAXBUFSIZE1413 - 1)	/* response too long */
71 	    cleanup_ident(ctx, cur);
72     } else if (!l || (l < 0 && errno != EAGAIN))
73 	cleanup_ident(ctx, cur);
74 
75     DebugOut(DEBUG_NET);
76 }
77