diff options
Diffstat (limited to 'src/nshd')
-rw-r--r-- | src/nshd/hackers_git/hackers_parse.go | 43 | ||||
-rw-r--r-- | src/nshd/hackers_git/name2gid.go | 41 |
2 files changed, 81 insertions, 3 deletions
diff --git a/src/nshd/hackers_git/hackers_parse.go b/src/nshd/hackers_git/hackers_parse.go index efc30a5..5152b55 100644 --- a/src/nshd/hackers_git/hackers_parse.go +++ b/src/nshd/hackers_git/hackers_parse.go @@ -1,13 +1,50 @@ package hackers_git import ( - _ "gopkg.in/yaml.v2" + "fmt" + yaml "gopkg.in/yaml.v2" + "io/ioutil" "nslcd_proto" + "os" ) -func load_user_yaml(filename string) (nslcd_proto.Passwd, error) { +type yaml_user struct { + username string + fullname string + shell string + groups []string +} + +func filename2uid(filename string) int32 { // TODO - return nslcd_proto.Passwd{}, nil + return 0 +} + +func load_user_yaml(filename string) (ret nslcd_proto.Passwd, err error) { + ret.UID = filename2uid(filename) + if ret.UID < 0 { + err = fmt.Errorf("Invalid user filename: %q", filename) + return + } + file, err := os.Open(filename) + if err != nil { + return + } + contents, err := ioutil.ReadAll(file) + if err != nil { + return + } + var user yaml_user + err = yaml.Unmarshal(contents, &user) + + ret.Name = user.username + ret.Password = "x" + ret.GID = name2gid("users") + ret.GECOS = user.fullname + ret.HomeDir = "/home/" + ret.Name + ret.Shell = user.shell + + return } func load_user_password(filename string) string { diff --git a/src/nshd/hackers_git/name2gid.go b/src/nshd/hackers_git/name2gid.go new file mode 100644 index 0000000..b33e6aa --- /dev/null +++ b/src/nshd/hackers_git/name2gid.go @@ -0,0 +1,41 @@ +package hackers_git + +import ( + "syscall" + "unsafe" +) + +//#include <stdlib.h> /* free */ +//#include <unistd.h> /* sysconf */ +//#include <grp.h> /* getgrnam_r */ +import "C" + +func name2gid(name string) (gid int32) { + nameC := C.CString(name) + defer C.free(unsafe.Pointer(nameC)) + + gid = -1 + buflen := C.sysconf(C._SC_GETGR_R_SIZE_MAX) + if buflen < 1 { + buflen = 1024 + } + buf := make([]byte, buflen) + var grp C.struct_group + var ret *C.struct_group + for { + success, errno := C.getgrnam_r(nameC, &grp, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(buflen), &ret) + if success >= 0 { + break + } + if errno == syscall.ERANGE { + buflen += 256 + buf = make([]byte, buflen) + } else { + return + } + } + if ret != nil { + gid = int32(ret.gr_gid) + } + return +} |