1
- //+build libpam
2
-
3
1
/*
4
2
Maddy Mail Server - Composable all-in-one email server.
5
3
Copyright © 2019-2022 Max Mazurov <[email protected] >, Maddy Mail Server contributors
@@ -21,28 +19,33 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
21
19
#define _POSIX_C_SOURCE 200809L
22
20
#include <stdio.h>
23
21
#include <stdlib.h>
22
+ #include <string.h>
24
23
#include <security/pam_appl.h>
25
24
#include "pam.h"
26
25
27
26
static int conv_func (int num_msg , const struct pam_message * * msg , struct pam_response * * resp , void * appdata_ptr ) {
28
- * resp = (struct pam_response * )appdata_ptr ;
29
- return PAM_SUCCESS ;
30
- }
31
-
32
- struct error_obj run_pam_auth (const char * username , char * password ) {
33
- // PAM frees pam_response for us.
34
27
struct pam_response * reply = malloc (sizeof (struct pam_response ));
35
28
if (reply == NULL ) {
36
- struct error_obj ret_val ;
37
- ret_val .status = 2 ;
38
- ret_val .func_name = "malloc" ;
39
- ret_val .error_msg = "Out of memory" ;
40
- return ret_val ;
29
+ return PAM_CONV_ERR ;
41
30
}
42
- reply -> resp = password ;
31
+
32
+ char * password_cpy = malloc (strlen ((char * )appdata_ptr )+ 1 );
33
+ if (password_cpy == NULL ) {
34
+ return PAM_CONV_ERR ;
35
+ }
36
+ memcpy (password_cpy , (char * )appdata_ptr , strlen ((char * )appdata_ptr )+ 1 );
37
+
38
+ reply -> resp = password_cpy ;
43
39
reply -> resp_retcode = 0 ;
44
40
45
- const struct pam_conv local_conv = { conv_func , reply };
41
+ // PAM frees pam_response for us.
42
+ * resp = reply ;
43
+
44
+ return PAM_SUCCESS ;
45
+ }
46
+
47
+ struct error_obj run_pam_auth (const char * username , char * password ) {
48
+ const struct pam_conv local_conv = { conv_func , password };
46
49
pam_handle_t * local_auth = NULL ;
47
50
int status = pam_start ("maddy" , username , & local_conv , & local_auth );
48
51
if (status != PAM_SUCCESS ) {
0 commit comments