Merge branch 'patch-1' of https://github.com/mr-c/arvados into mr-c-patch-1
[arvados.git] / lib / pam / testclient.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 // +build never
6
7 // This file is compiled by docker_test.go to build a test client.
8 // It's not part of the pam module itself.
9
10 package main
11
12 import (
13         "fmt"
14         "os"
15         "os/exec"
16
17         "github.com/msteinert/pam"
18         "github.com/sirupsen/logrus"
19 )
20
21 func main() {
22         if len(os.Args) != 4 || os.Args[1] != "try" {
23                 logrus.Print("usage: testclient try 'username' 'password'")
24                 os.Exit(1)
25         }
26         username := os.Args[2]
27         password := os.Args[3]
28
29         // Configure PAM to use arvados token auth by default.
30         cmd := exec.Command("pam-auth-update", "--force", "arvados", "--remove", "unix")
31         cmd.Env = append([]string{"DEBIAN_FRONTEND=noninteractive"}, os.Environ()...)
32         cmd.Stdin = nil
33         cmd.Stdout = os.Stdout
34         cmd.Stderr = os.Stderr
35         err := cmd.Run()
36         if err != nil {
37                 logrus.WithError(err).Error("pam-auth-update failed")
38                 os.Exit(1)
39         }
40
41         // Check that pam-auth-update actually added arvados config.
42         cmd = exec.Command("grep", "-Hn", "arvados", "/etc/pam.d/common-auth")
43         cmd.Stdout = os.Stderr
44         cmd.Stderr = os.Stderr
45         err = cmd.Run()
46         if err != nil {
47                 panic(err)
48         }
49
50         logrus.Debugf("starting pam: username=%q password=%q", username, password)
51
52         sentPassword := false
53         errorMessage := ""
54         tx, err := pam.StartFunc("default", username, func(style pam.Style, message string) (string, error) {
55                 logrus.Debugf("pam conversation: style=%v message=%q", style, message)
56                 switch style {
57                 case pam.ErrorMsg:
58                         logrus.WithField("Message", message).Info("pam.ErrorMsg")
59                         errorMessage = message
60                         return "", nil
61                 case pam.TextInfo:
62                         logrus.WithField("Message", message).Info("pam.TextInfo")
63                         errorMessage = message
64                         return "", nil
65                 case pam.PromptEchoOn, pam.PromptEchoOff:
66                         sentPassword = true
67                         return password, nil
68                 default:
69                         return "", fmt.Errorf("unrecognized message style %d", style)
70                 }
71         })
72         if err != nil {
73                 logrus.WithError(err).Print("StartFunc failed")
74                 os.Exit(1)
75         }
76         err = tx.Authenticate(pam.DisallowNullAuthtok)
77         if err != nil {
78                 err = fmt.Errorf("PAM: %s (message = %q)", err, errorMessage)
79                 logrus.WithError(err).Print("authentication failed")
80                 os.Exit(1)
81         }
82         logrus.Print("authentication succeeded")
83 }