Clean.
[seven-1.x.git] / modules / m_oper.c
blobfa39cc4d4315d4a69b7567cf05e6556405e3498a
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_oper.c: Makes a user an IRC Operator.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
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 2 of the License, or
12 * (at your option) any later version.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
24 * $Id: m_oper.c 148 2006-11-13 13:08:18Z spb $
27 #include "stdinc.h"
28 #include "tools.h"
29 #include "client.h"
30 #include "common.h"
31 #include "irc_string.h"
32 #include "ircd.h"
33 #include "numeric.h"
34 #include "commio.h"
35 #include "s_conf.h"
36 #include "s_newconf.h"
37 #include "s_log.h"
38 #include "s_user.h"
39 #include "send.h"
40 #include "msg.h"
41 #include "parse.h"
42 #include "modules.h"
43 #include "packet.h"
44 #include "cache.h"
45 #include "hash.h"
47 static int m_oper(struct Client *, struct Client *, int, const char **);
48 static int me_oper(struct Client *, struct Client *, int, const char **);
50 struct Message oper_msgtab = {
51 "OPER", 0, 0, 0, MFLG_SLOW,
52 {mg_unreg, {m_oper, 3}, mg_ignore, mg_ignore, {me_oper, 2}, {m_oper, 3}}
55 mapi_clist_av1 oper_clist[] = { &oper_msgtab, NULL };
56 DECLARE_MODULE_AV1(oper, NULL, NULL, oper_clist, NULL, NULL, "$Revision: 148 $");
58 static int match_oper_password(const char *password, struct oper_conf *oper_p);
59 extern char *crypt();
62 * m_oper
63 * parv[0] = sender prefix
64 * parv[1] = oper name
65 * parv[2] = oper password
67 static int
68 m_oper(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
70 struct oper_conf *oper_p;
71 const char *name;
72 const char *password;
74 name = parv[1];
75 password = parv[2];
77 if(IsOper(source_p))
79 sendto_one(source_p, form_str(RPL_YOUREOPER), me.name, source_p->name);
80 send_oper_motd(source_p);
81 return 0;
84 /* end the grace period */
85 if(!IsFloodDone(source_p))
86 flood_endgrace(source_p);
88 oper_p = find_oper_conf(source_p->username, source_p->orighost,
89 source_p->sockhost, name);
91 if(oper_p == NULL)
93 sendto_one(source_p, form_str(ERR_NOOPERHOST), me.name, source_p->name);
94 ilog(L_FOPER, "FAILED OPER (%s) by (%s!%s@%s) (%s)",
95 name, source_p->name,
96 source_p->username, source_p->host, source_p->sockhost);
98 if(ConfigFileEntry.failed_oper_notice)
100 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
101 "Failed OPER attempt - host mismatch by %s (%s@%s)",
102 source_p->name, source_p->username, source_p->host);
105 return 0;
108 if(match_oper_password(password, oper_p))
110 oper_up(source_p, oper_p);
112 ilog(L_OPERED, "OPER %s by %s!%s@%s (%s)",
113 name, source_p->name, source_p->username, source_p->host,
114 source_p->sockhost);
115 return 0;
117 else
119 sendto_one(source_p, form_str(ERR_PASSWDMISMATCH),
120 me.name, source_p->name);
122 ilog(L_FOPER, "FAILED OPER (%s) by (%s!%s@%s) (%s)",
123 name, source_p->name, source_p->username, source_p->host,
124 source_p->sockhost);
126 if(ConfigFileEntry.failed_oper_notice)
128 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
129 "Failed OPER attempt by %s (%s@%s)",
130 source_p->name, source_p->username, source_p->host);
134 return 0;
138 * me_oper()
140 * parv[0] = user
141 * parv[1] = operflags
143 * XXX - we could probably optimize this somehow --nenolod
145 static int
146 me_oper(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
148 struct Client *target_p;
149 const char *p;
151 if ((target_p = find_client(parv[0])) == NULL)
152 return -1;
154 /* make sure we start out with a clean slate */
155 target_p->operflags = 0;
157 for (p = parv[1]; *p != '\0'; p++)
159 int i;
161 for (i = 0; oper_flagtable[i].flag; i++)
163 if (*p == oper_flagtable[i].has)
164 target_p->operflags |= oper_flagtable[i].flag;
165 else if (*p == oper_flagtable[i].hasnt)
166 target_p->operflags &= ~oper_flagtable[i].flag;
170 return 0;
174 * match_oper_password
176 * inputs - pointer to given password
177 * - pointer to Conf
178 * output - YES or NO if match
179 * side effects - none
181 static int
182 match_oper_password(const char *password, struct oper_conf *oper_p)
184 const char *encr;
186 /* passwd may be NULL pointer. Head it off at the pass... */
187 if(EmptyString(oper_p->passwd))
188 return NO;
190 if(IsOperConfEncrypted(oper_p))
192 /* use first two chars of the password they send in as salt */
193 /* If the password in the conf is MD5, and ircd is linked
194 * to scrypt on FreeBSD, or the standard crypt library on
195 * glibc Linux, then this code will work fine on generating
196 * the proper encrypted hash for comparison.
198 if(!EmptyString(password))
199 encr = crypt(password, oper_p->passwd);
200 else
201 encr = "";
203 else
204 encr = password;
206 if(strcmp(encr, oper_p->passwd) == 0)
207 return YES;
208 else
209 return NO;