Skip to content

Instantly share code, notes, and snippets.

@radical
Created June 22, 2012 08:47
Show Gist options
  • Save radical/2971433 to your computer and use it in GitHub Desktop.
Save radical/2971433 to your computer and use it in GitHub Desktop.
Index: cifs-utils-4.9/mount.cifs.c
===================================================================
--- cifs-utils-4.9.orig/mount.cifs.c
+++ cifs-utils-4.9/mount.cifs.c
@@ -1641,6 +1641,58 @@ drop_child_privs(void)
return 0;
}
+/*
+ * If systemd is present, then try to get password via
+ * /bin/systemd-ask-password, else just use getpass(..)
+ */
+static char*
+get_password(const char *prompt, char *input, int capacity)
+{
+ int is_systemd_running;
+ struct stat a, b;
+
+ /* We simply test whether the systemd cgroup hierarchy is
+ * mounted */
+ is_systemd_running = (lstat("/sys/fs/cgroup", &a) == 0)
+ && (lstat("/sys/fs/cgroup/systemd", &b) == 0)
+ && (a.st_dev != b.st_dev);
+
+ if (is_systemd_running) {
+ /* systemd */
+ char *cmd;
+ FILE *fp = NULL;
+
+ if (asprintf(&cmd, "/bin/systemd-ask-password \"%s\"", prompt) >= 0) {
+ fp = popen (cmd, "re");
+ free (cmd);
+ }
+
+ if (fp) {
+ if (fgets(input, capacity, fp)) {
+ int len = strlen(input);
+ if (input[len - 1] == '\n')
+ input[len - 1] = '\0';
+ }
+
+ fclose(fp);
+ return input;
+ } // else fallback on getpass(..)
+ }
+
+ /* no systemd or couldn't run systemd-ask-password */
+
+ /* getpass is obsolete, but there's apparently nothing that replaces it */
+ char *tmp_pass = getpass(prompt);
+ if (!tmp_pass)
+ return NULL;
+
+ strncpy(input, tmp_pass, capacity);
+ /* zero-out the static buffer */
+ memset(tmp_pass, 0, strlen(tmp_pass));
+
+ return input;
+}
+
static int
assemble_mountinfo(struct parsed_mount_info *parsed_info,
const char *thisprogram, const char *mountpoint,
@@ -1722,14 +1774,20 @@ assemble_mountinfo(struct parsed_mount_i
}
if (!parsed_info->got_password) {
- /* getpass is obsolete, but there's apparently nothing that replaces it */
- char *tmp_pass = getpass("Password: ");
- if (!tmp_pass) {
+ char tmp_pass[MOUNT_PASSWD_SIZE + 1];
+ char *prompt = NULL;
+
+ if(asprintf(&prompt, "Password for %s@%s: ", parsed_info->username, orig_dev) < 0)
+ prompt = NULL;
+
+ if (get_password(prompt ? prompt : "Password: ", tmp_pass, MOUNT_PASSWD_SIZE + 1)) {
+ rc = set_password(parsed_info, tmp_pass);
+ } else {
fprintf(stderr, "Error reading password, exiting\n");
rc = EX_SYSERR;
- goto assemble_exit;
}
- rc = set_password(parsed_info, tmp_pass);
+
+ free(prompt);
if (rc)
goto assemble_exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment