From d4de5497bd89c408377194b9fa9026ba8e68b634 Mon Sep 17 00:00:00 2001
From: Kai Kang <kai.kang@windriver.com>
Date: Mon, 11 Jan 2021 14:11:05 +0800
Subject: [PATCH 4/8] lxdm.c: add function to change password with pam

Add function to change user's password when pam is enabled. It is useful
to change user's password when the password is expired.

Upstream-Status: Submitted [https://sourceforge.net/p/lxdm/code/merge-requests/1/]

Signed-off-by: Kai Kang <kai.kang@windriver.com>
---
 src/lxdm.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/lxdm.h |  1 +
 2 files changed, 69 insertions(+)

diff --git a/src/lxdm.c b/src/lxdm.c
index 638c30f..fe17a71 100644
--- a/src/lxdm.c
+++ b/src/lxdm.c
@@ -104,6 +104,10 @@ static int old_tty=1,def_tty = 7,nr_tty=0;
 static int def_display=0;
 static GSList *session_list;
 
+#if HAVE_LIBPAM
+static const char *new_passwd = NULL;
+#endif
+
 static void lxdm_startx(LXSession *s);
 
 static int get_active_vt(void)
@@ -759,6 +763,69 @@ int lxdm_auth_user(int type,char *user, char *pass, struct passwd **ppw)
 	return ret;
 }
 
+
+#if HAVE_LIBPAM
+
+static int do_conv(int num, const struct pam_message **msg,struct pam_response **resp, void *arg)
+{
+	int result = PAM_SUCCESS;
+	int i;
+
+	*resp = (struct pam_response *) calloc(num, sizeof(struct pam_response));
+	for(i = 0; i < num; i++)
+	{
+		switch (msg[i]->msg_style) {
+			case PAM_PROMPT_ECHO_ON:
+				break;
+			case PAM_PROMPT_ECHO_OFF:
+				resp[i]->resp = strdup(new_passwd);
+				break;
+			case PAM_ERROR_MSG:
+			case PAM_TEXT_INFO:
+				break;
+			default:
+				break;
+		}
+	}
+	return result;
+}
+
+static int lxdm_change_passwd_pam(const char *service, const char *user, const char *pass)
+{
+	pam_handle_t *pamh = NULL;
+	static struct pam_conv conv = {
+		do_conv,
+		NULL
+	};
+
+	int ret = pam_start("lxdm", user, &conv, &pamh);
+	if (PAM_SUCCESS != ret) {
+		g_warning("pam_start failed.");
+		return 1;
+	}
+
+	new_passwd = pass;
+	ret = pam_chauthtok(pamh, 0);
+	if (PAM_SUCCESS != ret) {
+		g_warning("pam_chauthtok failed: %s", pam_strerror(pamh, ret));
+		return 1;
+	}
+
+	(void)pam_end(pamh, PAM_SUCCESS);
+
+	return 0;
+}
+#endif
+
+int lxdm_change_passwd(const char *user, const char *pass)
+{
+#if HAVE_LIBPAM
+	return lxdm_change_passwd_pam("lxdm", user, pass);
+#else
+	return 0;
+#endif
+}
+
 static void close_left_fds(void)
 {
 	struct dirent **list;
@@ -1446,6 +1513,7 @@ int lxdm_do_auto_login(void)
 			lxdm_do_login(pw,session,lang,option);
 			success=1;
 		}
+
 		g_free(user);g_free(session);g_free(lang);
 	}
 	g_free(last_lang);
diff --git a/src/lxdm.h b/src/lxdm.h
index 1c2f837..be3c81f 100644
--- a/src/lxdm.h
+++ b/src/lxdm.h
@@ -30,6 +30,7 @@ G_BEGIN_DECLS
 extern GKeyFile *config;
 
 int lxdm_auth_user(int type,char *user,char *pass,struct passwd **ppw);
+int lxdm_change_passwd(const char *user, const char *pass);
 void lxdm_do_login(struct passwd *pw,char *session,char *lang,char *option);
 void lxdm_do_reboot(void);
 void lxdm_do_shutdown(void);
-- 
2.25.1

