Author: Bleep <tomh@inxpress.net>
[ircu2.10.12-pk.git] / ircd / m_oper.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_oper.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 1, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $Id$
24  */
25
26 /*
27  * m_functions execute protocol messages on this server:
28  *
29  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
30  *            structure (with an open socket connected!). This
31  *            identifies the physical socket where the message
32  *            originated (or which caused the m_function to be
33  *            executed--some m_functions may call others...).
34  *
35  *    sptr    is the source of the message, defined by the
36  *            prefix part of the message if present. If not
37  *            or prefix not found, then sptr==cptr.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
63  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64  *            *cannot* be a local connection, unless it's
65  *            actually cptr!). [MyConnect(x) should probably
66  *            be defined as (x == x->from) --msa ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
73  *                    parv[0], sender (prefix string), if not present
74  *                            this points to an empty string.
75  *                    parv[1]...parv[parc-1]
76  *                            pointers to additional parameters
77  *                    parv[parc] == NULL, *always*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #if 0
83 /*
84  * No need to include handlers.h here the signatures must match
85  * and we don't need to force a rebuild of all the handlers everytime
86  * we add a new one to the list. --Bleep
87  */
88 #include "handlers.h"
89 #endif /* 0 */
90 #include "client.h"
91 #include "hash.h"
92 #include "ircd.h"
93 #include "ircd_log.h"
94 #include "ircd_reply.h"
95 #include "ircd_string.h"
96 #include "ircd_xopen.h"
97 #include "msg.h"
98 #include "numeric.h"
99 #include "numnicks.h"
100 #include "querycmds.h"
101 #include "s_conf.h"
102 #include "s_debug.h"
103 #include "s_user.h"
104 #include "send.h"
105
106 #include <assert.h>
107 #include <stdlib.h>
108 #include <string.h>
109
110 #ifdef CRYPT_OPER_PASSWORD
111 int oper_password_match(const char* to_match, const char* passwd)
112 {
113   /*
114    * use first two chars of the password they send in as salt
115    *
116    * passwd may be NULL. Head it off at the pass...
117    */
118   if (to_match && passwd) {
119     char salt[3];
120     const char* encr;
121     salt[0] = passwd[0];
122     salt[1] = passwd[1];
123     salt[2] = '\0';
124     encr = ircd_crypt(to_match, salt);
125     return (0 == strcmp(encr, passwd));
126   }
127   return 0;
128 }
129 #else
130 int oper_password_match(const char* to_match, const char* passwd)
131 {
132   return (to_match && passwd) ? (0 == strcmp(to_match, passwd)) : 0;
133 }
134 #endif
135
136 /*
137  * m_oper - generic message handler
138  */
139 int m_oper(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
140 {
141   struct ConfItem* aconf;
142   char*            name;
143   char*            password;
144
145   assert(0 != cptr);
146   assert(cptr == sptr);
147
148   name     = parc > 1 ? parv[1] : 0;
149   password = parc > 2 ? parv[2] : 0;
150
151   if (EmptyString(name) || EmptyString(password))
152     return need_more_params(sptr, "OPER");
153
154   aconf = find_conf_exact(name, sptr->username, sptr->sockhost, CONF_OPS);
155   if (!aconf) 
156     aconf = find_conf_exact(name, sptr->username,
157                             ircd_ntoa((const char*) &cptr->ip), CONF_OPS);
158
159   if (!aconf || IsIllegal(aconf)) {
160     send_error_to_client(sptr, ERR_NOOPERHOST);
161     sendto_realops("Failed OPER attempt by %s (%s@%s)",
162                    parv[0], sptr->user->username, sptr->sockhost);
163     return 0;
164   }
165   assert(0 != (aconf->status & CONF_OPS));
166
167   if (oper_password_match(password, aconf->passwd)) {
168     unsigned int old_mode = (sptr->flags & ALL_UMODES);
169
170     if (ACR_OK != attach_conf(sptr, aconf)) {
171       send_error_to_client(sptr, ERR_NOOPERHOST);
172       sendto_realops("Failed OPER attempt by %s (%s@%s)",
173                      parv[0], sptr->user->username, sptr->sockhost);
174       return 0;
175     }
176     if (CONF_LOCOP == aconf->status) {
177       ClearOper(sptr);
178       SetLocOp(sptr);
179     }
180     else {
181       /*
182        * prevent someone from being both oper and local oper
183        */
184       ClearLocOp(sptr);
185       SetOper(sptr);
186       ++UserStats.opers;
187     }
188     cptr->handler = OPER_HANDLER;
189
190     
191     sptr->flags |= (FLAGS_WALLOP | FLAGS_SERVNOTICE | FLAGS_DEBUG);
192
193     set_snomask(sptr, SNO_OPERDEFAULT, SNO_ADD);
194     send_umode_out(cptr, sptr, old_mode);
195     sendto_one(sptr, rpl_str(RPL_YOUREOPER), me.name, parv[0]);
196
197     sendto_ops("%s (%s@%s) is now operator (%c)", parv[0],
198                sptr->user->username, sptr->sockhost,
199                IsOper(sptr) ? 'O' : 'o');
200
201     ircd_log(L_INFO, "OPER (%s) by (%s!%s@%s)",
202              name, parv[0], sptr->user->username, sptr->sockhost);
203 #ifdef FNAME_OPERLOG
204     if (IsUser(sptr))
205       write_log(FNAME_OPERLOG,
206                 "%s OPER (%s) by (%s!%s@%s)\n", myctime(CurrentTime),
207                 name, parv[0], sptr->user->username, sptr->sockhost);
208 #endif
209   }
210   else {
211     sendto_one(sptr, err_str(ERR_PASSWDMISMATCH), me.name, parv[0]);
212     sendto_realops("Failed OPER attempt by %s (%s@%s)",
213                    parv[0], sptr->user->username, sptr->sockhost);
214   }
215   return 0;
216 }
217
218 /*
219  * ms_oper - server message handler
220  */
221 int ms_oper(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
222 {
223   assert(0 != cptr);
224   assert(IsServer(cptr));
225   /*
226    * if message arrived from server, trust it, and set to oper
227    */
228   if (!IsServer(sptr) && !IsOper(sptr)) {
229     ++UserStats.opers;
230     sptr->flags |= FLAGS_OPER;
231     sendto_serv_butone(cptr, "%s%s " TOK_MODE " %s :+o", NumNick(sptr), parv[0]);
232   }
233   return 0;
234 }
235
236 /*
237  * mo_oper - oper message handler
238  */
239 int mo_oper(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
240 {
241   assert(0 != cptr);
242   assert(cptr == sptr);
243   sendto_one(sptr, rpl_str(RPL_YOUREOPER), me.name, parv[0]);
244   return 0;
245 }
246  
247 #if 0
248 /*
249  *  m_oper
250  *    parv[0] = sender prefix
251  *    parv[1] = oper name
252  *    parv[2] = oper password
253  */
254 int m_oper(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
255 {
256   struct ConfItem* aconf;
257   char*            name;
258   char*            password;
259   const char*      encr;
260 #ifdef CRYPT_OPER_PASSWORD
261   char             salt[3];
262 #endif /* CRYPT_OPER_PASSWORD */
263
264   name = parc > 1 ? parv[1] : 0;
265   password = parc > 2 ? parv[2] : 0;
266
267   if (!IsServer(cptr) && (EmptyString(name) || EmptyString(password)))
268     return need_more_params(sptr, "OPER");
269
270   /* if message arrived from server, trust it, and set to oper */
271
272   if (IsServer(cptr) && !IsOper(sptr)) {
273     ++UserStats.opers;
274     sptr->flags |= FLAGS_OPER;
275     sendto_serv_butone(cptr, "%s%s " TOK_MODE " %s :+o", NumNick(sptr), parv[0]);
276     return 0;
277   }
278   else if (IsAnOper(sptr)) {
279     if (MyConnect(sptr))
280       sendto_one(sptr, rpl_str(RPL_YOUREOPER), me.name, parv[0]);
281     return 0;
282   }
283   assert(cptr == sptr);
284   aconf = find_conf_exact(name, sptr->username, sptr->sockhost, CONF_OPS);
285   if (!aconf) 
286     aconf = find_conf_exact(name, sptr->username,
287                             ircd_ntoa((const char*) &cptr->ip), CONF_OPS);
288
289   if (!aconf || IsIllegal(aconf)) {
290     sendto_one(sptr, err_str(ERR_NOOPERHOST), me.name, parv[0]);
291     sendto_realops("Failed OPER attempt by %s (%s@%s)",
292                    parv[0], sptr->user->username, sptr->sockhost);
293     return 0;
294   }
295   assert(0 != (aconf->status & CONF_OPS));
296
297 #ifdef CRYPT_OPER_PASSWORD
298   /* use first two chars of the password they send in as salt */
299
300   /* passwd may be NULL. Head it off at the pass... */
301   salt[0] = '\0';
302   if (password && aconf->passwd)
303   {
304     salt[0] = aconf->passwd[0];
305     salt[1] = aconf->passwd[1];
306     salt[2] = '\0';
307     encr = ircd_crypt(password, salt);
308   }
309   else
310     encr = "";
311 #else
312   encr = password;
313 #endif /* CRYPT_OPER_PASSWORD */
314
315   if (0 == strcmp(encr, aconf->passwd)) {
316     int old = (sptr->flags & ALL_UMODES);
317
318     if (ACR_OK != attach_conf(sptr, aconf)) {
319       sendto_one(sptr, err_str(ERR_NOOPERHOST), me.name, parv[0]);
320       sendto_realops("Failed OPER attempt by %s (%s@%s)",
321                      parv[0], sptr->user->username, sptr->sockhost);
322       return 0;
323     }
324 #ifdef        OPER_REMOTE
325     if (aconf->status == CONF_LOCOP) {
326 #else
327     if (!IsLocal(sptr)) || aconf->status == CONF_LOCOP) {
328 #endif
329       ClearOper(sptr);
330       SetLocOp(sptr);
331     }
332     else {
333       /* prevent someone from being both oper and local oper */
334       ClearLocOp(sptr);
335       SetOper(sptr);
336       ++UserStats.opers;
337     }
338     cptr->handler = OPER_HANDLER;
339     sendto_ops("%s (%s@%s) is now operator (%c)", parv[0],
340         sptr->user->username, sptr->sockhost, IsOper(sptr) ? 'O' : 'o');
341
342     sptr->flags |= (FLAGS_WALLOP | FLAGS_SERVNOTICE | FLAGS_DEBUG);
343     set_snomask(sptr, SNO_OPERDEFAULT, SNO_ADD);
344     send_umode_out(cptr, sptr, old);
345     sendto_one(sptr, rpl_str(RPL_YOUREOPER), me.name, parv[0]);
346
347     ircd_log(L_INFO, "OPER (%s) by (%s!%s@%s)",
348              name, parv[0], sptr->user->username, sptr->sockhost);
349 #ifdef FNAME_OPERLOG
350     if (IsUser(sptr))
351       write_log(FNAME_OPERLOG,
352           "%s OPER (%s) by (%s!%s@%s)\n", myctime(CurrentTime),
353           name, parv[0], sptr->user->username, sptr->sockhost);
354 #endif
355   }
356   else {
357     sendto_one(sptr, err_str(ERR_PASSWDMISMATCH), me.name, parv[0]);
358     sendto_realops("Failed OPER attempt by %s (%s@%s)",
359                    parv[0], sptr->user->username, sptr->sockhost);
360   }
361   return 0;
362 }
363 #endif /* 0 */