[go: up one dir, main page]

File: bncidentd.cpp

package info (click to toggle)
sbnc 1.3.9-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,156 kB
  • ctags: 4,719
  • sloc: ansic: 20,379; cpp: 14,175; sh: 12,783; tcl: 6,025; php: 448; makefile: 430; perl: 46; awk: 25
file content (209 lines) | stat: -rw-r--r-- 5,527 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*******************************************************************************
 * shroudBNC - an object-oriented framework for IRC                            *
 * Copyright (C) 2005-2014 Gunnar Beutner                                      *
 *                                                                             *
 * This program is free software; you can redistribute it and/or               *
 * modify it under the terms of the GNU General Public License                 *
 * as published by the Free Software Foundation; either version 2              *
 * of the License, or (at your option) any later version.                      *
 *                                                                             *
 * This program is distributed in the hope that it will be useful,             *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of              *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
 * GNU General Public License for more details.                                *
 *                                                                             *
 * You should have received a copy of the GNU General Public License           *
 * along with this program; if not, write to the Free Software                 *
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. *
 *******************************************************************************/

#include "../src/StdAfx.h"

#ifndef _WIN32
#error This module cannot be used on *nix systems.
#endif

class CIdentModule;

CCore *g_Bouncer;

class CIdentClient : public CConnection {
public:
	CIdentClient(SOCKET Client) : CConnection(Client) {
	}

	virtual void ParseLine(const char* Line) {
		if (Line[0] == '\0') {
			return;
		}

		if (g_Bouncer->GetIdent() == NULL) {
			g_Bouncer->Log("GetIdent() failed. identd not functional.");

			Destroy();

			return;
		}

		int LocalPort = 0, RemotePort = 0;
		char *dupLine = strdup(Line);
		char *StringRemotePort;

		if ((StringRemotePort = strstr(dupLine, ",")) != NULL) {
			StringRemotePort[0] = '\0';
			StringRemotePort++;

			RemotePort = atoi(StringRemotePort);
		}

		LocalPort = atoi(dupLine);
		free(dupLine);

		if (LocalPort == 0 || RemotePort == 0) {
			g_Bouncer->Log("Received invalid ident-request.");

			return;
		}

		int i = 0, LPort, RPort;
		const char *Ident;
		hash_t<CUser *> *UserHash;

		while ((UserHash = g_Bouncer->GetUsers()->Iterate(i++)) != NULL) {
			CUser *User = UserHash->Value;
			CIRCConnection *IRC = User->GetIRCConnection();

			if (!IRC || IRC->GetState() == State_Connected) {
				continue;
			}

			Ident = User->GetIdent();

			if (Ident == NULL) {
				Ident = UserHash->Name;
			}

			// 113 , 3559 : USERID : UNIX : shroud
			WriteLine("%d , %d : USERID : UNIX : %s", LocalPort, RemotePort, Ident);

			g_Bouncer->Log("Answered ident-request for %s", User->GetUsername());

			return;
		}

		FILE *IdentFile = fopen("ident", "r");

		if (IdentFile != NULL) {
			char IdentBuffer[50];

			if (fgets(IdentBuffer, sizeof(IdentBuffer), IdentFile) == NULL) {
				// TODO: error reply
				IdentBuffer[0] = '\0';
			} else {
				for (int i = strlen(IdentBuffer); i > 0; i--) {
					if (IdentBuffer[i] == '\r' || IdentBuffer[i] == '\n') {
						IdentBuffer[i] = '\0';
					}
				}
			}

			WriteLine("%d, %d : USERID : UNIX : %s", LocalPort, RemotePort, IdentBuffer);

			fclose(IdentFile);

			g_Bouncer->Log("Ident-request for unknown user. Returned ident from \"ident\" file: %s", IdentBuffer);
		} else {
			WriteLine("%d , %d : USERID : UNIX : %s", LocalPort, RemotePort, g_Bouncer->GetIdent());

			g_Bouncer->Log("Ident-request for unknown user.");
		}
	}

	virtual void Destroy(void) {
		delete this;
	}

	bool ShouldDestroy(void) const { return false; }

	const char *GetClassName(void) const {
		return "CIdentClient";
	}

	virtual int SSLVerify(int PreVerifyOk, X509_STORE_CTX *Context) const {
		return 0;
	}
};

IMPL_SOCKETLISTENER(CIdentListener) {
public:
	CIdentListener(int Family) : CListenerBase<CIdentListener>(113, NULL, Family) { }

	void Accept(SOCKET Client, const sockaddr *PeerAddress) {
		CIdentClient *Handler = new CIdentClient(Client);

		g_Bouncer->RegisterSocket(Client, Handler);
	}
};

class CIdentModule : public CModuleImplementation {
	CIdentListener *m_Listener, *m_ListenerV6;

	void Init(CCore* Root) {
		CModuleImplementation::Init(Root);

		g_Bouncer = Root;

		m_Listener = new CIdentListener(AF_INET);

		if (!m_Listener->IsValid()) {
			g_Bouncer->Log("Could not create listener for identd.");

			delete m_Listener;
			m_Listener = NULL;
		}

		g_Bouncer->Log("Created IPv4 identd listener.");

#ifdef HAVE_IPV6
		m_ListenerV6 = new CIdentListener(AF_INET6);

		if (!m_ListenerV6->IsValid()) {
			delete m_ListenerV6;

			m_ListenerV6 = NULL;
		} else {
			g_Bouncer->Log("Created IPv6 identd listener.");
		}
#endif
	}

	~CIdentModule(void) {
		const socket_t *SocketPv;

		if (m_Listener != NULL) {
			g_Bouncer->Log("Destroying IPv4 identd-listener.");

			m_Listener->Destroy();
		}

#ifdef HAVE_IPV6
		if (m_ListenerV6 != NULL) {
			m_ListenerV6->Destroy();

			g_Bouncer->Log("Destroying IPv4 identd-listener.");
		}
#endif

		while ((SocketPv = g_Bouncer->GetSocketByClass("CIdentClient", 0)) != NULL) {
			SocketPv->Events->Destroy();
		}
	}
};

extern "C" EXPORT CModuleFar *bncGetObject(void) {
	return (CModuleFar *)new CIdentModule();
}

extern "C" EXPORT int bncGetInterfaceVersion(void) {
	return INTERFACEVERSION;
}