summaryrefslogtreecommitdiff
path: root/go/src/nshd/nslcd_backend/db_pam.go
blob: 96a5567a9b8df8c55d44b2df6c091ea4927864df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2015-2017 Luke Shumaker <git.lukeshu@sbcglobal>.
//
// This is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this manual; if not, see
// <http://www.gnu.org/licenses/>.

package nslcd_backend

import (
	"fmt"
	"os"

	"nshd/nshd_files"
	"nshd/util"

	p "git.lukeshu.com/go/libnslcd/nslcd_proto"
	s "golang.org/x/sys/unix"

	"git.lukeshu.com/go/libgnulinux/crypt"
	"git.lukeshu.com/go/libsystemd/sd_daemon"
)

func checkPassword(password string, hash string) bool {
	return crypt.Crypt(password, hash) == hash
}

func hashPassword(newPassword string, oldHash string) string {
	salt := oldHash
	if salt == "!" {
		str, err := util.RandomString(crypt.SaltAlphabet, 8)
		if err != nil {
			sd_daemon.Log.Err("Could not generate a random string")
			str = ""
		}
		// "$6$" is SHA-512; see crypt(3) for others
		salt = "$6$" + str + "$"
	}
	return crypt.Crypt(newPassword, salt)
}

func dirExists(path string) bool {
	stat, err := os.Stat(path)
	if err != nil {
		return false
	}
	return stat.IsDir()
}

func (o *Hackers) canChangePassword(user nshd_files.User, oldpassword string) bool {
	// special hack: if the old password is not
	// set, but the home directory exists, let the
	// user set their password
	if user.Passwd.PwHash == "!" && dirExists(user.Passwd.HomeDir) {
		return true
	}

	return checkPassword(oldpassword, user.Passwd.PwHash)
}

// We don't actually use this for authentication (we let pam_unix.so
// call NSS getspnam(3), which will call our Shadow_ByName()), but
// pam_ldap.so calls this as a pre-flight check for
// pam_sm_chauthtok()/PAM_PwMod().
func (o *Hackers) PAM_Authentication(cred s.Ucred, req p.Request_PAM_Authentication) <-chan p.PAM_Authentication {
	o.lock.RLock()
	ret := make(chan p.PAM_Authentication)
	go func() {
		defer o.lock.RUnlock()
		defer close(ret)

		if len(req.UserName) == 0 && len(req.Password) == 0 && cred.Uid == 0 {
			// Being called by root; root can do what root
			// wants.
			ret <- p.PAM_Authentication{
				AuthenticationResult: p.NSLCD_PAM_SUCCESS,
				UserName:             "",
				AuthorizationResult:  p.NSLCD_PAM_SUCCESS,
				AuthorizationError:   "",
			}
			return
		}

		uid := o.name2uid(req.UserName)
		if uid < 0 {
			return
		}
		user := o.users[uid]

		if o.canChangePassword(user, req.Password) {
			// Success
			ret <- p.PAM_Authentication{
				AuthenticationResult: p.NSLCD_PAM_SUCCESS,
				UserName:             user.Passwd.Name,
				AuthorizationResult:  p.NSLCD_PAM_SUCCESS,
				AuthorizationError:   "",
			}
			return
		} else {
			// Failure
			ret <- p.PAM_Authentication{
				AuthenticationResult: p.NSLCD_PAM_AUTH_ERR,
				UserName:             "",
				AuthorizationResult:  p.NSLCD_PAM_AUTH_ERR,
				AuthorizationError:   fmt.Sprintf("password change failed: %s", "Old password did not match"),
			}
			return
		}
	}()
	return ret
}

func (o *Hackers) PAM_PwMod(cred s.Ucred, req p.Request_PAM_PwMod) <-chan p.PAM_PwMod {
	ret := make(chan p.PAM_PwMod)
	o.lock.Lock()
	go func() {
		defer o.lock.Unlock()
		defer close(ret)

		uid := o.name2uid(req.UserName)
		if uid < 0 {
			return
		}
		user := o.users[uid]

		// Check the OldPassword
		if req.AsRoot == 1 && cred.Uid == 0 {
			goto update
		}
		if !o.canChangePassword(user, req.OldPassword) {
			ret <- p.PAM_PwMod{
				Result: p.NSLCD_PAM_PERM_DENIED,
				Error:  fmt.Sprintf("password change failed: %s", "Old password did not match"),
			}
			return
		}
	update:
		if len(req.NewPassword) == 0 {
			ret <- p.PAM_PwMod{
				Result: p.NSLCD_PAM_PERM_DENIED,
				Error:  "password cannot be empty",
			}
			return
		}

		// Update the PwHash in memory
		user.Passwd.PwHash = hashPassword(req.NewPassword, user.Passwd.PwHash)
		if len(user.Passwd.PwHash) == 0 {
			sd_daemon.Log.Err("Password hashing failed")
			return
		}

		// Update the PwHash on disk
		passwords := make(map[string]string, len(o.users))
		for _, ouser := range o.users {
			passwords[ouser.Passwd.Name] = ouser.Passwd.PwHash
		}
		passwords[user.Passwd.Name] = user.Passwd.PwHash
		err := nshd_files.SaveAllPasswords(passwords)
		if err != nil {
			sd_daemon.Log.Err(fmt.Sprintf("Writing passwords to disk: %v", err))
			return
		}

		// Ok, we're done, commit the changes
		o.users[uid] = user
		ret <- p.PAM_PwMod{
			Result: p.NSLCD_PAM_SUCCESS,
			Error:  "",
		}
	}()
	return ret
}