2015-01-25 06:32:33 +03:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-10-18 02:36:22 +04:00
|
|
|
|
2014-03-18 20:00:41 +04:00
|
|
|
package system
|
2014-03-13 21:56:59 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
2015-03-28 20:08:30 +03:00
|
|
|
"github.com/vtolstov/cloudinit/config"
|
2014-09-22 03:33:01 +04:00
|
|
|
)
|
2014-03-13 21:56:59 +04:00
|
|
|
|
2015-03-28 20:04:09 +03:00
|
|
|
func UserHome(name string) (string, error) {
|
|
|
|
output, err := exec.Command("getent", "passwd", name).CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
passwd := strings.Split(output, ":")
|
|
|
|
return passwd[5], nil
|
|
|
|
}
|
|
|
|
|
2014-09-22 03:33:01 +04:00
|
|
|
func UserExists(u *config.User) bool {
|
2015-03-28 20:04:09 +03:00
|
|
|
return exec.Command("getent", "shadow", u.Name).Run() == nil
|
2014-03-13 21:56:59 +04:00
|
|
|
}
|
|
|
|
|
2014-09-22 03:33:01 +04:00
|
|
|
func CreateUser(u *config.User) error {
|
2014-03-13 21:56:59 +04:00
|
|
|
args := []string{}
|
|
|
|
|
|
|
|
if u.PasswordHash != "" {
|
|
|
|
args = append(args, "--password", u.PasswordHash)
|
2014-04-01 09:20:02 +04:00
|
|
|
} else {
|
|
|
|
args = append(args, "--password", "*")
|
2014-03-13 21:56:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if u.GECOS != "" {
|
|
|
|
args = append(args, "--comment", fmt.Sprintf("%q", u.GECOS))
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Homedir != "" {
|
|
|
|
args = append(args, "--home-dir", u.Homedir)
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.NoCreateHome {
|
|
|
|
args = append(args, "--no-create-home")
|
|
|
|
} else {
|
|
|
|
args = append(args, "--create-home")
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.PrimaryGroup != "" {
|
2014-05-08 01:06:51 +04:00
|
|
|
args = append(args, "--gid", u.PrimaryGroup)
|
2014-03-13 21:56:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(u.Groups) > 0 {
|
|
|
|
args = append(args, "--groups", strings.Join(u.Groups, ","))
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.NoUserGroup {
|
|
|
|
args = append(args, "--no-user-group")
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.System {
|
|
|
|
args = append(args, "--system")
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.NoLogInit {
|
|
|
|
args = append(args, "--no-log-init")
|
|
|
|
}
|
|
|
|
|
|
|
|
args = append(args, u.Name)
|
|
|
|
|
|
|
|
output, err := exec.Command("useradd", args...).CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Command 'useradd %s' failed: %v\n%s", strings.Join(args, " "), err, output)
|
2015-03-26 10:23:27 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-26 11:53:54 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func LockUnlockUser(u *config.User) error {
|
|
|
|
args := []string{}
|
2015-03-26 10:23:27 +03:00
|
|
|
|
|
|
|
if u.LockPasswd {
|
|
|
|
args = append(args, "--lock")
|
|
|
|
} else {
|
|
|
|
args = append(args, "--unlock")
|
|
|
|
}
|
|
|
|
|
|
|
|
args = append(args, u.Name)
|
|
|
|
|
2015-03-26 11:53:54 +03:00
|
|
|
output, err := exec.Command("passwd", args...).CombinedOutput()
|
2015-03-26 10:23:27 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Command 'passwd %s' failed: %v\n%s", strings.Join(args, " "), err, output)
|
2014-03-13 21:56:59 +04:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetUserPassword(user, hash string) error {
|
2015-03-26 11:53:54 +03:00
|
|
|
cmd := exec.Command("chpasswd", "-e")
|
2014-03-13 21:56:59 +04:00
|
|
|
|
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arg := fmt.Sprintf("%s:%s", user, hash)
|
|
|
|
_, err = stdin.Write([]byte(arg))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stdin.Close()
|
|
|
|
|
|
|
|
err = cmd.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|