- don't free user-supplied string (via -e)
[oweals/busybox.git] / libbb / pw_encrypt.c
index d439fc3b4cab00d9c6b77e1a126eee9d494d360c..469e71f6ccdc9606a8f895d9db2d07127a17419f 100644 (file)
@@ -9,13 +9,11 @@
 
 #include "libbb.h"
 
+#if ENABLE_USE_BB_CRYPT
+
 /*
  * DES and MD5 crypt implementations are taken from uclibc.
  * They were modified to not use static buffers.
- * Comparison with uclibc (before uclibc had 70k staic buffers reinstated):
- *   text    data     bss     dec     hex filename
- * 759909     604    6684  767197   bb4dd busybox_old
- * 759579     604    6684  766867   bb393 busybox_unstripped
  */
 /* Common for them */
 static const uint8_t ascii64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
@@ -52,9 +50,9 @@ static void my_crypt_cleanup(void)
        des_ctx = NULL;
 }
 
-char *pw_encrypt(const char *clear, const char *salt, int cleanup)
+char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
 {
-       static char *cipher;
+       char *encrypted;
 
 #if 0 /* was CONFIG_FEATURE_SHA1_PASSWORDS, but there is no such thing??? */
        if (strncmp(salt, "$2$", 3) == 0) {
@@ -62,11 +60,25 @@ char *pw_encrypt(const char *clear, const char *salt, int cleanup)
        }
 #endif
 
-       free(cipher);
-       cipher = my_crypt(clear, salt);
+       encrypted = my_crypt(clear, salt);
 
        if (cleanup)
                my_crypt_cleanup();
 
-       return cipher;
+       return encrypted;
+}
+
+#else /* if !ENABLE_USE_BB_CRYPT */
+
+char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
+{
+#if 0 /* was CONFIG_FEATURE_SHA1_PASSWORDS, but there is no such thing??? */
+       if (strncmp(salt, "$2$", 3) == 0) {
+               return xstrdup(sha1_crypt(clear));
+       }
+#endif
+
+       return xstrdup(crypt(clear, salt));
 }
+
+#endif