-
Notifications
You must be signed in to change notification settings - Fork 6
/
pam.go
55 lines (47 loc) · 1.4 KB
/
pam.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
package main
/*
#cgo LDFLAGS: -lpam
#cgo openbsd CFLAGS: -I/usr/local/include
#cgo openbsd LDFLAGS: -L/usr/local/lib
#cgo darwin CFLAGS: -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
char *homedir(const char *username);
bool login(const char *username, const char *password, const char *exec, pid_t *child_pid);
bool logout(void);
*/
import "C"
import (
"errors"
)
// homedir gets the home directory for a username.
// An error is returned if the lookup was not successful.
func homedir(username string) (string, error) {
cName := C.CString(username)
cHome := C.homedir(cName)
if cHome == nil {
return "", errors.New("unable to look up homedir")
}
return C.GoString(cHome), nil
}
// login logs in the username with password and returns the pid of the login process
// or an error if login failed
func login(username, password, exec string) (int, error) {
cUser := C.CString(username)
cPass := C.CString(password)
cExec := C.CString("exec " + exec)
var child C.pid_t
ok := bool(C.login(cUser, cPass, cExec, &child))
if !ok {
return 0, errors.New("could not log in user")
}
return int(child), nil
}
// logout requests the user log out and returns an error if this was not possible
func logout() error {
if !bool(C.logout()) {
return errors.New("could not log user out")
}
return nil
}