]> jfr.im git - irc/rqf/shadowircd.git/blame - extensions/m_mkpasswd.c
[svn] Backport from early 3.x:
[irc/rqf/shadowircd.git] / extensions / m_mkpasswd.c
CommitLineData
212380e3 1/*
2 * m_mkpasswd.c: Encrypts a password online, DES or MD5.
3 *
4 * Copyright 2002 W. Campbell and the ircd-ratbox development team
5 * Based on mkpasswd.c, originally by Nelson Minar (minar@reed.edu)
6 *
7 * You can use this code in any way as long as these names remain.
8 *
5366977b 9 * $Id: m_mkpasswd.c 3161 2007-01-25 07:23:01Z nenolod $
212380e3 10 */
11
12/* List of ircd includes from ../include/ */
13#include "stdinc.h"
14#include "client.h"
15#include "common.h" /* FALSE bleah */
16#include "ircd.h"
17#include "irc_string.h"
18#include "numeric.h"
19#include "patricia.h"
20#include "s_newconf.h"
21#include "s_conf.h"
22#include "s_log.h"
23#include "s_serv.h"
24#include "send.h"
25#include "msg.h"
26#include "parse.h"
27#include "modules.h"
28
29#include <string.h>
30
31extern char *crypt();
32
33static int m_mkpasswd(struct Client *client_p, struct Client *source_p,
34 int parc, const char *parv[]);
35static int mo_mkpasswd(struct Client *client_p, struct Client *source_p,
36 int parc, const char *parv[]);
37static char *make_salt(void);
38static char *make_md5_salt(void);
39
40static char saltChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
41
42
43struct Message mkpasswd_msgtab = {
44 "MKPASSWD", 0, 0, 0, MFLG_SLOW,
45 {mg_unreg, {m_mkpasswd, 2}, mg_ignore, mg_ignore, mg_ignore, {mo_mkpasswd, 2}}
46};
47
48mapi_clist_av1 mkpasswd_clist[] = { &mkpasswd_msgtab, NULL };
49
5366977b 50DECLARE_MODULE_AV1(mkpasswd, NULL, NULL, mkpasswd_clist, NULL, NULL, "$Revision: 3161 $");
212380e3 51
52
53static int
54m_mkpasswd(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
55{
56 static time_t last_used = 0;
57 int is_md5 = 0;
58
59 if((last_used + ConfigFileEntry.pace_wait) > CurrentTime)
60 {
61 /* safe enough to give this on a local connect only */
62 sendto_one(source_p, form_str(RPL_LOAD2HI), me.name, parv[0]);
63 return 0;
64 }
65 else
66 {
67 last_used = CurrentTime;
68 }
69
70 if(parc == 3)
71 {
72 if(!irccmp(parv[2], "MD5"))
73 {
74 is_md5 = 1;
75 }
76 else if(!irccmp(parv[2], "DES"))
77 {
78 /* Not really needed, but we may want to have a default encryption
79 * setting somewhere down the road
80 */
81 is_md5 = 0;
82 }
83 else
84 {
5366977b 85 sendto_one_notice(source_p, ":MKPASSWD syntax error: MKPASSWD pass [DES|MD5]");
212380e3 86 return 0;
87 }
88 }
89
90 if(parc == 1)
91 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS), me.name, parv[0], "MKPASSWD");
92 else
5366977b 93 sendto_one_notice(source_p, ":Encryption for [%s]: %s",
94 parv[1], crypt(parv[1],
212380e3 95 is_md5 ? make_md5_salt() :
96 make_salt()));
97
98 return 0;
99}
100
101/*
102** mo_test
103** parv[0] = sender prefix
104** parv[1] = parameter
105*/
106static int
107mo_mkpasswd(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
108{
109 int is_md5 = 0;
110
111 if(parc == 3)
112 {
113 if(!irccmp(parv[2], "MD5"))
114 {
115 is_md5 = 1;
116 }
117 else if(!irccmp(parv[2], "DES"))
118 {
119 /* Not really needed, but we may want to have a default encryption
120 * setting somewhere down the road
121 */
122 is_md5 = 0;
123 }
124 else
125 {
5366977b 126 sendto_one_notice(source_p,
127 ":MKPASSWD syntax error: MKPASSWD pass [DES|MD5]");
212380e3 128 return 0;
129 }
130 }
131
132 if(parc == 1)
133 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS), me.name, parv[0], "MKPASSWD");
134 else
5366977b 135 sendto_one_notice(source_p, ":Encryption for [%s]: %s",
136 parv[1], crypt(parv[1], is_md5 ? make_md5_salt() : make_salt()));
212380e3 137
138 return 0;
139}
140
141static char *
142make_salt(void)
143{
144 static char salt[3];
145 salt[0] = saltChars[random() % 64];
146 salt[1] = saltChars[random() % 64];
147 salt[2] = '\0';
148 return salt;
149}
150
151static char *
152make_md5_salt(void)
153{
154 static char salt[13];
155 int i;
156 salt[0] = '$';
157 salt[1] = '1';
158 salt[2] = '$';
159 for(i = 3; i < 11; i++)
160 salt[i] = saltChars[random() % 64];
161 salt[11] = '$';
162 salt[12] = '\0';
163 return salt;
164}