diff options
-rw-r--r-- | src/nshd/hackers_git/db_group.go | 129 |
1 files changed, 116 insertions, 13 deletions
diff --git a/src/nshd/hackers_git/db_group.go b/src/nshd/hackers_git/db_group.go index 8ce6fa3..2a9a9cf 100644 --- a/src/nshd/hackers_git/db_group.go +++ b/src/nshd/hackers_git/db_group.go @@ -1,30 +1,133 @@ package hackers_git -/* -import p "nslcd_proto" +import ( + p "nslcd_proto" + "nslcd_proto/util" +) + +func (o *Hackers) groupByName(name string, users bool) p.Group { + members_set, found := o.groups[name] + if !found { + return p.Group{ID: -1} + } + gid := name2gid(name) + if gid < 0 { + return p.Group{ID: -1} + } + var members_list []string + if users { + members_list = set2list(members_set) + } else { + members_list = make([]string, 0) + } + return p.Group{ + Name: name, + PwHash: "x", + ID: gid, + Members: members_list, + } +} + +func (o *Hackers) groupByGid(gid int32, users bool) p.Group { + name, found := gid2name(gid) + if !found { + return p.Group{ID: -1} + } + members_set, found := o.groups[name] + if !found { + return p.Group{ID: -1} + } + var members_list []string + if users { + members_list = set2list(members_set) + } else { + members_list = make([]string, 0) + } + return p.Group{ + Name: name, + PwHash: "x", + ID: gid, + Members: members_list, + } +} func (o *Hackers) Group_ByName(cred p.Ucred, req p.Request_Group_ByName) p.Group_Enumerator { o.lock.RLock() defer o.lock.RUnlock() - // TODO - return nil + group := o.groupByName(req.Name, true) + if group.ID < 0 { + return util.Group_Ø{} + } + return util.New_Group_List([]p.Group{group}) } + func (o *Hackers) Group_ByGid(cred p.Ucred, req p.Request_Group_ByGid) p.Group_Enumerator { o.lock.RLock() defer o.lock.RUnlock() - // TODO - return nil + group := o.groupByGid(req.Gid, true) + if group.ID < 0 { + return util.Group_Ø{} + } + return util.New_Group_List([]p.Group{group}) +} + +type groupEnumerator struct { + groups []string + users bool + backend *Hackers + done bool } + +func (e *groupEnumerator) GetNext() (*p.Group, error) { + for len(e.groups) > 0 { + name := e.groups[0] + group := e.backend.groupByName(name, e.users) + if group.ID >= 0 { + return &group, nil + } + } + if !e.done { + e.done = true + e.backend.lock.RUnlock() + } + return nil, nil +} + +func (o *groupEnumerator) GenericGetNext() (n *interface{}, err error) { + a, err := o.GetNext() + if a != nil { + b := (interface{})(*a) + n = &b + } + return +} + +// note that the BYMEMBER call returns an empty members list func (o *Hackers) Group_ByMember(cred p.Ucred, req p.Request_Group_ByMember) p.Group_Enumerator { o.lock.RLock() - defer o.lock.RUnlock() - // TODO - return nil + uid := o.name2uid(req.Member) + if uid < 0 { + return util.Group_Ø{} + } + return &groupEnumerator{ + groups: o.users[uid].groups, + users: false, + backend: o, + done: false, + } } + func (o *Hackers) Group_All(cred p.Ucred, req p.Request_Group_All) p.Group_Enumerator { o.lock.RLock() - defer o.lock.RUnlock() - // TODO - return nil + e := groupEnumerator{ + groups: make([]string, len(o.groups)), + users: true, + backend: o, + done: false, + } + i := uint(0) + for group, _ := range o.groups { + e.groups[i] = group + } + return &e } -*/ |