func (t *srpcType) ChownDirectory(conn *srpc.Conn, request imageserver.ChangeOwnerRequest, reply *imageserver.ChangeOwnerResponse) error { username := conn.Username() if username == "" { return errors.New("no username: unauthenticated connection") } if request.OwnerGroup != "" { if _, err := user.LookupGroup(request.OwnerGroup); err != nil { return err } } t.logger.Printf("ChownDirectory(%s) to: \"%s\" by %s\n", request.DirectoryName, request.OwnerGroup, username) return t.imageDataBase.ChownDirectory(request.DirectoryName, request.OwnerGroup) }
// FindGroup will find the user-group given by name. func FindGroup(name string) (*Group, error) { g, err := user.LookupGroup(name) if err != nil { if _, ok := err.(user.UnknownGroupError); ok { return nil, ErrUserGroupNotFound } return nil, fmt.Errorf("Failed to lookup user-group, error: %s", err) } // Parse group id, which should always be an integer on POSIX systems. gid, err := strconv.Atoi(g.Gid) if err != nil { panic(fmt.Sprintf( "gid: '%s' for group: %s should have been an integer on POSIX system", g.Gid, name, )) } return &Group{gid}, nil }
func checkUserInGroup(username, ownerGroup string) error { userData, err := user.Lookup(username) if err != nil { return err } groupData, err := user.LookupGroup(ownerGroup) if err != nil { return err } groupIDs, err := userData.GroupIds() if err != nil { return err } for _, groupID := range groupIDs { if groupData.Gid == groupID { return nil } } return fmt.Errorf("user: %s not a member of: %s", username, ownerGroup) }
func LookupGroup(groupname string) (*user.Group, error) { var gr *user.Group var err error r := regexp.MustCompile(`^\d+$`) if r.MatchString(groupname) { gr, err = user.LookupGroupId(groupname) if err != nil { return nil, err } } else { gr, err = user.LookupGroup(groupname) if err != nil { return nil, err } } return gr, err }
// SetOwner sets the ownership for the file func (fu *FileUtil) SetOwner(owner, group string) error { o, err := user.Lookup(owner) if err != nil { return err } g, err := user.LookupGroup(group) if err != nil { return err } uid, err := strconv.Atoi(o.Uid) if err != nil { return err } gid, err := strconv.Atoi(g.Gid) if err != nil { return err } return os.Chown(fu.Path, uid, gid) }