Exemple #1
0
func (u *User) ensureAbsent(result *task.Result) (*task.Result, error) {
	var err error
	oldUser, err := unixuser.Lookup(u.Name, nil)
	if err != nil {
		if _, ok := err.(osuser.UnknownUserError); ok {
			err = nil
			result.Skipped = true
		}
		return result, err
	}

	result.Extra["old_uid"], err = strconv.Atoi(oldUser.Gid)
	if err != nil {
		return result, err
	}

	args := make([]string, 0)
	if u.RemovesHome {
		args = append(args, "-r")
	}
	args = append(args, u.Name)

	err = result.ExecCommand("userdel", args...)
	return result, err
}
Exemple #2
0
func (d *Directory) ensurePresent(result *task.Result) (*task.Result, error) {
	fi, err := os.Lstat(d.Path)
	if err != nil {
		if isNoSuchFileOrDirectory(err) {
			err = os.MkdirAll(d.Path, d.Mode)
			if err == nil {
				result.Changed = true
			}
		}
	} else {
		if fi.IsDir() {
			oldMode := fi.Mode() & os.ModePerm
			if oldMode == d.Mode {
				result.Skipped = true
			} else {
				result.Extra["old_mode"] = fmt.Sprintf("%o", oldMode)
				err = os.Chmod(d.Path, d.Mode)
				if err == nil {
					result.Changed = true
				}
			}
		} else {
			err = errors.New("something not directory exists or permission denied")
		}
	}
	return result, err
}
Exemple #3
0
func (d *Directory) ensureAbsent(result *task.Result) (*task.Result, error) {
	fi, err := os.Lstat(d.Path)
	if err != nil {
		if isNoSuchFileOrDirectory(err) {
			result.Skipped = true
			err = nil
		}
	} else {
		if fi.IsDir() {
			err = os.RemoveAll(d.Path)
			if err == nil {
				result.Changed = true
			}
		} else {
			err = errors.New("something not directory exists or permission denied")
		}
	}
	return result, err
}
Exemple #4
0
func (g *Group) ensureAbsent(result *task.Result) (*task.Result, error) {
	var err error
	oldGroup, err := unixgroup.Lookup(g.Name, nil)
	if err != nil {
		if _, ok := err.(unixgroup.UnknownGroupError); ok {
			err = nil
			result.Skipped = true
		}
		return result, err
	}

	result.Extra["old_gid"], err = strconv.Atoi(oldGroup.Gid)
	if err != nil {
		return result, err
	}

	err = result.ExecCommand("groupdel", g.Name)
	return result, err
}
Exemple #5
0
func (g *Group) ensurePresent(result *task.Result) (*task.Result, error) {
	var gidStr string
	if g.Gid != AutoGID {
		gidStr = strconv.Itoa(g.Gid)
	}

	var command string
	var err error
	oldGroup, err := unixgroup.Lookup(g.Name, nil)
	if err != nil {
		if _, ok := err.(unixgroup.UnknownGroupError); !ok {
			return result, err
		}

		result.Op = "create"
		command = "groupadd"
	} else {
		if gidStr == "" || gidStr == oldGroup.Gid {
			result.Skipped = true
			return result, err
		}

		result.Extra["old_gid"], err = strconv.Atoi(oldGroup.Gid)
		if err != nil {
			return result, err
		}

		result.Op = "modify"
		command = "groupmod"
	}

	args := make([]string, 0)
	if gidStr != "" {
		args = append(args, "-g", gidStr)
	}
	if g.System {
		args = append(args, "-r")
	}
	args = append(args, g.Name)

	err = result.ExecCommand(command, args...)
	return result, err
}
Exemple #6
0
func (u *User) ensurePresent(result *task.Result) (*task.Result, error) {
	var uidStr string
	if u.Uid != AutoUID {
		uidStr = strconv.Itoa(u.Uid)
	}

	var command string
	args := make([]string, 0)
	var err error
	oldUser, err := unixuser.Lookup(u.Name, nil)
	if err != nil {
		if _, ok := err.(osuser.UnknownUserError); !ok {
			return result, err
		}

		result.Op = "create"
		command = "useradd"
	} else {
		var allGroups []*unixgroup.Group
		allGroups, err = unixgroup.AllGroups()
		if err != nil {
			return result, err
		}

		var oldGroup *unixgroup.Group
		oldGroup, err = unixgroup.LookupId(oldUser.Gid, allGroups)
		if err != nil {
			return result, err
		}

		var oldGroups []string
		oldGroups, err = unixgroup.SupplementaryGroups(u.Name, allGroups)
		if err != nil {
			return result, err
		}

		var groupsWillChange bool
		if u.Appends {
			groupsWillChange = !stringutil.ArrayContainsAll(oldGroups, u.Groups)
		} else {
			groupsWillChange = !stringutil.SetEqual(oldGroups, u.Groups)
		}

		if (uidStr == "" || uidStr == oldUser.Uid) &&
			(u.Group == "" || u.Group == oldUser.Gid || u.Group == oldGroup.Name) &&
			!groupsWillChange &&
			(u.Comment == "" || u.Comment == oldUser.Name) &&
			(u.HomeDir == "" || u.HomeDir == oldUser.HomeDir) &&
			(u.Shell == "" || u.Shell == oldUser.Shell) {
			result.Skipped = true
			return result, err
		}

		if uidStr != "" && uidStr != oldUser.Uid {
			result.Extra["old_uid"], err = strconv.Atoi(oldUser.Uid)
			if err != nil {
				return result, err
			}
		}

		if u.Group != "" && u.Group != oldUser.Gid && u.Group != oldGroup.Name {
			result.Extra["old_gid"], err = strconv.Atoi(oldUser.Gid)
			if err != nil {
				return result, err
			}
		}

		if groupsWillChange {
			result.Extra["old_u.Groups"] = oldGroups
		}

		result.Op = "modify"
		command = "usermod"

		if u.Appends && len(u.Groups) > 0 {
			args = append(args, "-a")
		}
	}
	if uidStr != "" {
		args = append(args, "-u", uidStr)
	}
	if u.Group != "" {
		args = append(args, "-g", u.Group)
	}
	if len(u.Groups) > 0 {
		args = append(args, "-G", strings.Join(u.Groups, ","))
	}
	if u.System {
		args = append(args, "-r")
	}
	if u.Comment != "" {
		args = append(args, "-c", u.Comment)
	}
	if u.HomeDir != "" {
		args = append(args, "-d", u.HomeDir)
	}
	if u.Shell != "" {
		args = append(args, "-s", u.Shell)
	}
	args = append(args, u.Name)

	err = result.ExecCommand(command, args...)
	return result, err
}