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 const char mkpasswd_desc
[] = "Hash a password for use in ircd.conf";
41 DECLARE_MODULE_AV2(mkpasswd
, NULL
, NULL
, mkpasswd_clist
, NULL
, NULL
, NULL
, NULL
, mkpasswd_desc
);
44 /* m_mkpasswd - mkpasswd message handler
49 m_mkpasswd(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
51 static time_t last_used
= 0;
55 const char hashdefault
[] = "SHA512";
57 if(EmptyString(parv
[1]))
59 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
), me
.name
, source_p
->name
, "MKPASSWD");
64 hashtype
= hashdefault
;
68 if((last_used
+ ConfigFileEntry
.pace_wait
) > rb_current_time())
70 /* safe enough to give this on a local connect only */
71 sendto_one(source_p
, form_str(RPL_LOAD2HI
), me
.name
, source_p
->name
, "MKPASSWD");
75 last_used
= rb_current_time();
77 if(!irccmp(hashtype
, "SHA256"))
78 salt
= make_sha256_salt(16);
79 else if(!irccmp(hashtype
, "SHA512"))
80 salt
= make_sha512_salt(16);
81 else if(!irccmp(hashtype
, "MD5"))
82 salt
= make_md5_salt(8);
85 sendto_one_notice(source_p
,
86 ":MKPASSWD syntax error: MKPASSWD pass [SHA256|SHA512|MD5]");
90 crypted
= rb_crypt(parv
[1], salt
);
91 sendto_one_notice(source_p
, ":Hash [%s] for %s: %s", hashtype
, parv
[1], crypted
? crypted
: "???");
95 /* mo_mkpasswd - mkpasswd message handler
100 mo_mkpasswd(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
104 const char *hashtype
;
105 const char hashdefault
[] = "SHA512";
107 if(EmptyString(parv
[1]))
109 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
), me
.name
, source_p
->name
, "MKPASSWD");
114 hashtype
= hashdefault
;
118 if(!irccmp(hashtype
, "SHA256"))
119 salt
= make_sha256_salt(16);
120 else if(!irccmp(hashtype
, "SHA512"))
121 salt
= make_sha512_salt(16);
122 else if(!irccmp(hashtype
, "MD5"))
123 salt
= make_md5_salt(8);
126 sendto_one_notice(source_p
,
127 ":MKPASSWD syntax error: MKPASSWD pass [SHA256|SHA512|MD5]");
131 crypted
= rb_crypt(parv
[1], salt
);
132 sendto_one_notice(source_p
, ":Hash [%s] for %s: %s", hashtype
, parv
[1], crypted
? crypted
: "???");
137 make_md5_salt(int length
)
139 static char salt
[21];
142 printf("MD5 salt length too long\n");
148 generate_random_salt(&salt
[3], length
);
149 salt
[length
+ 3] = '$';
150 salt
[length
+ 4] = '\0';
155 make_sha256_salt(int length
)
157 static char salt
[21];
160 printf("SHA256 salt length too long\n");
166 generate_random_salt(&salt
[3], length
);
167 salt
[length
+ 3] = '$';
168 salt
[length
+ 4] = '\0';
173 make_sha512_salt(int length
)
175 static char salt
[21];
178 printf("SHA512 salt length too long\n");
184 generate_random_salt(&salt
[3], length
);
185 salt
[length
+ 3] = '$';
186 salt
[length
+ 4] = '\0';
191 generate_poor_salt(char *salt
, int length
)
195 for(i
= 0; i
< length
; i
++)
197 salt
[i
] = saltChars
[rand() % 64];
203 generate_random_salt(char *salt
, int length
)
207 if((fd
= open("/dev/random", O_RDONLY
)) < 0)
209 return (generate_poor_salt(salt
, length
));
211 buf
= calloc(1, length
);
212 if(read(fd
, buf
, length
) != length
)
216 return (generate_poor_salt(salt
, length
));
219 for(i
= 0; i
< length
; i
++)
221 salt
[i
] = saltChars
[abs(buf
[i
]) % 64];