| 1 |
/* |
| 2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
| 3 |
* m_challenge.c: Allows an IRC Operator to securely authenticate. |
| 4 |
* |
| 5 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 20 |
* USA |
| 21 |
* |
| 22 |
* $Id$ |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "stdinc.h" |
| 26 |
#include "conf/conf.h" |
| 27 |
#include "handlers.h" |
| 28 |
#include "client.h" |
| 29 |
#include "ircd.h" |
| 30 |
#include "numeric.h" |
| 31 |
#include "send.h" |
| 32 |
#include "motd.h" |
| 33 |
/* -lcrypto is implicit for building this module! */ |
| 34 |
#include "rsa.h" |
| 35 |
#include "msg.h" |
| 36 |
#include "parse.h" |
| 37 |
#include "user.h" |
| 38 |
|
| 39 |
static void failed_challenge_notice(struct Client *, const char *, |
| 40 |
const char *); |
| 41 |
static void m_challenge(struct Client *, struct Client *, int, char *[]); |
| 42 |
|
| 43 |
/* We have openssl support, so include /CHALLENGE */ |
| 44 |
struct Message challenge_msgtab = { |
| 45 |
"CHALLENGE", 0, 0, 2, 0, MFLG_SLOW, 0, |
| 46 |
{ m_unregistered, m_challenge, m_ignore, m_ignore, m_challenge, m_ignore } |
| 47 |
}; |
| 48 |
|
| 49 |
INIT_MODULE(m_challenge, "$Revision$") |
| 50 |
{ |
| 51 |
mod_add_cmd(&challenge_msgtab); |
| 52 |
} |
| 53 |
|
| 54 |
CLEANUP_MODULE |
| 55 |
{ |
| 56 |
mod_del_cmd(&challenge_msgtab); |
| 57 |
} |
| 58 |
|
| 59 |
/*! \brief CHALLENGE command handler (called for local clients only) |
| 60 |
* |
| 61 |
* Generate RSA challenge for wouldbe oper |
| 62 |
* |
| 63 |
* \param client_p Pointer to allocated Client struct with physical connection |
| 64 |
* to this server, i.e. with an open socket connected. |
| 65 |
* \param source_p Pointer to allocated Client struct from which the message |
| 66 |
* originally comes from. This can be a local or remote client. |
| 67 |
* \param parc Integer holding the number of supplied arguments. |
| 68 |
* \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL |
| 69 |
* pointers. |
| 70 |
* \note Valid arguments for this command are: |
| 71 |
* - parv[0] = sender prefix |
| 72 |
* - parv[1] = operator to challenge for, or +response |
| 73 |
*/ |
| 74 |
static void |
| 75 |
m_challenge(struct Client *client_p, struct Client *source_p, |
| 76 |
int parc, char *parv[]) |
| 77 |
{ |
| 78 |
char *challenge = NULL; |
| 79 |
struct OperatorConf *conf = NULL; |
| 80 |
|
| 81 |
assert(source_p->localClient); |
| 82 |
|
| 83 |
/* if theyre an oper, reprint oper motd and ignore */ |
| 84 |
if (IsOper(source_p)) |
| 85 |
{ |
| 86 |
sendto_one(source_p, form_str(RPL_YOUREOPER), me.name, source_p->name); |
| 87 |
send_message_file(source_p, &opermotd); |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
if (*parv[1] == '+') |
| 92 |
{ |
| 93 |
/* Ignore it if we aren't expecting this... -A1kmm */ |
| 94 |
if (source_p->localClient->response == NULL) |
| 95 |
return; |
| 96 |
|
| 97 |
if (irccmp(source_p->localClient->response, ++parv[1])) |
| 98 |
{ |
| 99 |
sendto_one(source_p, form_str(ERR_PASSWDMISMATCH), me.name, |
| 100 |
source_p->name); |
| 101 |
failed_challenge_notice(source_p, source_p->localClient->auth_oper, |
| 102 |
"challenge failed"); |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
if ((conf = find_operconf(source_p->localClient->auth_oper, |
| 107 |
source_p->username, source_p->host, |
| 108 |
&source_p->localClient->ip)) == NULL) |
| 109 |
{ |
| 110 |
sendto_one(source_p, form_str(ERR_NOOPERHOST), me.name, source_p->name); |
| 111 |
log_oper_action(LOG_FAILED_OPER_TYPE, source_p, "%s\n", |
| 112 |
source_p->localClient->auth_oper); |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
oper_up(source_p, conf); |
| 117 |
|
| 118 |
ilog(L_TRACE, "OPER %s by %s!%s@%s", |
| 119 |
source_p->localClient->auth_oper, source_p->name, source_p->username, |
| 120 |
source_p->host); |
| 121 |
log_oper_action(LOG_OPER_TYPE, source_p, |
| 122 |
"%s\n", source_p->localClient->auth_oper); |
| 123 |
|
| 124 |
MyFree(source_p->localClient->response); |
| 125 |
/* |
| 126 |
* Do NOT free auth_oper here as it is already done and |
| 127 |
* a new string is copied to it in oper_up() |
| 128 |
*/ |
| 129 |
// MyFree(source_p->localClient->auth_oper); |
| 130 |
source_p->localClient->response = NULL; |
| 131 |
// source_p->localClient->auth_oper = NULL; |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
MyFree(source_p->localClient->response); |
| 136 |
MyFree(source_p->localClient->auth_oper); |
| 137 |
source_p->localClient->response = NULL; |
| 138 |
source_p->localClient->auth_oper = NULL; |
| 139 |
|
| 140 |
conf = find_operconf(parv[1], source_p->username, source_p->host, |
| 141 |
&source_p->localClient->ip); |
| 142 |
if (conf == NULL) |
| 143 |
{ |
| 144 |
sendto_one(source_p, form_str(ERR_NOOPERHOST), me.name, source_p->name); |
| 145 |
conf = find_operconf(parv[1], NULL, NULL, NULL); |
| 146 |
failed_challenge_notice(source_p, parv[1], (conf != NULL) |
| 147 |
? "host mismatch" : "no oper {} block"); |
| 148 |
log_oper_action(LOG_FAILED_OPER_TYPE, source_p, "%s\n", parv[1]); |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
if (conf->rsa_public_key == NULL) |
| 153 |
{ |
| 154 |
sendto_one(source_p, ":%s NOTICE %s :I'm sorry, PK authentication " |
| 155 |
"is not enabled for your oper{} block.", me.name, |
| 156 |
source_p->name); |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
if (!generate_challenge(&challenge, &(source_p->localClient->response), |
| 161 |
conf->rsa_public_key)) |
| 162 |
sendto_one(source_p, form_str(RPL_RSACHALLENGE), |
| 163 |
me.name, source_p->name, challenge); |
| 164 |
|
| 165 |
DupString(source_p->localClient->auth_oper, conf->name); |
| 166 |
MyFree(challenge); |
| 167 |
} |
| 168 |
|
| 169 |
/* failed_challenge_notice() |
| 170 |
* |
| 171 |
* inputs - pointer to client doing /oper ... |
| 172 |
* - pointer to nick they tried to oper as |
| 173 |
* - pointer to reason they have failed |
| 174 |
* output - nothing |
| 175 |
* side effects - notices all opers of the failed oper attempt if enabled |
| 176 |
*/ |
| 177 |
static void |
| 178 |
failed_challenge_notice(struct Client *source_p, const char *name, |
| 179 |
const char *reason) |
| 180 |
{ |
| 181 |
if (General.failed_oper_notice) |
| 182 |
sendto_realops_flags(UMODE_ALL, L_ALL, "Failed CHALLENGE attempt as %s " |
| 183 |
"by %s (%s@%s) - %s", name, source_p->name, |
| 184 |
source_p->username, source_p->host, reason); |
| 185 |
} |