Skip to content

Commit

Permalink
Allow . in the usernames of create-user
Browse files Browse the repository at this point in the history
LP: #1603018
  • Loading branch information
mvo5 committed Jul 28, 2016
1 parent 0c40ad7 commit 0d03ce4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 9 additions & 1 deletion osutil/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ import (
"os/exec"
"os/user"
"path/filepath"
"regexp"
"strings"
)

var userLookup = user.Lookup

func AddExtraUser(name string, sshKeys []string, gecos string) error {
cmd := exec.Command("adduser", "--gecos", gecos, "--extrausers", "--disabled-password", name)
// we check the (user)name ourselfs, adduser is a bit too
// strict (i.e. no `.`)
validNames := regexp.MustCompile(`^[a-z][-a-z0-9_.]*$`)
if !validNames.MatchString(name) {
return fmt.Errorf("cannot add user %q: name contains invalid charackters", name)
}

cmd := exec.Command("adduser", "--force-badname", "--gecos", gecos, "--extrausers", "--disabled-password", name)
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("adduser failed with %s: %s", err, output)
}
Expand Down
7 changes: 6 additions & 1 deletion osutil/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ func (s *createUserSuite) TestAddExtraUser(c *check.C) {
err := osutil.AddExtraUser("karl", []string{"ssh-key1", "ssh-key2"}, "my gecos")
c.Assert(err, check.IsNil)
c.Check(mc.Calls(), check.DeepEquals, [][]string{
{"adduser", "--gecos", "my gecos", "--extrausers", "--disabled-password", "karl"},
{"adduser", "--force-badname", "--gecos", "my gecos", "--extrausers", "--disabled-password", "karl"},
})
sshKeys, err := ioutil.ReadFile(filepath.Join(mockHome, ".ssh", "authorized_keys"))
c.Assert(err, check.IsNil)
c.Check(string(sshKeys), check.Equals, "ssh-key1\nssh-key2")
}

func (s *createUserSuite) TestAddExtraUserInvalid(c *check.C) {
err := osutil.AddExtraUser("k!", nil, "my gecos")
c.Assert(err, check.ErrorMatches, `cannot add user "k!": name contains invalid charackters`)
}

0 comments on commit 0d03ce4

Please sign in to comment.