]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/users.cpp
Allowed ulined servers to by pass all oper permissions checking
[user/henk/code/inspircd.git] / src / users.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 using namespace std;
18
19 #include "inspircd_config.h" 
20 #include "channels.h"
21 #include "connection.h"
22 #include "users.h"
23 #include "inspircd.h"
24 #include <stdio.h>
25 #ifdef THREADED_DNS
26 #include <pthread.h>
27 #include <signal.h>
28 #endif
29 #include "inspstring.h"
30 #include "helperfuncs.h"
31
32 extern std::stringstream config_f;
33 extern char ServerName[MAXBUF];
34
35 extern time_t TIME;
36
37 userrec::userrec()
38 {
39         // the PROPER way to do it, AVOID bzero at *ALL* costs
40         strcpy(nick,"");
41         strcpy(ip,"127.0.0.1");
42         timeout = 0;
43         strcpy(ident,"");
44         strcpy(host,"");
45         strcpy(dhost,"");
46         strcpy(fullname,"");
47         strcpy(modes,"");
48         server = (char*)FindServerNamePtr(ServerName);
49         strcpy(awaymsg,"");
50         strcpy(oper,"");
51         reset_due = TIME;
52         lines_in = 0;
53         fd = lastping = signon = idle_lastmsg = nping = registered = 0;
54         flood = port = bytes_in = bytes_out = cmds_in = cmds_out = 0;
55         haspassed = false;
56         dns_done = false;
57         recvq = "";
58         sendq = "";
59         for (int i = 0; i < MAXCHANS; i++)
60         {
61                 this->chans[i].channel = NULL;
62                 this->chans[i].uc_modes = 0;
63         }
64         invites.clear();
65 }
66
67 userrec::~userrec()
68 {
69 #ifdef THREADED_DNS
70         // for local clients, clean up their dns thread
71         if (!strcmp(this->server,ServerName))
72         {
73                 pthread_kill(this->dnsthread,9);
74         }
75 #endif
76 }
77
78 void userrec::CloseSocket()
79 {
80         shutdown(this->fd,2);
81         close(this->fd);
82 }
83  
84 char* userrec::GetFullHost()
85 {
86         static char result[MAXBUF];
87         snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,dhost);
88         return result;
89 }
90
91 int userrec::ReadData(void* buffer, size_t size)
92 {
93         if (this->fd > -1)
94         {
95                 log(DEBUG,"userrec::ReadData on fd %d",this->fd);
96                 return read(this->fd, buffer, size);
97         }
98         else return 0;
99 }
100
101
102 char* userrec::GetFullRealHost()
103 {
104         static char fresult[MAXBUF];
105         snprintf(fresult,MAXBUF,"%s!%s@%s",nick,ident,host);
106         return fresult;
107 }
108
109 bool userrec::IsInvited(char* channel)
110 {
111         for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
112         {
113                 if (i->channel) {
114                         if (!strcasecmp(i->channel,channel))
115                         {
116                                 return true;
117                         }
118                 }
119         }
120         return false;
121 }
122
123 InvitedList* userrec::GetInviteList()
124 {
125         return &invites;
126 }
127
128 void userrec::InviteTo(char* channel)
129 {
130         Invited i;
131         strlcpy(i.channel,channel,CHANMAX);
132         invites.push_back(i);
133 }
134
135 void userrec::RemoveInvite(char* channel)
136 {
137         log(DEBUG,"Removing invites");
138         if (channel)
139         {
140                 if (invites.size())
141                 {
142                         for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
143                         {
144                                 if (i->channel)
145                                 {
146                                         if (!strcasecmp(i->channel,channel))
147                                         {
148                                                 invites.erase(i);
149                                                 return;
150                                         }
151                                 }
152                         }
153                 }
154         }
155 }
156
157 bool userrec::HasPermission(char* command)
158 {
159         char TypeName[MAXBUF],Classes[MAXBUF],ClassName[MAXBUF],CommandList[MAXBUF];
160         char* mycmd;
161         char* savept;
162         char* savept2;
163         
164         // users on u-lined servers can completely bypass
165         // all permissions based checks.
166         //
167         // of course, if this is sent to a remote server and this
168         // server is not ulined there, then that other server
169         // silently drops the command.
170         if (is_uline(this->server))
171                 return true;
172         
173         // are they even an oper at all?
174         if (strchr(this->modes,'o'))
175         {
176                 log(DEBUG,"*** HasPermission: %s is an oper",this->nick);
177                 for (int j =0; j < ConfValueEnum("type",&config_f); j++)
178                 {
179                         ConfValue("type","name",j,TypeName,&config_f);
180                         if (!strcmp(TypeName,this->oper))
181                         {
182                                 log(DEBUG,"*** HasPermission: %s is an oper of type '%s'",this->nick,this->oper);
183                                 ConfValue("type","classes",j,Classes,&config_f);
184                                 char* myclass = strtok_r(Classes," ",&savept);
185                                 while (myclass)
186                                 {
187                                         log(DEBUG,"*** HasPermission: checking classtype '%s'",myclass);
188                                         for (int k =0; k < ConfValueEnum("class",&config_f); k++)
189                                         {
190                                                 ConfValue("class","name",k,ClassName,&config_f);
191                                                 if (!strcmp(ClassName,myclass))
192                                                 {
193                                                         ConfValue("class","commands",k,CommandList,&config_f);
194                                                         log(DEBUG,"*** HasPermission: found class named %s with commands: '%s'",ClassName,CommandList);
195                                                         
196                                                         
197                                                         mycmd = strtok_r(CommandList," ",&savept2);
198                                                         while (mycmd)
199                                                         {
200                                                                 if (!strcasecmp(mycmd,command))
201                                                                 {
202                                                                         log(DEBUG,"*** Command %s found, returning true",command);
203                                                                         return true;
204                                                                 }
205                                                                 mycmd = strtok_r(NULL," ",&savept2);
206                                                         }
207                                                 }
208                                         }
209                                         myclass = strtok_r(NULL," ",&savept);
210                                 }
211                         }
212                 }
213         }
214         return false;
215 }
216
217
218 bool userrec::AddBuffer(std::string a)
219 {
220         std::string b = "";
221         for (unsigned int i = 0; i < a.length(); i++)
222                 if ((a[i] != '\r') && (a[i] != '\0') && (a[i] != 7))
223                         b = b + a[i];
224         std::stringstream stream(recvq);
225         stream << b;
226         recvq = stream.str();
227         unsigned int i = 0;
228         // count the size of the first line in the buffer.
229         while (i < recvq.length())
230         {
231                 if (recvq[i++] == '\n')
232                         break;
233         }
234         if (recvq.length() > (unsigned)this->recvqmax)
235         {
236                 this->SetWriteError("RecvQ exceeded");
237                 WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
238         }
239         // return false if we've had more than 600 characters WITHOUT
240         // a carriage return (this is BAD, drop the socket)
241         return (i < 600);
242 }
243
244 bool userrec::BufferIsReady()
245 {
246         for (unsigned int i = 0; i < recvq.length(); i++)
247                 if (recvq[i] == '\n')
248                         return true;
249         return false;
250 }
251
252 void userrec::ClearBuffer()
253 {
254         recvq = "";
255 }
256
257 std::string userrec::GetBuffer()
258 {
259         if (recvq == "")
260                 return "";
261         char* line = (char*)recvq.c_str();
262         std::string ret = "";
263         while ((*line != '\n') && (strlen(line)))
264         {
265                 ret = ret + *line;
266                 line++;
267         }
268         if ((*line == '\n') || (*line == '\r'))
269                 line++;
270         recvq = line;
271         return ret;
272 }
273
274 void userrec::AddWriteBuf(std::string data)
275 {
276         if (this->GetWriteError() != "")
277                 return;
278         if (sendq.length() + data.length() > (unsigned)this->sendqmax)
279         {
280                 WriteOpers("*** User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->sendqmax);
281                 this->SetWriteError("SendQ exceeded");
282                 return;
283         }
284         std::stringstream stream;
285         stream << sendq << data;
286         sendq = stream.str();
287 }
288
289 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
290 void userrec::FlushWriteBuf()
291 {
292         if (sendq.length())
293         {
294                 char* tb = (char*)this->sendq.c_str();
295                 int n_sent = write(this->fd,tb,this->sendq.length());
296                 if (n_sent == -1)
297                 {
298                         this->SetWriteError(strerror(errno));
299                 }
300                 else
301                 {
302                         // advance the queue
303                         tb += n_sent;
304                         this->sendq = tb;
305                         // update the user's stats counters
306                         this->bytes_out += n_sent;
307                         this->cmds_out++;
308                 }
309         }
310 }
311
312 void userrec::SetWriteError(std::string error)
313 {
314         log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
315         // don't try to set the error twice, its already set take the first string.
316         if (this->WriteError == "")
317                 this->WriteError = error;
318 }
319
320 std::string userrec::GetWriteError()
321 {
322         return this->WriteError;
323 }