]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqlauth.cpp
71eb2b9bccda9ee5f38022761f753c9b42f7cc6b
[user/henk/code/inspircd.git] / src / modules / extra / m_sqlauth.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include <stdio.h>
18 #include <string>
19 #include <stdlib.h>
20 #include <time.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <sys/time.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <poll.h>
29 #include "users.h"
30 #include "channels.h"
31 #include "modules.h"
32 #include "inspircd.h"
33 #include "m_sql.h"
34
35 /* $ModDesc: An SQL test module */
36
37 Server *Srv;
38
39 class ModuleSQLAuth : public Module
40 {
41         ConfigReader* Conf;
42         std::string usertable;
43         unsigned long dbid;
44         Module* SQLModule;
45
46  public:
47         bool ReadConfig()
48         {
49                 Conf = new ConfigReader();
50                 usertable = Conf->ReadValue("sqlauth","usertable",0);
51                 dbid = Conf->ReadInteger("sqlauth","dbid",0,true);
52                 delete Conf;
53                 SQLModule = Srv->FindModule("m_sql.so");
54                 return (SQLModule);
55         }
56
57         ModuleSQLAuth()
58         {
59                 Srv = new Server;
60                 ReadConfig();
61         }
62
63         virtual void OnRehash()
64         {
65                 ReadConfig();
66         }
67
68         bool CheckCredentials(std::string username, std::string password,std::string usertable)
69         {
70                 bool found = false;
71
72                 // is the sql module loaded? If not, we don't attempt to do anything.
73                 if (!SQLModule)
74                         return false;
75
76                 // Create a request containing the SQL query and send it to m_sql.so
77                 SQLRequest* query = new SQLRequest(SQL_RESULT,1,"SELECT * FROM "+usertable+" WHERE user='"+username+"' AND pass=md5('"+password+"')");
78                 Request queryrequest((char*)query, this, SQLModule);
79                 SQLResult* result = (SQLResult*)queryrequest.Send();
80
81                 // Did we get "OK" as a result?
82                 if (result->GetType() == SQL_OK)
83                 {
84
85                         // if we did, this means we may now request a row... there should be only one row for each user, so,
86                         // we don't need to loop to fetch multiple rows.
87                         SQLRequest* rowrequest = new SQLRequest(SQL_ROW,1,"");
88                         Request rowquery((char*)rowrequest, this, SQLModule);
89                         SQLResult* rowresult = (SQLResult*)rowquery.Send();
90
91                         // did we get a row? If we did, we can now do something with the fields
92                         if (rowresult->GetType() == SQL_ROW)
93                         {
94                                 Srv->Log(DEBUG,"*********** SQL TEST MODULE - RESULTS *************");
95                                 Srv->Log(DEBUG,"Result, field 'qcount': '" + rowrequest->GetField("qcount"));
96                                 Srv->Log(DEBUG,"Result, field 'asked': '" + rowrequest->GetField("asked"));
97                                 found = true;
98                                 delete rowresult;
99                         }
100                         else
101                         {
102                                 // we didn't have a row.
103                                 found = false;
104                         }
105                         delete rowrequest;
106                         delete result;
107                 }
108                 else
109                 {
110                         // the query was bad
111                         found = false;
112                 }
113                 query->SetQueryType(SQL_DONE);
114                 query->SetConnID(1);
115                 Request donerequest((char*)query, this, SQLModule);
116                 donerequest.Send();
117                 delete query;
118                 return found;
119         }
120
121         virtual bool OnCheckReady(userrec* user)
122         {
123         }
124
125         virtual void OnUserDisconnect(userrec* user)
126         {
127         }
128         
129         virtual ~ModuleSQLAuth()
130         {
131                 delete Srv;
132         }
133         
134         virtual Version GetVersion()
135         {
136                 return Version(1,0,0,1,VF_VENDOR);
137         }
138         
139 };
140
141 class ModuleSQLAuthFactory : public ModuleFactory
142 {
143  public:
144         ModuleSQLAuthFactory()
145         {
146         }
147         
148         ~ModuleSQLAuthFactory()
149         {
150         }
151         
152         virtual Module * CreateModule()
153         {
154                 return new ModuleSQLAuth;
155         }
156         
157 };
158
159
160 extern "C" void * init_module( void )
161 {
162         return new ModuleSQLAuthFactory;
163 }
164