Debian Codegraph @

pam/pam.go

raw
1 /*
2 * pam.go - Utility functions for interfacing with the PAM libraries.
3 *
4 * Copyright 2017 Google Inc.
5 * Author: Joe Richey ([email protected])
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8 * use this file except in compliance with the License. You may obtain a copy of
9 * the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 * License for the specific language governing permissions and limitations under
17 * the License.
18 */
19
20 package pam
21
22 /*
23 #cgo LDFLAGS: -lpam
24 #include "pam.h"
25
26 #include <pwd.h>
27 #include <stdlib.h>
28 #include <security/pam_modules.h>
29 */
30 import "C"
31 import (
32 "errors"
33 "log"
34 "os/user"
35 "unsafe"
36
37 "github.com/google/fscrypt/security"
38 )
39
40 // Handle wraps the C pam_handle_t type. This is used from within modules.
41 type Handle struct {
42 handle *C.pam_handle_t
43 status C.int
44 origPrivs *security.Privileges
45 // PamUser is the user for whom the PAM module is running.
46 PamUser *user.User
47 }
48
49 // NewHandle creates a Handle from a raw pointer.
50 func NewHandle(pamh unsafe.Pointer) (*Handle, error) {
51 var err error
52 h := &Handle{
53 handle: (*C.pam_handle_t)(pamh),
54 status: C.PAM_SUCCESS,
55 }
56
57 var pamUsername *C.char
58 h.status = C.pam_get_user(h.handle, &pamUsername, nil)
59 if err = h.err(); err != nil {
60 return nil, err
61 }
62
63 h.PamUser, err = user.Lookup(C.GoString(pamUsername))
64 return h, err
65 }
66
67 func (h *Handle) setData(name string, data unsafe.Pointer, cleanup C.CleanupFunc) error {
68 cName := C.CString(name)
69 defer C.free(unsafe.Pointer(cName))
70 h.status = C.pam_set_data(h.handle, cName, data, cleanup)
71 return h.err()
72 }
73
74 func (h *Handle) getData(name string) (unsafe.Pointer, error) {
75 var data unsafe.Pointer
76 cName := C.CString(name)
77 defer C.free(unsafe.Pointer(cName))
78 h.status = C.pam_get_data(h.handle, cName, &data)
79 return data, h.err()
80 }
81
82 // ClearData removes the PAM data with the specified name.
83 func (h *Handle) ClearData(name string) error {
84 return h.setData(name, unsafe.Pointer(C.CString("")), C.CleanupFunc(C.freeData))
85 }
86
87 // SetSecret sets a copy of the C string secret into the PAM data with the
88 // specified name. This copy will be held in locked memory until this PAM data
89 // is cleared.
90 func (h *Handle) SetSecret(name string, secret unsafe.Pointer) error {
91 return h.setData(name, C.copyIntoSecret(secret), C.CleanupFunc(C.freeSecret))
92 }
93
94 // GetSecret returns a pointer to the C string PAM data with the specified name.
95 // This is a pointer directly to the data, so it shouldn't be modified. It
96 // should have been previously set with SetSecret().
97 func (h *Handle) GetSecret(name string) (unsafe.Pointer, error) {
98 return h.getData(name)
99 }
100
101 // SetString sets a string value for the PAM data with the specified name.
102 func (h *Handle) SetString(name string, s string) error {
103 return h.setData(name, unsafe.Pointer(C.CString(s)), C.CleanupFunc(C.freeData))
104 }
105
106 // GetString gets a string value for the PAM data with the specified name. It
107 // should have been previously set with SetString().
108 func (h *Handle) GetString(name string) (string, error) {
109 data, err := h.getData(name)
110 if err != nil {
111 return "", err
112 }
113 return C.GoString((*C.char)(data)), nil
114 }
115
116 // GetItem retrieves a PAM information item. This is a pointer directly to the
117 // data, so it shouldn't be modified.
118 func (h *Handle) GetItem(i Item) (unsafe.Pointer, error) {
119 var data unsafe.Pointer
120 h.status = C.pam_get_item(h.handle, C.int(i), &data)
121 if err := h.err(); err != nil {
122 return nil, err
123 }
124 if data == nil {
125 return nil, errors.New("item not found")
126 }
127 return data, nil
128 }
129
130 // GetServiceName retrieves the name of the application running the PAM transaction.
131 func (h *Handle) GetServiceName() string {
132 data, err := h.GetItem(Service)
133 if err != nil {
134 return "[unknown service]"
135 }
136 return C.GoString((*C.char)(data))
137 }
138
139 // StartAsPamUser sets the effective privileges to that of the PAM user.
140 func (h *Handle) StartAsPamUser() error {
141 userPrivs, err := security.UserPrivileges(h.PamUser)
142 if err != nil {
143 return err
144 }
145 origPrivs, err := security.ProcessPrivileges()
146 if err != nil {
147 return err
148 }
149 if err = security.SetProcessPrivileges(userPrivs); err != nil {
150 return err
151 }
152 h.origPrivs = origPrivs
153 return nil
154 }
155
156 // StopAsPamUser restores the original privileges that were running the
157 // PAM module (this is usually root).
158 func (h *Handle) StopAsPamUser() error {
159 if h.origPrivs == nil {
160 return nil
161 }
162 err := security.SetProcessPrivileges(h.origPrivs)
163 h.origPrivs = nil
164 if err != nil {
165 log.Print(err)
166 }
167 return err
168 }
169
170 func (h *Handle) err() error {
171 if h.status == C.PAM_SUCCESS {
172 return nil
173 }
174 s := C.GoString(C.pam_strerror(h.handle, C.int(h.status)))
175 return errors.New(s)
176 }
177
178 // InfoMessage sends a message to the application using pam_info().
179 func (h *Handle) InfoMessage(message string) {
180 cMessage := C.CString(message)
181 defer C.free(unsafe.Pointer(cMessage))
182 C.infoMessage(h.handle, cMessage)
183 }
184
185 // Transaction represents a wrapped pam_handle_t type created with pam_start
186 // from an application.
187 type Transaction Handle
188
189 // Start initializes a pam Transaction. End() should be called after the
190 // Transaction is no longer needed.
191 func Start(service, username string) (*Transaction, error) {
192 cService := C.CString(service)
193 defer C.free(unsafe.Pointer(cService))
194 cUsername := C.CString(username)
195 defer C.free(unsafe.Pointer(cUsername))
196
197 t := &Transaction{
198 handle: nil,
199 status: C.PAM_SUCCESS,
200 }
201 t.status = C.pam_start(
202 cService,
203 cUsername,
204 C.goConv,
205 &t.handle)
206 return t, (*Handle)(t).err()
207 }
208
209 // End finalizes a pam Transaction with pam_end().
210 func (t *Transaction) End() {
211 C.pam_end(t.handle, t.status)
212 }
213
214 // Authenticate returns a boolean indicating if the user authenticated correctly
215 // or not. If the authentication check did not complete, an error is returned.
216 func (t *Transaction) Authenticate(quiet bool) (bool, error) {
217 var flags C.int = C.PAM_DISALLOW_NULL_AUTHTOK
218 if quiet {
219 flags |= C.PAM_SILENT
220 }
221 t.status = C.pam_authenticate(t.handle, flags)
222 if t.status == C.PAM_AUTH_ERR {
223 return false, nil
224 }
225 return true, (*Handle)(t).err()
226 }