From 0d03ce4160324bdb80cc69f05b0181da9d43f172 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Thu, 28 Jul 2016 19:41:08 +0200 Subject: [PATCH] Allow `.` in the usernames of create-user LP: #1603018 --- osutil/user.go | 10 +++++++++- osutil/user_test.go | 7 ++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/osutil/user.go b/osutil/user.go index 7c61ea91bb6..89d00103531 100644 --- a/osutil/user.go +++ b/osutil/user.go @@ -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) } diff --git a/osutil/user_test.go b/osutil/user_test.go index 670989ea14f..6779dd3206e 100644 --- a/osutil/user_test.go +++ b/osutil/user_test.go @@ -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`) +}