summaryrefslogtreecommitdiff
path: root/src/nshd/hackers_git/hackers.go
blob: f84b110aef230e3489d888310859efde21966fc9 (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
package hackers_git

import (
	"inotify"
	"nslcd_proto"
	"nslcd_proto/util"
	"nslcd_systemd"
	"sd_daemon/logger"
	"sd_daemon/lsb"
	"sync"
)

type user struct {
	passwd nslcd_proto.Passwd
	groups []string
}

type Config struct {
	Pam_password_prohibit_message string
	Yamldir                       string
}

type Hackers struct {
	util.NullBackend
	cfg     Config
	lock    sync.RWMutex
	workers sync.WaitGroup

	users  map[int32]user
	groups map[string]map[string]bool

	in_fd      *inotify.Inotify
	in_wd_home inotify.Wd
	in_wd_yaml inotify.Wd
	in_uid2wd  map[int32]inotify.Wd
	in_wd2uid  map[inotify.Wd]int32
}

var _ nslcd_systemd.Backend = &Hackers{}
var _ nslcd_proto.Backend = &Hackers{}

func NewHackers(config Config) *Hackers {
	o := Hackers{
		cfg: config,
	}
	err := o.Reload()
	if err != nil {
		logger.Err("Could not initialize hackers.git: %v", err)
		return nil
	}
	o.workers.Add(1)
	go func() {
		defer lsb.Recover()
		defer o.workers.Done()
		o.worker()
	}()
	return &o
}

func (o *Hackers) Close() {
	defer o.workers.Wait()
	logger.Info("Closing hackers.git session")
	o.lock.Lock()
	defer o.lock.Unlock()

	o.close()
}

func (o *Hackers) Reload() error {
	logger.Info("Loading hackers.git session")
	o.lock.Lock()
	defer o.lock.Unlock()

	return o.reload()
}

func (o *Hackers) name2uid(name string) int32 {
	for uid, data := range o.users {
		if data.passwd.Name == name {
			return uid
		}
	}
	return -1
}