Fix static analysis issue
[platform/core/appfw/app2sd.git] / plugin / app2sd / server / app2sd_internals_utils.c
index 75425bc..c3df8cb 100644 (file)
@@ -343,6 +343,21 @@ char *_app2sd_execute_command(const char *argv[])
 
 }
 
+static int generate_random_num(unsigned short* buf, int len) {
+       int fd = open("/dev/urandom", O_RDONLY);
+       if (fd < 0)
+               return -1;
+
+       if (read(fd, buf, len) < 0) {
+                close(fd);
+                return -1;
+        }
+
+       close(fd);
+
+       return 0;
+}
+
 /*
 * This is a simple password generator
 * return: On success, it will return the password, else NULL.
@@ -354,6 +369,12 @@ char *_app2sd_generate_password(void)
                "!\"#$%&()*+,-./0123456789:;<=>?@ABCDE" \
                "FGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
        int i;
+       unsigned short rand_nums[PASSWD_LEN];
+
+       if (generate_random_num(rand_nums, sizeof(rand_nums)) < 0) {
+               _E("Failed to read random data");
+               return NULL;
+       }
 
        /* include null byte */
        passwd = (char *)malloc(sizeof(char) * (PASSWD_LEN + 1));
@@ -363,12 +384,28 @@ char *_app2sd_generate_password(void)
        }
 
        for (i = 0; i < PASSWD_LEN; i++)
-               passwd[i] = charset[g_random_int() % ASCII_PASSWD_CHAR];
+               passwd[i] = charset[rand_nums[i] % ASCII_PASSWD_CHAR];
        passwd[i] = '\0';
 
        return passwd;
 }
 
+int _app2sd_check_is_luks_device(const char *device_path)
+{
+       int ret = 0;
+       int result = 1; /* default: luks format */
+       const char *argv_bin[] = { "/sbin/cryptsetup", "isLuks", device_path };
+       ret = _xsystem(argv_bin);
+       if (ret < 0)
+               _E("there was errot to check isLuks");
+
+       if (ret == 1) /* legacy format */
+               result = 0;
+
+       _D("ret(%d), result(%d)", ret, result);
+       return result;
+}
+
 int _app2sd_get_loopback_device_path(const char *mmc_path,
                const char *pkgid, uid_t uid, char *loopback_device, size_t len)
 {