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.
17 static int m_mkpasswd(struct Client
*client_p
, struct Client
*source_p
,
18 int parc
, const char *parv
[]);
19 static int mo_mkpasswd(struct Client
*client_p
, struct Client
*source_p
,
20 int parc
, const char *parv
[]);
22 static char *make_md5_salt(int);
23 static char *make_sha256_salt(int);
24 static char *make_sha512_salt(int);
25 static char *generate_random_salt(char *, int);
26 static char *generate_poor_salt(char *, int);
28 static char saltChars
[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
29 /* 0 .. 63, ascii - 64 */
31 struct Message mkpasswd_msgtab
= {
32 "MKPASSWD", 0, 0, 0, MFLG_SLOW
,
33 {mg_unreg
, {m_mkpasswd
, 2}, mg_ignore
, mg_ignore
, mg_ignore
, {mo_mkpasswd
, 2}}
36 mapi_clist_av1 mkpasswd_clist
[] = { &mkpasswd_msgtab
, NULL
};
38 DECLARE_MODULE_AV1(mkpasswd
, NULL
, NULL
, mkpasswd_clist
, NULL
, NULL
, "$Revision$");
41 /* m_mkpasswd - mkpasswd message handler
46 m_mkpasswd(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
48 static time_t last_used
= 0;
51 const char hashdefault
[] = "SHA512";
53 if(EmptyString(parv
[1]))
55 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
), me
.name
, source_p
->name
, "MKPASSWD");
60 hashtype
= hashdefault
;
64 if((last_used
+ ConfigFileEntry
.pace_wait
) > rb_current_time())
66 /* safe enough to give this on a local connect only */
67 sendto_one(source_p
, form_str(RPL_LOAD2HI
), me
.name
, source_p
->name
, "MKPASSWD");
71 last_used
= rb_current_time();
73 if(!irccmp(hashtype
, "SHA256"))
74 salt
= make_sha256_salt(16);
75 else if(!irccmp(hashtype
, "SHA512"))
76 salt
= make_sha512_salt(16);
77 else if(!irccmp(hashtype
, "MD5"))
78 salt
= make_md5_salt(8);
81 sendto_one_notice(source_p
,
82 ":MKPASSWD syntax error: MKPASSWD pass [SHA256|SHA512|MD5]");
86 sendto_one_notice(source_p
, ":Hash [%s] for %s: %s", hashtype
, parv
[1], rb_crypt(parv
[1], salt
));
90 /* mo_mkpasswd - mkpasswd message handler
95 mo_mkpasswd(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
99 const char hashdefault
[] = "SHA512";
101 if(EmptyString(parv
[1]))
103 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
), me
.name
, source_p
->name
, "MKPASSWD");
108 hashtype
= hashdefault
;
112 if(!irccmp(hashtype
, "SHA256"))
113 salt
= make_sha256_salt(16);
114 else if(!irccmp(hashtype
, "SHA512"))
115 salt
= make_sha512_salt(16);
116 else if(!irccmp(hashtype
, "MD5"))
117 salt
= make_md5_salt(8);
120 sendto_one_notice(source_p
,
121 ":MKPASSWD syntax error: MKPASSWD pass [SHA256|SHA512|MD5]");
125 sendto_one_notice(source_p
, ":Hash [%s] for %s: %s", hashtype
, parv
[1], rb_crypt(parv
[1], salt
));
130 make_md5_salt(int length
)
132 static char salt
[21];
135 printf("MD5 salt length too long\n");
141 generate_random_salt(&salt
[3], length
);
142 salt
[length
+ 3] = '$';
143 salt
[length
+ 4] = '\0';
148 make_sha256_salt(int length
)
150 static char salt
[21];
153 printf("SHA256 salt length too long\n");
159 generate_random_salt(&salt
[3], length
);
160 salt
[length
+ 3] = '$';
161 salt
[length
+ 4] = '\0';
166 make_sha512_salt(int length
)
168 static char salt
[21];
171 printf("SHA512 salt length too long\n");
177 generate_random_salt(&salt
[3], length
);
178 salt
[length
+ 3] = '$';
179 salt
[length
+ 4] = '\0';
184 generate_poor_salt(char *salt
, int length
)
188 for(i
= 0; i
< length
; i
++)
190 salt
[i
] = saltChars
[rand() % 64];
196 generate_random_salt(char *salt
, int length
)
200 if((fd
= open("/dev/random", O_RDONLY
)) < 0)
202 return (generate_poor_salt(salt
, length
));
204 buf
= calloc(1, length
);
205 if(read(fd
, buf
, length
) != length
)
208 return (generate_poor_salt(salt
, length
));
211 for(i
= 0; i
< length
; i
++)
213 salt
[i
] = saltChars
[abs(buf
[i
]) % 64];