diff options
Diffstat (limited to 'src/nshd')
-rw-r--r-- | src/nshd/hackers_git/hackers_parse.go | 45 | ||||
-rw-r--r-- | src/nshd/hackers_git/hackers_watch.go | 24 |
2 files changed, 54 insertions, 15 deletions
diff --git a/src/nshd/hackers_git/hackers_parse.go b/src/nshd/hackers_git/hackers_parse.go index 8f93432..b0ed91b 100644 --- a/src/nshd/hackers_git/hackers_parse.go +++ b/src/nshd/hackers_git/hackers_parse.go @@ -5,11 +5,23 @@ import ( yaml "gopkg.in/yaml.v2" "io/ioutil" "os" + "path" + "sd_daemon/logger" + "strconv" + "strings" ) func filename2uid(filename string) int32 { - // TODO - return 0 + basename := path.Base(filename) + parts := strings.SplitN(basename, ".", 2) + if len(parts) != 2 || parts[1] != ".yml" { + return -1 + } + uid, err := strconv.ParseInt(parts[0], 10, 32) + if err != nil { + return -1 + } + return int32(uid) } var usersGid = name2gid("users") @@ -96,7 +108,30 @@ func load_user_yaml(filename string) (ret user, err error) { return } -func load_user_password(filename string) string { - // TODO - return "!" +func load_user_password(filename string) (hash string) { + hash = "!" + file, err := os.Open(filename) + if err != nil { + logger.Info("Could not open: %q: %v", filename, err) + return + } + contents, err := ioutil.ReadAll(file) + if err != nil { + logger.Info("Error while reading: %q: %v", filename, err) + return + } + lines := strings.Split(string(contents), "\n") + switch len(lines) { + case 1: + hash = lines[0] + case 2: + if lines[1] == "" { + hash = lines[0] + } else { + logger.Info("Invalid password format in file: %q", filename) + } + default: + logger.Info("Invalid password format in file: %q", filename) + } + return } diff --git a/src/nshd/hackers_git/hackers_watch.go b/src/nshd/hackers_git/hackers_watch.go index be4af40..1b4f08d 100644 --- a/src/nshd/hackers_git/hackers_watch.go +++ b/src/nshd/hackers_git/hackers_watch.go @@ -72,20 +72,20 @@ func (o *Hackers) reload() (err error) { o.in_wd_home, err = o.in_fd.AddWatch("/home" , in_DIR|in_CHILD_ADD); if err != nil { return } o.in_wd_yaml, err = o.in_fd.AddWatch(o.cfg.Yamldir, in_DIR|in_CHILD_ANY); if err != nil { return } - files, err := filepath.Glob(o.cfg.Yamldir + "/*.yml") - o.users = make(map[int32]user, len(files)) - o.in_uid2wd = make(map[int32]inotify.Cint, len(files)) - o.in_wd2uid = make(map[inotify.Cint]int32, len(files)) - for _, file := range files { - logger.Debug("Loading yaml file: %s", file) - user, err := load_user_yaml(file) + filenames, err := filepath.Glob(o.cfg.Yamldir + "/*.yml") + o.users = make(map[int32]user, len(filenames)) + o.in_uid2wd = make(map[int32]inotify.Cint, len(filenames)) + o.in_wd2uid = make(map[inotify.Cint]int32, len(filenames)) + for _, filename := range filenames { + logger.Info("Loading yaml file: %s", filename) + user, err := load_user_yaml(filename) if err == nil { o.users[user.passwd.UID] = user - logger.Debug("... success") + logger.Info("... success") o.watchHomedir(user.passwd.UID) o.passwords[user.passwd.UID] = load_user_password(user.passwd.HomeDir + "/.password") } else { - logger.Debug("... error") + logger.Info("... error: %v", err) } } @@ -151,12 +151,16 @@ func (o *Hackers) worker() { worker_error("failed to load %q: %v", o.cfg.Yamldir, err) } } else if event.Mask&in_CHILD_ANY != 0 { - user, err := load_user_yaml(*event.Name) + filename := o.cfg.Yamldir + "/" + *event.Name + logger.Info("Loading yaml file: %s", filename) + user, err := load_user_yaml(filename) if err == nil { // User added/updated + logger.Info("... success") o.worker_handle_user_add(user) } else if user.passwd.UID >= 0 { // User became invalid + logger.Info("... error: %v", err) o.worker_handle_user_del(user.passwd.UID) } } else { |