-
Notifications
You must be signed in to change notification settings - Fork 0
/
pam.c
42 lines (35 loc) · 994 Bytes
/
pam.c
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
// Standard library includes
#include <stdlib.h>
#include <string.h>
// Third party includes
#include <security/pam_appl.h>
#include <security/pam_ext.h>
// Local includes
#include "pam.h"
// Retrieve a username from a PAM handle
char* get_username(pam_handle_t* handle) {
if (!handle) {
return NULL;
}
const char* username = NULL;
// pam_get_item outputs into `const void**` so we must cast `user` accordingly
if (pam_get_item(handle, PAM_USER, (const void**)&username) != PAM_SUCCESS) {
return NULL;
}
return strdup(username);
}
// Retrieve a password from a PAM handle
char* get_password(pam_handle_t* handle) {
if (!handle) {
return NULL;
}
const char* password = NULL;
if (pam_get_authtok(handle, PAM_AUTHTOK, &password, NULL) != PAM_SUCCESS) {
return NULL;
}
return strdup(password);
}
// Get the specified item from an array
char *get_array_item(int i, char **argv) {
return strdup(argv[i]);
}