]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ssl_data.cpp
Migrate SSL metadata and visible information (/whois line) to single module
[user/henk/code/inspircd.git] / src / modules / m_ssl_data.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "transport.h"
16
17 /* $ModDesc: Provides SSL metadata and /WHOIS information */
18 class ModuleSSLData : public Module
19 {
20  public:
21         ModuleSSLData(InspIRCd* Me) : Module(Me)
22         {
23                 Implementation eventlist[] = { I_OnSyncUserMetaData, I_OnDecodeMetaData, I_OnWhois };
24                 ServerInstance->Modules->Attach(eventlist, this, 3);
25         }
26
27         virtual Version GetVersion()
28         {
29                 return Version("$Id$", VF_VENDOR|VF_COMMON, API_VERSION);
30         }
31
32
33         // :kenny.chatspike.net 320 Om Epy|AFK :is a Secure Connection
34         virtual void OnWhois(User* source, User* dest)
35         {
36                 if(dest->GetExt("ssl"))
37                 {
38                         ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
39                 }
40         }
41
42         virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
43         {
44                 // check if the linking module wants to know about OUR metadata
45                 if (extname == "ssl")
46                 {
47                         // check if this user has an ssl field to send
48                         if (!user->GetExt(extname))
49                                 return;
50
51                         // call this function in the linking module, let it format the data how it
52                         // sees fit, and send it on its way. We dont need or want to know how.
53                         proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, displayable ? "Enabled" : "ON");
54                 }
55                 else if (extname == "ssl_cert")
56                 {
57                         ssl_cert* cert;
58                         if (!user->GetExt("ssl_cert", cert))
59                                 return;
60
61                         std::stringstream value;
62                         bool hasError = cert->GetError().length();
63                         value << (cert->IsInvalid() ? "v" : "V") << (cert->IsTrusted() ? "T" : "t") << (cert->IsRevoked() ? "R" : "r")
64                                 << (cert->IsUnknownSigner() ? "s" : "S") << (hasError ? "E" : "e") << " ";
65                         if (hasError)
66                                 value << cert->GetError();
67                         else
68                                 value << cert->GetFingerprint() << " " << cert->GetDN() << " " << cert->GetIssuer();
69
70                         proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, value.str().c_str());
71                 }
72         }
73
74         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
75         {
76                 // check if its our metadata key, and its associated with a user
77                 if ((target_type == TYPE_USER) && (extname == "ssl"))
78                 {
79                         User* dest = static_cast<User*>(target);
80                         // if they dont already have an ssl flag, accept the remote server's
81                         if (!dest->GetExt(extname))
82                         {
83                                 dest->Extend(extname);
84                         }
85                 }
86                 else if ((target_type == TYPE_USER) && (extname == "ssl_cert"))
87                 {
88                         User* dest = static_cast<User*>(target);
89                         if (dest->GetExt(extname))
90                                 return;
91
92                         ssl_cert* cert = new ssl_cert;
93                         dest->Extend(extname, cert);
94
95                         std::stringstream s(extdata);
96                         std::string v;
97                         getline(s,v,' ');
98
99                         cert->data.insert(std::make_pair("invalid", ConvToStr(v.find('v') != std::string::npos)));
100                         cert->data.insert(std::make_pair("trusted", ConvToStr(v.find('T') != std::string::npos)));
101                         cert->data.insert(std::make_pair("revoked", ConvToStr(v.find('R') != std::string::npos)));
102                         cert->data.insert(std::make_pair("unknownsigner", ConvToStr(v.find('s') != std::string::npos)));
103                         if (v.find('E') != std::string::npos)
104                         {
105                                 getline(s,v,'\n');
106                                 cert->data.insert(std::make_pair("error", v));
107                         }
108                         else
109                         {
110                                 getline(s,v,' ');
111                                 cert->data.insert(std::make_pair("fingerprint", v));
112
113                                 getline(s,v,' ');
114                                 cert->data.insert(std::make_pair("dn", v));
115
116                                 getline(s,v,'\n');
117                                 cert->data.insert(std::make_pair("issuer", v));
118                         }
119                 }
120         }
121 };
122
123 MODULE_INIT(ModuleSSLData)