summary refs log blame commit diff stats
path: root/src/common/identd.c
blob: 919282ea9bc250cf35bafc00333191ab3c03b487 (plain) (tree)
























































































                                                                                         
/* simple identd server for xchat under win32 */


static int identd_is_running = FALSE;


static int
identd (char *username)
{
	int sok, read_sok, len;
	char *p;
	char buf[256];
	char outbuf[256];
	struct sockaddr_in addr;

	sok = socket (AF_INET, SOCK_STREAM, 0);
	if (sok == INVALID_SOCKET)
	{
		free (username);
		return 0;
	}

	len = 1;
	setsockopt (sok, SOL_SOCKET, SO_REUSEADDR, (char *) &len, sizeof (len));

	memset (&addr, 0, sizeof (addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons (113);

	if (bind (sok, (struct sockaddr *) &addr, sizeof (addr)) == SOCKET_ERROR)
	{
		closesocket (sok);
		free (username);
		return 0;
	}

	if (listen (sok, 1) == SOCKET_ERROR)
	{
		closesocket (sok);
		free (username);
		return 0;
	}

	len = sizeof (addr);
	read_sok = accept (sok, (struct sockaddr *) &addr, &len);
	closesocket (sok);
	if (read_sok == INVALID_SOCKET)
	{
		free (username);
		return 0;
	}

	identd_is_running = FALSE;

	snprintf (outbuf, sizeof (outbuf), "%%\tServicing ident request from %s\n",
				 inet_ntoa (addr.sin_addr));
	PrintText (current_sess, outbuf);

	recv (read_sok, buf, sizeof (buf) - 1, 0);
	buf[sizeof (buf) - 1] = 0;	  /* ensure null termination */

	p = strchr (buf, ',');
	if (p)
	{
		snprintf (outbuf, sizeof (outbuf) - 1, "%d, %d : USERID : UNIX : %s\r\n",
					 atoi (buf), atoi (p + 1), username);
		outbuf[sizeof (outbuf) - 1] = 0;	/* ensure null termination */
		send (read_sok, outbuf, strlen (outbuf), 0);
	}

	sleep (1);
	closesocket (read_sok);
	free (username);

	return 0;
}

static void
identd_start (char *username)
{
	DWORD tid;

	if (identd_is_running == FALSE)
	{
		identd_is_running = TRUE;
		CloseHandle (CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) identd,
						 strdup (username), 0, &tid));
	}
}
pan>guint16 magic45; /* 213-214: 0x44, 0x00 */ char pad45[2]; /* 215-216 */ guint16 magic50; /* 217-218: 0x39, 0x00 */ char pad50[2]; /* 219-220 */ char data[256]; /* 221-EOP: a sequence of NULL-terminated strings: - username; - empty string (just a NULL); - application name; - hostname */ } hello; struct { char pad1[4]; /* 39-42 */ guint16 magic2; /* 43-44 */ char pad10[12]; /* 45-56 */ guint32 bindaddr; /* 57-60: address to bind. */ guint16 bindport; /* 61-62: port to bind. */ char pad15[2]; /* 63-64 */ guint16 magic3; /* 65-66 */ guint16 boundport; /* 67-68 */ char pad20[104]; /* 69-172 */ char NTLMSSP[sizeof("NTLMSSP")]; /* 173-180: "NTLMSSP" */ guint32 msgtype; /* 181-184: NTLM message type = 1 */ guint32 flags; /* 185-188: NTLM message flags */ guint16 magic20; /* 189-190: 0x28, 0x00 */ char pad30[2]; /* 191-192 */ guint16 magic25; /* 193-194: 0x96, 0x82 */ guint16 magic30; /* 195-196: 0x01, 0x00 */ char pad40[12]; /* 197-208 */ guint16 magic50; /* 209-210: 0x30, 0x00 */ char pad50[6]; /* 211-216 */ guint16 magic55; /* 217-218: 0x30, 0x00 */ char pad55[2]; /* 219-220 */ char data[0]; /* Dummy end marker, no real data required */ } auth; struct { char pad1[4]; /* 39-42 */ guint16 magic1; /* 43-44 */ guint32 magic2; /* 45-48 */ char pad2[8]; /* 49-56 */ guint16 magic3; /* 57-58 */ char pad3[6]; /* 59-64 */ guint16 magic4; /* 65-66 */ guint16 boundport; /* 67-68 */ char pad4[104]; /* 69-172 */ char NTLMSSP[sizeof("NTLMSSP")]; /* 173-180: "NTLMSSP" */ guint32 msgtype; /* 181-184: NTLM message type = 3 */ struct ntlm_buffer lm_resp; /* 185-192: LM response security buffer */ struct ntlm_buffer ntlm_resp; /* 193-200: NTLM response security buffer */ struct ntlm_buffer ntdomain_buf; /* 201-208: domain name security buffer */ struct ntlm_buffer username_buf; /* 209-216: username security buffer */ struct ntlm_buffer clienthost_buf; /* 217-224: hostname security buffer */ struct ntlm_buffer sessionkey_buf; /* 225-232: session key security buffer */ guint32 flags; /* 233-236: message flags */ char data[1024]; /* 237-EOP: data area */ } auth2; struct { guint16 magic1; /* 39-40 */ char pad1[2]; /* 41-42 */ guint16 magic2; /* 43-44 */ guint32 magic3; /* 45-48 */ char pad5[8]; /* 48-56 */ guint16 magic6; /* 57-58: 0x0200 */ guint16 destport; /* 59-60 */ guint32 destaddr; /* 61-64 */ char pad10[4]; /* 65-68 */ guint16 magic10; /* 69-70 */ char pad15[2]; /* 71-72 */ guint16 srcport; /* 73-74: port client connects from */ char pad20[82]; /* 75-156 */ char executable[256]; /* 76-EOP: application name */ } connect; struct { guint16 magic1; /* 39-40 */ char pad5[2]; /* 41-42 */ guint16 magic5; /* 43-44 */ guint32 magic10; /* 45-48 */ char pad10[2]; /* 49-50 */ guint16 magic15; /* 51-52 */ guint32 magic16; /* 53-56 */ guint16 magic20; /* 57-58 */ guint16 clientport; /* 59-60: forwarded port. */ guint32 clientaddr; /* 61-64: forwarded address. */ guint32 magic30; /* 65-68 */ guint32 magic35; /* 69-72 */ guint16 serverport; /* 73-74: port server will connect to us from. */ guint16 srcport; /* 75-76: connect request; port used on client behalf. */ guint16 boundport; /* 77-78: bind request; port used on client behalf. */ guint32 boundaddr; /* 79-82: addr used on client behalf */ char pad30[90]; /* 83-172 */ char data[0]; /* End marker */ } connack; } packet; }; struct msproxy_response_t { guint32 packetid; /* 1-4 */ guint32 magic5; /* 5-8 */ guint32 serverid; /* 9-12 */ char clientack; /* 13: ack of last client packet. */ char pad5[3]; /* 14-16 */ unsigned char sequence; /* 17: sequence # of this packet. */ char pad10[7]; /* 18-24 */ char RWSP[4]; /* 25-28: 0x52,0x57,0x53,0x50 */ char pad15[8]; /* 29-36 */ guint16 command; /* 37-38 */ union { struct { char pad5[18]; /* 39-56 */ guint16 magic20; /* 57-58: 0x02, 0x00 */ char pad10[6]; /* 59-64 */ guint16 magic30; /* 65-66: 0x74, 0x01 */ char pad15[2]; /* 67-68 */ guint16 magic35; /* 69-70: 0x0c, 0x00 */ char pad20[6]; /* 71-76 */ guint16 magic50; /* 77-78: 0x04, 0x00 */ char pad30[6]; /* 79-84 */ guint16 magic60; /* 85-86: 0x65, 0x05 */ char pad35[2]; /* 87-88 */ guint16 magic65; /* 89-90: 0x02, 0x00 */ char pad40[8]; /* 91-98 */ guint16 udpport; /* 99-100 */ guint32 udpaddr; /* 101-104 */ } hello; struct { char pad1[6]; /* 39-44 */ guint32 magic10; /* 45-48 */ char pad3[10]; /* 49-58 */ guint16 boundport; /* 59-60: port server bound for us. */ guint32 boundaddr; /* 61-64: addr server bound for us. */ char pad10[4]; /* 65-68 */ guint16 magic15; /* 69-70 */ char pad15[102]; /* 70-172 */ char NTLMSSP[sizeof("NTLMSSP")]; /* 173-180: "NTLMSSP" */ guint32 msgtype; /* 181-184: NTLM message type = 2 */ struct ntlm_buffer target; /* 185-192: target security buffer */ guint32 flags; /* 193-196: NTLM message flags */ char challenge[8]; /* 197-204: NTLM challenge request */ char context[8]; /* 205-212: NTLM context */ char data[1024]; /* 213-EOP: target information data */ } auth; struct { guint16 magic1; /* 39-40 */ char pad5[18]; /* 41-58 */ guint16 clientport; /* 59-60: forwarded port. */ guint32 clientaddr; /* 61-64: forwarded address. */ guint32 magic10; /* 65-68 */ guint32 magic15; /* 69-72 */ guint16 serverport; /* 73-74: port server will connect to us from. */ guint16 srcport; /* 75-76: connect request; port used on client behalf. */ guint16 boundport; /* 77-78: bind request; port used on client behalf. */ guint32 boundaddr; /* 79-82: addr used on client behalf */ char pad10[90]; /* 83-172 */ } connect; } packet; }; #pragma pack() int traverse_msproxy (int sok, char *serverAddr, int port, struct msproxy_state_t *state, netstore *ns_proxy, int csok4, int csok6, int *csok, char bound); void msproxy_keepalive (void); #endif