vendor: update github.com/msteinert/pam (#4216)

This commit is contained in:
Unknwon
2017-05-29 23:43:40 -04:00
parent 9085c3b73d
commit 0778d7de80
5 changed files with 68 additions and 53 deletions

View File

@@ -5,13 +5,7 @@
# Go PAM
This is a Go wrapper for the PAM application API. There's not much
else to be said. PAM is a simple API and now it's available for use in Go
applications.
There's an example of a "fake login" program in the examples directory.
Look at the pam module's [godocs][1] for details about the Go API, or refer
to the official [PAM documentation][2].
This is a Go wrapper for the PAM application API.
## Testing

39
vendor/github.com/msteinert/pam/callback.go generated vendored Normal file
View File

@@ -0,0 +1,39 @@
package pam
import "sync"
var cb struct {
sync.Mutex
m map[int]interface{}
c int
}
func init() {
cb.m = make(map[int]interface{})
}
func cbAdd(v interface{}) int {
cb.Lock()
defer cb.Unlock()
cb.c++
cb.m[cb.c] = v
return cb.c
}
func cbGet(c int) interface{} {
cb.Lock()
defer cb.Unlock()
if v, ok := cb.m[c]; ok {
return v
}
panic("Callback pointer not found")
}
func cbDelete(c int) {
cb.Lock()
defer cb.Unlock()
if _, ok := cb.m[c]; !ok {
panic("Callback pointer not found")
}
delete(cb.m, c)
}

View File

@@ -19,7 +19,7 @@ int cb_pam_conv(
struct cbPAMConv_return result = cbPAMConv(
msg[i]->msg_style,
(char *)msg[i]->msg,
appdata_ptr);
(long)appdata_ptr);
if (result.r1 != PAM_SUCCESS) {
goto error;
}
@@ -39,13 +39,8 @@ error:
return PAM_CONV_ERR;
}
struct pam_conv *make_pam_conv(void *appdata_ptr)
void init_pam_conv(struct pam_conv *conv, long c)
{
struct pam_conv* conv = malloc(sizeof *conv);
if (!conv) {
return NULL;
}
conv->conv = cb_pam_conv;
conv->appdata_ptr = appdata_ptr;
return conv;
conv->appdata_ptr = (void *)c;
}

View File

@@ -1,10 +1,11 @@
// Package pam provides a wrapper for the PAM application API.
package pam
//#include <security/pam_appl.h>
//#include <stdlib.h>
//#cgo CFLAGS: -Wall -std=c99
//#cgo LDFLAGS: -lpam
//struct pam_conv *make_pam_conv(void *);
//void init_pam_conv(struct pam_conv *conv, long c);
import "C"
import (
@@ -50,31 +51,16 @@ func (f ConversationFunc) RespondPAM(s Style, msg string) (string, error) {
return f(s, msg)
}
// Internal conversation structure
type conversation struct {
handler ConversationHandler
conv *C.struct_pam_conv
}
// Constructs a new conversation object with a given handler and a newly
// allocated pam_conv struct that uses this object as its appdata_ptr.
func newConversation(handler ConversationHandler) (*conversation, C.int) {
c := &conversation{}
c.handler = handler
c.conv = C.make_pam_conv(unsafe.Pointer(c))
if c.conv == nil {
return nil, C.PAM_BUF_ERR
}
return c, C.PAM_SUCCESS
}
// Go-side function for processing a single conversational message. Ultimately
// this calls the associated ConversationHandler's ResponsePAM callback with data
// coming in from a C-side call.
// cbPAMConv is a wrapper for the conversation callback function.
//export cbPAMConv
func cbPAMConv(s C.int, msg *C.char, appdata unsafe.Pointer) (*C.char, C.int) {
c := (*conversation)(appdata)
r, err := c.handler.RespondPAM(Style(s), C.GoString(msg))
func cbPAMConv(s C.int, msg *C.char, c int) (*C.char, C.int) {
var r string
var err error
v := cbGet(c)
switch cb := v.(type) {
case ConversationHandler:
r, err = cb.RespondPAM(Style(s), C.GoString(msg))
}
if err != nil {
return nil, C.PAM_CONV_ERR
}
@@ -84,14 +70,16 @@ func cbPAMConv(s C.int, msg *C.char, appdata unsafe.Pointer) (*C.char, C.int) {
// Transaction is the application's handle for a PAM transaction.
type Transaction struct {
handle *C.pam_handle_t
conv *conversation
conv *C.struct_pam_conv
status C.int
c int
}
// Finalize a PAM transaction.
// transactionFinalizer cleans up the PAM handle and deletes the callback
// function.
func transactionFinalizer(t *Transaction) {
C.pam_end(t.handle, t.status)
C.free(unsafe.Pointer(t.conv.conv))
cbDelete(t.c)
}
// Start initiates a new PAM transaction. Service is treated identically to
@@ -100,11 +88,12 @@ func transactionFinalizer(t *Transaction) {
// All application calls to PAM begin with Start (or StartFunc). The returned
// transaction provides an interface to the remainder of the API.
func Start(service, user string, handler ConversationHandler) (*Transaction, error) {
t := &Transaction{}
t.conv, t.status = newConversation(handler)
if t.status != C.PAM_SUCCESS {
return nil, t
t := &Transaction{
conv: &C.struct_pam_conv{},
c: cbAdd(handler),
}
C.init_pam_conv(t.conv, C.long(t.c))
runtime.SetFinalizer(t, transactionFinalizer)
s := C.CString(service)
defer C.free(unsafe.Pointer(s))
var u *C.char
@@ -112,12 +101,10 @@ func Start(service, user string, handler ConversationHandler) (*Transaction, err
u = C.CString(user)
defer C.free(unsafe.Pointer(u))
}
t.status = C.pam_start(s, u, t.conv.conv, &t.handle)
t.status = C.pam_start(s, u, t.conv, &t.handle)
if t.status != C.PAM_SUCCESS {
C.free(unsafe.Pointer(t.conv.conv))
return nil, t
}
runtime.SetFinalizer(t, transactionFinalizer)
return t, nil
}