/
pam_auth.go
60 lines (47 loc) · 1.53 KB
/
pam_auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package util
// #cgo LDFLAGS: -lpam
/*
// Code taken from : https://stackoverflow.com/questions/10910193/how-to-authenticate-username-password-using-pam-w-o-root-privileges
#include <stdio.h>
#include <security/pam_appl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
struct pam_response *reply;
// //function used to get user input
int function_conversation(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr){
*resp = reply;
return PAM_SUCCESS;
}
int authenticate_system(const char *username, const char *password)
{
const struct pam_conv local_conversation = { function_conversation, NULL };
pam_handle_t *local_auth_handle = NULL; // this gets set by pam_start
int retval;
retval = pam_start("passwd", username, &local_conversation, &local_auth_handle);
if (retval != PAM_SUCCESS){
return 0;
}
reply = (struct pam_response *)malloc(sizeof(struct pam_response));
reply[0].resp = strdup(password);
reply[0].resp_retcode = 0;
retval = pam_authenticate(local_auth_handle, 0);
if (retval != PAM_SUCCESS){
return 0;
}
retval = pam_end(local_auth_handle, retval);
if (retval != PAM_SUCCESS){
return 0;
}
return 1;
}
*/
import "C"
// PamAauthenticate - authenticates a user based on local PAM based
// authentication
func PamAauthenticate(username, password string) int {
uname := C.CString(username)
pass := C.CString(password)
res := C.int(C.authenticate_system(uname, pass))
return int(res)
}