2 * m_mkpasswd.c: Encrypts a password online.
4 * Based on mkpasswd.c, originally by Nelson Minar (minar@reed.edu)
5 * You can use this code in any way as long as these names remain.
18 static int m_mkpasswd(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
,
19 int parc
, const char *parv
[]);
20 static int mo_mkpasswd(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
,
21 int parc
, const char *parv
[]);
23 static char *make_md5_salt(int);
24 static char *make_sha256_salt(int);
25 static char *make_sha512_salt(int);
26 static char *generate_random_salt(char *, int);
27 static char *generate_poor_salt(char *, int);
29 static char saltChars
[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
30 /* 0 .. 63, ascii - 64 */
32 struct Message mkpasswd_msgtab
= {
33 "MKPASSWD", 0, 0, 0, 0,
34 {mg_unreg
, {m_mkpasswd
, 2}, mg_ignore
, mg_ignore
, mg_ignore
, {mo_mkpasswd
, 2}}
37 mapi_clist_av1 mkpasswd_clist
[] = { &mkpasswd_msgtab
, NULL
};
39 DECLARE_MODULE_AV1(mkpasswd
, NULL
, NULL
, mkpasswd_clist
, NULL
, NULL
, "$Revision$");
42 /* m_mkpasswd - mkpasswd message handler
47 m_mkpasswd(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
49 static time_t last_used
= 0;
53 const char hashdefault
[] = "SHA512";
55 if(EmptyString(parv
[1]))
57 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
), me
.name
, source_p
->name
, "MKPASSWD");
62 hashtype
= hashdefault
;
66 if((last_used
+ ConfigFileEntry
.pace_wait
) > rb_current_time())
68 /* safe enough to give this on a local connect only */
69 sendto_one(source_p
, form_str(RPL_LOAD2HI
), me
.name
, source_p
->name
, "MKPASSWD");
73 last_used
= rb_current_time();
75 if(!irccmp(hashtype
, "SHA256"))
76 salt
= make_sha256_salt(16);
77 else if(!irccmp(hashtype
, "SHA512"))
78 salt
= make_sha512_salt(16);
79 else if(!irccmp(hashtype
, "MD5"))
80 salt
= make_md5_salt(8);
83 sendto_one_notice(source_p
,
84 ":MKPASSWD syntax error: MKPASSWD pass [SHA256|SHA512|MD5]");
88 crypted
= rb_crypt(parv
[1], salt
);
89 sendto_one_notice(source_p
, ":Hash [%s] for %s: %s", hashtype
, parv
[1], crypted
? crypted
: "???");
93 /* mo_mkpasswd - mkpasswd message handler
98 mo_mkpasswd(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
102 const char *hashtype
;
103 const char hashdefault
[] = "SHA512";
105 if(EmptyString(parv
[1]))
107 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
), me
.name
, source_p
->name
, "MKPASSWD");
112 hashtype
= hashdefault
;
116 if(!irccmp(hashtype
, "SHA256"))
117 salt
= make_sha256_salt(16);
118 else if(!irccmp(hashtype
, "SHA512"))
119 salt
= make_sha512_salt(16);
120 else if(!irccmp(hashtype
, "MD5"))
121 salt
= make_md5_salt(8);
124 sendto_one_notice(source_p
,
125 ":MKPASSWD syntax error: MKPASSWD pass [SHA256|SHA512|MD5]");
129 crypted
= rb_crypt(parv
[1], salt
);
130 sendto_one_notice(source_p
, ":Hash [%s] for %s: %s", hashtype
, parv
[1], crypted
? crypted
: "???");
135 make_md5_salt(int length
)
137 static char salt
[21];
140 printf("MD5 salt length too long\n");
146 generate_random_salt(&salt
[3], length
);
147 salt
[length
+ 3] = '$';
148 salt
[length
+ 4] = '\0';
153 make_sha256_salt(int length
)
155 static char salt
[21];
158 printf("SHA256 salt length too long\n");
164 generate_random_salt(&salt
[3], length
);
165 salt
[length
+ 3] = '$';
166 salt
[length
+ 4] = '\0';
171 make_sha512_salt(int length
)
173 static char salt
[21];
176 printf("SHA512 salt length too long\n");
182 generate_random_salt(&salt
[3], length
);
183 salt
[length
+ 3] = '$';
184 salt
[length
+ 4] = '\0';
189 generate_poor_salt(char *salt
, int length
)
193 for(i
= 0; i
< length
; i
++)
195 salt
[i
] = saltChars
[rand() % 64];
201 generate_random_salt(char *salt
, int length
)
205 if((fd
= open("/dev/random", O_RDONLY
)) < 0)
207 return (generate_poor_salt(salt
, length
));
209 buf
= calloc(1, length
);
210 if(read(fd
, buf
, length
) != length
)
214 return (generate_poor_salt(salt
, length
));
217 for(i
= 0; i
< length
; i
++)
219 salt
[i
] = saltChars
[abs(buf
[i
]) % 64];