diff options
-rw-r--r-- | .github/workflows/test.yaml | 22 | ||||
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | .travis.yml | 18 | ||||
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | example_test.go | 45 | ||||
-rw-r--r-- | go.mod | 7 | ||||
-rw-r--r-- | go.sum | 4 | ||||
-rw-r--r-- | transaction_test.go | 10 |
8 files changed, 56 insertions, 55 deletions
diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..77c9f2f --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,22 @@ +on: [push, pull_request] +name: Test +jobs: + test: + strategy: + matrix: + go-version: [1.16.x, 1.17.x] + os: [ubuntu-latest] + runs-on: ${{ matrix.os }} + steps: + - name: Install Go + uses: actions/setup-go@v2 + with: + go-version: ${{ matrix.go-version }} + - name: Install PAM + run: sudo apt install -y libpam-dev + - name: Add a test user + run: sudo useradd -d /tmp/test -p '$1$Qd8H95T5$RYSZQeoFbEB.gS19zS99A0' -s /bin/false test + - name: Checkout code + uses: actions/checkout@v2 + - name: Test + run: sudo go test -v ./... diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 2d83068..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -coverage.out diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 8924f0f..0000000 --- a/.travis.yml +++ /dev/null @@ -1,18 +0,0 @@ -language: go - -go: - - 1.14.x - - 1.15.x - - tip - -before_install: - - sudo apt-get update -qq - - sudo apt-get install -qq --no-install-recommends libpam0g-dev - - sudo useradd -d /tmp/test -p '$1$Qd8H95T5$RYSZQeoFbEB.gS19zS99A0' -s /bin/false test - - go get github.com/axw/gocov/gocov - - go get github.com/mattn/goveralls - - go get golang.org/x/tools/cmd/cover - -script: - - sudo GOROOT=$GOROOT GOPATH=$GOPATH $(which go) test -v -covermode=count -coverprofile=coverage.out . - - if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then goveralls -coverprofile=coverage.out -service travis-ci -repotoken $REPO_TOKEN; fi @@ -1,7 +1,5 @@ -[![Build Status](https://travis-ci.org/msteinert/pam.svg?branch=master)](https://travis-ci.org/msteinert/pam) [![GoDoc](https://godoc.org/github.com/msteinert/pam?status.svg)](http://godoc.org/github.com/msteinert/pam) -[![Coverage Status](https://coveralls.io/repos/msteinert/pam/badge.svg?branch=master)](https://coveralls.io/r/msteinert/pam?branch=master) -[![Go Report Card](http://goreportcard.com/badge/msteinert/pam)](http://goreportcard.com/report/msteinert/pam) +[![Go Report Card](https://goreportcard.com/badge/github.com/msteinert/pam)](https://goreportcard.com/report/github.com/msteinert/pam) # Go PAM diff --git a/example_test.go b/example_test.go index aca24a1..8a347a6 100644 --- a/example_test.go +++ b/example_test.go @@ -4,50 +4,49 @@ import ( "bufio" "errors" "fmt" - "log" "os" - "github.com/bgentry/speakeasy" "github.com/msteinert/pam" + "golang.org/x/term" ) -// This example uses whatever default PAM service configuration is available -// on the system, and tries to authenticate any user. This should cause PAM -// to ask its conversation handler for a username and password, in sequence. -// -// This application will handle those requests by displaying the -// PAM-provided prompt and sending back the first line of stdin input -// it can read for each. -// -// Keep in mind that unless run as root (or setuid root), the only -// user's authentication that can succeed is that of the process owner. -func Example_authenticate() { +// This example uses the default PAM service to authenticate any users. This +// should cause PAM to ask its conversation handler for a username and password +// in sequence. +func Example() { t, err := pam.StartFunc("", "", func(s pam.Style, msg string) (string, error) { switch s { case pam.PromptEchoOff: - return speakeasy.Ask(msg) - case pam.PromptEchoOn: - fmt.Print(msg + " ") - input, err := bufio.NewReader(os.Stdin).ReadString('\n') + fmt.Print(msg) + pw, err := term.ReadPassword(int(os.Stdin.Fd())) if err != nil { return "", err } - return input[:len(input)-1], nil + fmt.Println() + return string(pw), nil + case pam.PromptEchoOn: + fmt.Print(msg) + s := bufio.NewScanner(os.Stdin) + s.Scan() + return s.Text(), nil case pam.ErrorMsg: - log.Print(msg) + fmt.Fprintf(os.Stderr, "%s\n", msg) return "", nil case pam.TextInfo: fmt.Println(msg) return "", nil + default: + return "", errors.New("unrecognized message style") } - return "", errors.New("Unrecognized message style") }) if err != nil { - log.Fatalf("Start: %s", err.Error()) + fmt.Fprintf(os.Stderr, "start: %s\n", err.Error()) + os.Exit(1) } err = t.Authenticate(0) if err != nil { - log.Fatalf("Authenticate: %s", err.Error()) + fmt.Fprintf(os.Stderr, "authenticate: %s\n", err.Error()) + os.Exit(1) } - fmt.Println("Authentication succeeded!") + fmt.Println("authentication succeeded!") } @@ -0,0 +1,7 @@ +module github.com/msteinert/pam + +go 1.17 + +require golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 + +require golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect @@ -0,0 +1,4 @@ +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= diff --git a/transaction_test.go b/transaction_test.go index 84ebb4a..2da45ab 100644 --- a/transaction_test.go +++ b/transaction_test.go @@ -3,7 +3,6 @@ package pam import ( "errors" "os/user" - "runtime" "testing" ) @@ -31,7 +30,6 @@ func TestPAM_001(t *testing.T) { if err != nil { t.Fatalf("setcred #error: %v", err) } - runtime.GC() } func TestPAM_002(t *testing.T) { @@ -55,7 +53,6 @@ func TestPAM_002(t *testing.T) { if err != nil { t.Fatalf("authenticate #error: %v", err) } - runtime.GC() } type Credentials struct { @@ -90,7 +87,6 @@ func TestPAM_003(t *testing.T) { if err != nil { t.Fatalf("authenticate #error: %v", err) } - runtime.GC() } func TestPAM_004(t *testing.T) { @@ -109,7 +105,6 @@ func TestPAM_004(t *testing.T) { if err != nil { t.Fatalf("authenticate #error: %v", err) } - runtime.GC() } func TestPAM_005(t *testing.T) { @@ -127,7 +122,6 @@ func TestPAM_005(t *testing.T) { if err != nil { t.Fatalf("chauthtok #error: %v", err) } - runtime.GC() } func TestPAM_006(t *testing.T) { @@ -149,7 +143,6 @@ func TestPAM_006(t *testing.T) { if err != nil { t.Fatalf("close_session #error: %v", err) } - runtime.GC() } func TestPAM_007(t *testing.T) { @@ -171,7 +164,6 @@ func TestPAM_007(t *testing.T) { if len(s) == 0 { t.Fatalf("error #expected an error message") } - runtime.GC() } func TestItem(t *testing.T) { @@ -206,7 +198,6 @@ func TestItem(t *testing.T) { if s != "root" { t.Fatalf("getitem #error: expected root, got %v", s) } - runtime.GC() } func TestEnv(t *testing.T) { @@ -273,7 +264,6 @@ func TestEnv(t *testing.T) { if m["VAL3"] != "3" { t.Fatalf("getenvlist #error: expected 3, got %v", m["VAL1"]) } - runtime.GC() } func TestFailure_001(t *testing.T) { |