Debian Codegraph @

src/authplugins/ident.cpp

raw
1 // Ident server auth plugin
2
3 // For all support, instructions and copyright go to:
4 // http://e2guardian.org/
5 // Released under the GPL v2, with the OpenSSL exception described in the README file.
6
7 // INCLUDES
8 #ifdef HAVE_CONFIG_H
9 #include "e2config.h"
10 #endif
11
12 #include "../Auth.hpp"
13 #include "../OptionContainer.hpp"
14 #include "../Logger.hpp"
15
16
17 // GLOBALS
18
19 extern OptionContainer o;
20
21 // DECLARATIONS
22
23 // class name is relevant!
24 class identinstance : public AuthPlugin
25 {
26 public:
27 identinstance(ConfigVar &definition)
28 : AuthPlugin(definition){
29 client_ip_based = true; // not sure if this is correct!!
30 };
31 int identify(Socket &peercon, Socket &proxycon, HTTPHeader &h, std::string &string, bool &is_real_user,auth_rec &authrec,NaughtyFilter &cm);
32 int init(void *args);
33 };
34
35 // IMPLEMENTATION
36
37 // class factory code *MUST* be included in every plugin
38
39 AuthPlugin *identcreate(ConfigVar &definition)
40 {
41 return new identinstance(definition);
42 }
43
44 // end of Class factory
45
46 // ident server username extraction
47 // checkme: needs better error reporting
48 int identinstance::identify(Socket &peercon, Socket &proxycon, HTTPHeader &h, std::string &string, bool &is_real_user,auth_rec &authrec,NaughtyFilter &cm)
49 {
50 std::string clientip;
51 bool use_xforwardedfor;
52 use_xforwardedfor = false;
53 if (o.use_xforwardedfor == 1) {
54 if (o.net.xforwardedfor_filter_ip.size() > 0) {
55 const char *ip = peercon.getPeerIP().c_str();
56 for (unsigned int i = 0; i < o.net.xforwardedfor_filter_ip.size(); i++) {
57 if (strcmp(ip, o.net.xforwardedfor_filter_ip[i].c_str()) == 0) {
58 use_xforwardedfor = true;
59 break;
60 }
61 }
62 } else {
63 use_xforwardedfor = true;
64 }
65 }
66
67 if (use_xforwardedfor) {
68 // grab the X-Forwarded-For IP if available
69 clientip = h.getXForwardedForIP();
70 // otherwise, grab the IP directly from the client connection
71 if (clientip.length() == 0)
72 clientip = peercon.getPeerIP();
73 } else {
74 clientip = peercon.getPeerIP();
75 }
76 int clientport = peercon.getPeerSourcePort();
77 int serverport = peercon.getPort();
78 DEBUG_auth("Connecting to: ", clientip, "to ask about: ", clientport);
79 Socket iq;
80 iq.setTimeout(5000);
81 int rc = iq.connect(clientip.c_str(), 113); // ident port
82 if (rc) {
83 DEBUG_auth("Error connecting to obtain ident from: ", clientip);
84 return E2AUTH_NOMATCH;
85 }
86 DEBUG_auth("Connected to:", clientip);
87 std::string request;
88 request = String(clientport).toCharArray();
89 request += ", ";
90 request += String(serverport).toCharArray();
91 request += "\r\n";
92
93 DEBUG_auth("About to send:", request);
94 if (!iq.writeToSocket((char *)request.c_str(), request.length(), 0, 5000)) {
95 DEBUG_auth("Error writing to ident connection to: ", clientip);
96 iq.close(); // close conection to client
97 return -1;
98 }
99 DEBUG_auth("wrote ident request to:", clientip);
100
101 char buff[8192];
102 try {
103 iq.getLine(buff, 8192, 5000);
104 } catch (std::exception &e) {
105 return -2;
106 }
107 String temp;
108 temp = buff; // convert to String
109 DEBUG_auth("got ident reply: ", temp, " from: ", clientip);
110
111 iq.close(); // close conection to client
112 temp = temp.after(":");
113 if (!temp.before(":").contains("USERID")) {
114 return -3;
115 }
116 temp = temp.after(":");
117 temp = temp.after(":");
118 temp.removeWhiteSpace();
119 if (temp.length() > 0) {
120 string = temp.toCharArray();
121 authrec.user_name = string;
122 authrec.user_source = "ident";
123 is_real_user = true;
124 return E2AUTH_OK;
125 }
126 return E2AUTH_NOMATCH;
127 }
128
129 int identinstance::init(void *args)
130 {
131 AuthPlugin::init(args);
132 StoryBoardOptions::SB_entry_map sen;
133 sen.entry_function = cv["story_function"];
134 if (sen.entry_function.length() > 0) {
135 sen.entry_id = ENT_STORYA_AUTH_IDENT;
136 story_entry = sen.entry_id;
137 o.story.auth_entry_dq.push_back(sen);
138 return 0;
139 } else {
140 E2LOGGER_error("No story_function defined in ident auth plugin config");
141 return -1;
142 }
143 }