-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpam.go
125 lines (109 loc) · 2.99 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* pam.go - Utility functions for interfacing with the PAM libraries.
*
* Modified: 2020 by Michael Wyrick
*
* Copyright 2017 Google Inc.
* Author: Joe Richey (joerichey@google.com)
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package axiospam
/*
#cgo LDFLAGS: -lpam
#include "pam.h"
#include <pwd.h>
#include <stdlib.h>
#include <security/pam_modules.h>
*/
import "C"
import (
"errors"
"os/user"
"unsafe"
)
// Handle wraps the C pam_handle_t type. This is used from within modules.
type handle struct {
handle *C.pam_handle_t
status C.int
// PamUser is the user for whom the PAM module is running.
PamUser *user.User
}
func (h *handle) err() error {
if h.status == C.PAM_SUCCESS {
return nil
}
s := C.GoString(C.pam_strerror(h.handle, C.int(h.status)))
return errors.New(s)
}
// Transaction represents a wrapped pam_handle_t type created with pam_start
// form an application.
type transaction handle
// Start initializes a pam Transaction. End() should be called after the
// Transaction is no longer needed.
func start(service, username string) (*transaction, error) {
cService := C.CString(service)
defer C.free(unsafe.Pointer(cService))
cUsername := C.CString(username)
defer C.free(unsafe.Pointer(cUsername))
t := &transaction{
handle: nil,
status: C.PAM_SUCCESS,
}
t.status = C.pam_start(
cService,
cUsername,
C.goConv,
&t.handle)
return t, (*handle)(t).err()
}
// End finalizes a pam Transaction with pam_end().
func (t *transaction) End() {
C.pam_end(t.handle, t.status)
}
// authenticate returns a boolean indicating if the user authenticated correctly
// or not. If the authentication check did not complete, an error is returned.
func (t *transaction) authenticate(quiet bool) (bool, error) {
var flags C.int = C.PAM_DISALLOW_NULL_AUTHTOK
if quiet {
flags |= C.PAM_SILENT
}
t.status = C.pam_authenticate(t.handle, flags)
if t.status == C.PAM_AUTH_ERR {
return false, nil
}
return true, (*handle)(t).err()
}
// changeTok changes the user password
func (t *transaction) changeTok(quiet bool) (int, error) {
var flags C.int = C.PAM_DISALLOW_NULL_AUTHTOK
if quiet {
flags |= C.PAM_SILENT
}
t.status = C.pam_chauthtok(t.handle, flags)
switch t.status {
case C.PAM_SUCCESS:
return 0, nil
case C.PAM_AUTHTOK_ERR:
return 20, nil
}
return -1, (*handle)(t).err()
}
func (t *transaction) accountManagement(quiet bool) (int, error) {
var flags C.int
if quiet {
flags |= C.PAM_SILENT
}
t.status = C.pam_acct_mgmt(t.handle, flags)
return int(t.status), nil
}