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