summaryrefslogtreecommitdiff
path: root/src/parabola_hackers/nslcd_backend/db_pam.go
blob: 303a66c162d584e00e82286a35832329e837c824 (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
// Copyright 2015-2016 Luke Shumaker <lukeshu@sbcglobal.net>.
//
// 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 hackers_nslcd_backend

import (
	"parabola_hackers"
	s "syscall"

	"lukeshu.com/git/go/libgnulinux.git/crypt"
	p "lukeshu.com/git/go/libnslcd.git/proto"
)

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

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)

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

		user := o.users[uid]
		obj := p.PAM_Authentication{
			AuthenticationResult: p.NSLCD_PAM_AUTH_ERR,
			UserName:             "",
			AuthorizationResult:  p.NSLCD_PAM_AUTH_ERR,
			AuthorizationError:   "",
		}
		if checkPassword(req.Password, user.Passwd.PwHash) {
			obj.AuthenticationResult = p.NSLCD_PAM_SUCCESS
			obj.AuthorizationResult = obj.AuthenticationResult
			obj.UserName = user.Passwd.Name
		}
		ret <- obj
	}()
	return ret
}

func (o *Hackers) PAM_Authorization(cred s.Ucred, req p.Request_PAM_Authorization) <-chan p.PAM_Authorization {
	o.lock.RLock()
	ret := make(chan p.PAM_Authorization)
	go func() {
		defer o.lock.RUnlock()
		defer close(ret)

		uid := o.name2uid(req.UserName)
		if uid < 0 {
			return
		}
		ret <- p.PAM_Authorization{
			Result: p.NSLCD_PAM_SUCCESS,
			Error:  "",
		}
	}()
	return ret
}

const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

func (o *Hackers) PAM_SessionOpen(cred s.Ucred, req p.Request_PAM_SessionOpen) <-chan p.PAM_SessionOpen {
	ret := make(chan p.PAM_SessionOpen)
	go func() {
		defer close(ret)

		sessionid, err := parabola_hackers.RandomString(alphabet, 24)
		if err != nil {
			return
		}
		ret <- p.PAM_SessionOpen{SessionID: sessionid}
	}()
	return ret
}

func (o *Hackers) PAM_SessionClose(cred s.Ucred, req p.Request_PAM_SessionClose) <-chan p.PAM_SessionClose {
	ret := make(chan p.PAM_SessionClose)
	go close(ret)
	return ret
}