]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
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 AC |
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 | ||
31 | extern char *crypt(); | |
32 | ||
33 | static int m_mkpasswd(struct Client *client_p, struct Client *source_p, | |
34 | int parc, const char *parv[]); | |
35 | static int mo_mkpasswd(struct Client *client_p, struct Client *source_p, | |
36 | int parc, const char *parv[]); | |
37 | static char *make_salt(void); | |
38 | static char *make_md5_salt(void); | |
39 | ||
40 | static char saltChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./"; | |
41 | ||
42 | ||
43 | struct 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 | ||
48 | mapi_clist_av1 mkpasswd_clist[] = { &mkpasswd_msgtab, NULL }; | |
49 | ||
5366977b | 50 | DECLARE_MODULE_AV1(mkpasswd, NULL, NULL, mkpasswd_clist, NULL, NULL, "$Revision: 3161 $"); |
212380e3 AC |
51 | |
52 | ||
53 | static int | |
54 | m_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 AC |
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 AC |
93 | sendto_one_notice(source_p, ":Encryption for [%s]: %s", |
94 | parv[1], crypt(parv[1], | |
212380e3 AC |
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 | */ | |
106 | static int | |
107 | mo_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 AC |
126 | sendto_one_notice(source_p, |
127 | ":MKPASSWD syntax error: MKPASSWD pass [DES|MD5]"); | |
212380e3 AC |
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 AC |
135 | sendto_one_notice(source_p, ":Encryption for [%s]: %s", |
136 | parv[1], crypt(parv[1], is_md5 ? make_md5_salt() : make_salt())); | |
212380e3 AC |
137 | |
138 | return 0; | |
139 | } | |
140 | ||
141 | static char * | |
142 | make_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 | ||
151 | static char * | |
152 | make_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 | } |