ea1e4c38fa
This reverts commit cdfc94f4e965adf0eb2e29590b84be66f62b3199, reversing changes made to 2051cd3e1ca6ca825fd650646b6718ee05cf3eda. Conflicts: config/config.go config/config_test.go config/etc_hosts.go config/etcd.go config/file.go config/fleet.go config/oem.go config/unit.go config/update.go config/user.go initialize/config.go initialize/config_test.go initialize/env.go initialize/manage_etc_hosts.go initialize/workspace.go system/env.go system/etc_hosts_test.go system/etcd.go system/etcd_test.go system/fleet.go system/fleet_test.go system/oem.go system/oem_test.go system/systemd.go system/update.go system/update_test.go test
127 lines
2.8 KiB
Go
127 lines
2.8 KiB
Go
/*
|
|
Copyright 2014 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.
|
|
*/
|
|
|
|
package system
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os/exec"
|
|
"os/user"
|
|
"strings"
|
|
)
|
|
|
|
type User struct {
|
|
Name string `yaml:"name"`
|
|
PasswordHash string `yaml:"passwd"`
|
|
SSHAuthorizedKeys []string `yaml:"ssh-authorized-keys"`
|
|
SSHImportGithubUser string `yaml:"coreos-ssh-import-github"`
|
|
SSHImportURL string `yaml:"coreos-ssh-import-url"`
|
|
GECOS string `yaml:"gecos"`
|
|
Homedir string `yaml:"homedir"`
|
|
NoCreateHome bool `yaml:"no-create-home"`
|
|
PrimaryGroup string `yaml:"primary-group"`
|
|
Groups []string `yaml:"groups"`
|
|
NoUserGroup bool `yaml:"no-user-group"`
|
|
System bool `yaml:"system"`
|
|
NoLogInit bool `yaml:"no-log-init"`
|
|
}
|
|
|
|
func UserExists(u *User) bool {
|
|
_, err := user.Lookup(u.Name)
|
|
return err == nil
|
|
}
|
|
|
|
func CreateUser(u *User) error {
|
|
args := []string{}
|
|
|
|
if u.PasswordHash != "" {
|
|
args = append(args, "--password", u.PasswordHash)
|
|
} else {
|
|
args = append(args, "--password", "*")
|
|
}
|
|
|
|
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 != "" {
|
|
args = append(args, "--gid", u.PrimaryGroup)
|
|
}
|
|
|
|
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)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func SetUserPassword(user, hash string) error {
|
|
cmd := exec.Command("/usr/sbin/chpasswd", "-e")
|
|
|
|
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
|
|
}
|