diff options
-rw-r--r-- | src/nshd/hackers_git/hackers_parse.go | 4 | ||||
-rw-r--r-- | src/nshd/hackers_git/hackers_watch.go | 86 |
2 files changed, 37 insertions, 53 deletions
diff --git a/src/nshd/hackers_git/hackers_parse.go b/src/nshd/hackers_git/hackers_parse.go index a9a03f3..858347e 100644 --- a/src/nshd/hackers_git/hackers_parse.go +++ b/src/nshd/hackers_git/hackers_parse.go @@ -26,7 +26,7 @@ func filename2uid(filename string) int32 { var usersGid = name2gid("users") -func load_user_yaml(filename string) (ret user, err error) { +func parse_user_yaml(filename string) (ret user, err error) { ret.passwd.UID = filename2uid(filename) if ret.passwd.UID < 0 { @@ -107,7 +107,7 @@ func load_user_yaml(filename string) (ret user, err error) { return } -func load_user_password(filename string) (hash string) { +func parse_user_password(filename string) (hash string) { hash = "!" file, err := os.Open(filename) if err != nil { diff --git a/src/nshd/hackers_git/hackers_watch.go b/src/nshd/hackers_git/hackers_watch.go index b273741..afae203 100644 --- a/src/nshd/hackers_git/hackers_watch.go +++ b/src/nshd/hackers_git/hackers_watch.go @@ -31,9 +31,7 @@ func (o *Hackers) watchHomedir(uid int32) { delete(o.in_uid2wd, uid) logger.Debug("hackers.git: %v", err) } - user := o.users[uid] - user.passwd.PwHash = load_user_password(user.passwd.HomeDir + "/.password") - o.users[uid] = user + o.load_user_password(uid) } func (o *Hackers) unwatchHomedir(wd inotify.Wd) { @@ -45,9 +43,7 @@ func (o *Hackers) unwatchHomedir(wd inotify.Wd) { uid := o.in_wd2uid[wd] delete(o.in_wd2uid, wd) delete(o.in_uid2wd, uid) - user := o.users[uid] - user.passwd.PwHash = load_user_password(user.passwd.HomeDir + "/.password") - o.users[uid] = user + o.load_user_password(uid) } func (o *Hackers) close() { @@ -72,38 +68,41 @@ func (o *Hackers) reload() (err error) { o.in_uid2wd = make(map[int32]inotify.Wd, len(filenames)) o.in_wd2uid = make(map[inotify.Wd]int32, len(filenames)) for _, filename := range filenames { - logger.Debug("hackers.git: Loading yaml file: %s", filename) - user, err := load_user_yaml(filename) - if err == nil { - o.users[user.passwd.UID] = user - logger.Debug("hackers.git: ... success") - o.watchHomedir(user.passwd.UID) - } else { - logger.Debug("hackers.git: ... error: %v", err) - } + o.load_yaml_file(filename) } err = nil return } -func (o *Hackers) worker_handle_user_add(user user) { - o.lock.Lock() - defer o.lock.Unlock() - - o.users[user.passwd.UID] = user - o.watchHomedir(user.passwd.UID) +func (o *Hackers) load_yaml_file(filename string) { + logger.Debug("hackers.git: Loading yaml file: %s", filename) + user, err := parse_user_yaml(filename) + if err == nil { + // User added/updated + logger.Debug("hackers.git: ... success") + o.lock.Lock() + defer o.lock.Unlock() + o.users[user.passwd.UID] = user + o.watchHomedir(user.passwd.UID) + } else if user.passwd.UID >= 0 { + // User became invalid + logger.Debug("hackers.git: ... error: %v", err) + uid := user.passwd.UID + o.lock.Lock() + defer o.lock.Unlock() + wd, found := o.in_uid2wd[uid] + if found { + o.unwatchHomedir(wd) + } + delete(o.users, uid) + } } -func (o *Hackers) worker_handle_user_del(uid int32) { - o.lock.Lock() - defer o.lock.Unlock() - - wd, found := o.in_uid2wd[uid] - if found { - o.unwatchHomedir(wd) - } - delete(o.users, uid) +func (o *Hackers) load_user_password(uid int32) { + user := o.users[uid] + user.passwd.PwHash = parse_user_password(user.passwd.HomeDir + "/.password") + o.users[uid] = user } func (o *Hackers) worker_watch_homedirs() { @@ -112,15 +111,6 @@ func (o *Hackers) worker_watch_homedirs() { } } -func (o *Hackers) worker_handle_passwd(uid int32) { - o.lock.Lock() - defer o.lock.Unlock() - - user := o.users[uid] - user.passwd.PwHash = load_user_password(o.users[uid].passwd.HomeDir + "/.password") - o.users[uid] = user -} - func worker_error(format string, a ...interface{}) { logger.Err("hackers.git: "+ format, a) os.Exit(int(lsb.EXIT_FAILURE)) @@ -150,17 +140,7 @@ func (o *Hackers) worker() { panic("recieved child event from inotify, but no child name") } filename := o.cfg.Yamldir + "/" + *event.Name - logger.Debug("hackers.git: Loading yaml file: %s", filename) - user, err := load_user_yaml(filename) - if err == nil { - // User added/updated - logger.Debug("hackers.git: ... success") - o.worker_handle_user_add(user) - } else if user.passwd.UID >= 0 { - // User became invalid - logger.Debug("hackers.git: ... error: %v", err) - o.worker_handle_user_del(user.passwd.UID) - } + o.load_yaml_file(filename) } else { panic("recieved non-subscribed inotify event from kernel") } @@ -181,7 +161,11 @@ func (o *Hackers) worker() { o.worker_watch_homedirs() } else if event.Name != nil { if *event.Name == ".password" { - o.worker_handle_passwd(o.in_wd2uid[event.Wd]) + func() { + o.lock.Lock() + defer o.lock.Unlock() + o.load_user_password(o.in_wd2uid[event.Wd]) + }() } } else { logger.Debug("hackers.git: event didn't match: %#v", event) |