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