Debian Codegraph @

pam/login.go

raw
1 /*
2 * login.go - Checks the validity of a login token key against PAM.
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 contains all the functionality for interfacing with Linux
21 // Pluggable Authentication Modules (PAM). Currently, all this package does is
22 // check the validity of a user's login passphrase.
23 // See http://www.linux-pam.org/Linux-PAM-html/ for more information.
24 package pam
25
26 import "C"
27
28 import (
29 "fmt"
30 "log"
31 "sync"
32
33 "github.com/pkg/errors"
34
35 "github.com/google/fscrypt/crypto"
36 "github.com/google/fscrypt/util"
37 )
38
39 // Pam error values
40 var (
41 ErrPassphrase = errors.New("incorrect login passphrase")
42 )
43
44 // Global state is needed for the PAM callback, so we guard this function with a
45 // lock. tokenToCheck is only ever non-nil when tokenLock is held.
46 var (
47 tokenLock sync.Mutex
48 tokenToCheck *crypto.Key
49 )
50
51 // userInput is run when the callback needs some input from the user. We prompt
52 // the user for information and return their answer. A return value of nil
53 // indicates an error occurred.
54 //
55 //export userInput
56 func userInput(prompt *C.char) *C.char {
57 fmt.Print(C.GoString(prompt))
58 input, err := util.ReadLine()
59 if err != nil {
60 log.Printf("getting input for PAM: %s", err)
61 return nil
62 }
63 return C.CString(input)
64 }
65
66 // passphraseInput is run when the callback needs a passphrase from the user. We
67 // pass along the tokenToCheck without prompting. A return value of nil
68 // indicates an error occurred.
69 //
70 //export passphraseInput
71 func passphraseInput(prompt *C.char) *C.char {
72 log.Printf("getting secret data for PAM: %q", C.GoString(prompt))
73 if tokenToCheck == nil {
74 log.Print("secret data requested multiple times")
75 return nil
76 }
77
78 // Subsequent calls to passphrase input should fail
79 input := (*C.char)(tokenToCheck.UnsafeToCString())
80 tokenToCheck = nil
81 return input
82 }
83
84 // IsUserLoginToken returns nil if the presented token is the user's login key,
85 // and returns an error otherwise. Note that unless we are currently running as
86 // root, this check will only work for the user running this process.
87 func IsUserLoginToken(username string, token *crypto.Key, quiet bool) error {
88 log.Printf("Checking login token for %s", username)
89
90 // We require global state for the function. This function never takes
91 // ownership of the token, so it is not responsible for wiping it.
92 tokenLock.Lock()
93 tokenToCheck = token
94 defer func() {
95 tokenToCheck = nil
96 tokenLock.Unlock()
97 }()
98
99 transaction, err := Start("fscrypt", username)
100 if err != nil {
101 return err
102 }
103 defer transaction.End()
104
105 // Ask PAM to authenticate the token.
106 authenticated, err := transaction.Authenticate(quiet)
107 if err != nil {
108 return err
109 }
110
111 if !authenticated {
112 return ErrPassphrase
113 }
114 return nil
115 }