Andrew Geissler | ac970dd | 2021-02-12 15:32:45 -0600 | [diff] [blame^] | 1 | From d4de5497bd89c408377194b9fa9026ba8e68b634 Mon Sep 17 00:00:00 2001 |
| 2 | From: Kai Kang <kai.kang@windriver.com> |
| 3 | Date: Mon, 11 Jan 2021 14:11:05 +0800 |
| 4 | Subject: [PATCH 4/8] lxdm.c: add function to change password with pam |
| 5 | |
| 6 | Add function to change user's password when pam is enabled. It is useful |
| 7 | to change user's password when the password is expired. |
| 8 | |
| 9 | Upstream-Status: Submitted [https://sourceforge.net/p/lxdm/code/merge-requests/1/] |
| 10 | |
| 11 | Signed-off-by: Kai Kang <kai.kang@windriver.com> |
| 12 | --- |
| 13 | src/lxdm.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 14 | src/lxdm.h | 1 + |
| 15 | 2 files changed, 69 insertions(+) |
| 16 | |
| 17 | diff --git a/src/lxdm.c b/src/lxdm.c |
| 18 | index 638c30f..fe17a71 100644 |
| 19 | --- a/src/lxdm.c |
| 20 | +++ b/src/lxdm.c |
| 21 | @@ -104,6 +104,10 @@ static int old_tty=1,def_tty = 7,nr_tty=0; |
| 22 | static int def_display=0; |
| 23 | static GSList *session_list; |
| 24 | |
| 25 | +#if HAVE_LIBPAM |
| 26 | +static const char *new_passwd = NULL; |
| 27 | +#endif |
| 28 | + |
| 29 | static void lxdm_startx(LXSession *s); |
| 30 | |
| 31 | static int get_active_vt(void) |
| 32 | @@ -759,6 +763,69 @@ int lxdm_auth_user(int type,char *user, char *pass, struct passwd **ppw) |
| 33 | return ret; |
| 34 | } |
| 35 | |
| 36 | + |
| 37 | +#if HAVE_LIBPAM |
| 38 | + |
| 39 | +static int do_conv(int num, const struct pam_message **msg,struct pam_response **resp, void *arg) |
| 40 | +{ |
| 41 | + int result = PAM_SUCCESS; |
| 42 | + int i; |
| 43 | + |
| 44 | + *resp = (struct pam_response *) calloc(num, sizeof(struct pam_response)); |
| 45 | + for(i = 0; i < num; i++) |
| 46 | + { |
| 47 | + switch (msg[i]->msg_style) { |
| 48 | + case PAM_PROMPT_ECHO_ON: |
| 49 | + break; |
| 50 | + case PAM_PROMPT_ECHO_OFF: |
| 51 | + resp[i]->resp = strdup(new_passwd); |
| 52 | + break; |
| 53 | + case PAM_ERROR_MSG: |
| 54 | + case PAM_TEXT_INFO: |
| 55 | + break; |
| 56 | + default: |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + return result; |
| 61 | +} |
| 62 | + |
| 63 | +static int lxdm_change_passwd_pam(const char *service, const char *user, const char *pass) |
| 64 | +{ |
| 65 | + pam_handle_t *pamh = NULL; |
| 66 | + static struct pam_conv conv = { |
| 67 | + do_conv, |
| 68 | + NULL |
| 69 | + }; |
| 70 | + |
| 71 | + int ret = pam_start("lxdm", user, &conv, &pamh); |
| 72 | + if (PAM_SUCCESS != ret) { |
| 73 | + g_warning("pam_start failed."); |
| 74 | + return 1; |
| 75 | + } |
| 76 | + |
| 77 | + new_passwd = pass; |
| 78 | + ret = pam_chauthtok(pamh, 0); |
| 79 | + if (PAM_SUCCESS != ret) { |
| 80 | + g_warning("pam_chauthtok failed: %s", pam_strerror(pamh, ret)); |
| 81 | + return 1; |
| 82 | + } |
| 83 | + |
| 84 | + (void)pam_end(pamh, PAM_SUCCESS); |
| 85 | + |
| 86 | + return 0; |
| 87 | +} |
| 88 | +#endif |
| 89 | + |
| 90 | +int lxdm_change_passwd(const char *user, const char *pass) |
| 91 | +{ |
| 92 | +#if HAVE_LIBPAM |
| 93 | + return lxdm_change_passwd_pam("lxdm", user, pass); |
| 94 | +#else |
| 95 | + return 0; |
| 96 | +#endif |
| 97 | +} |
| 98 | + |
| 99 | static void close_left_fds(void) |
| 100 | { |
| 101 | struct dirent **list; |
| 102 | @@ -1446,6 +1513,7 @@ int lxdm_do_auto_login(void) |
| 103 | lxdm_do_login(pw,session,lang,option); |
| 104 | success=1; |
| 105 | } |
| 106 | + |
| 107 | g_free(user);g_free(session);g_free(lang); |
| 108 | } |
| 109 | g_free(last_lang); |
| 110 | diff --git a/src/lxdm.h b/src/lxdm.h |
| 111 | index 1c2f837..be3c81f 100644 |
| 112 | --- a/src/lxdm.h |
| 113 | +++ b/src/lxdm.h |
| 114 | @@ -30,6 +30,7 @@ G_BEGIN_DECLS |
| 115 | extern GKeyFile *config; |
| 116 | |
| 117 | int lxdm_auth_user(int type,char *user,char *pass,struct passwd **ppw); |
| 118 | +int lxdm_change_passwd(const char *user, const char *pass); |
| 119 | void lxdm_do_login(struct passwd *pw,char *session,char *lang,char *option); |
| 120 | void lxdm_do_reboot(void); |
| 121 | void lxdm_do_shutdown(void); |
| 122 | -- |
| 123 | 2.25.1 |
| 124 | |