This repository was archived by the owner on Feb 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 881
stage1,tests: add support for non-numerical uid/gid #2159
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
965446a
pkg: new passwd package
monstermunchkin 2edfc49
pkg/group: allow lookup from specified file
monstermunchkin 9bf0d1f
pkg/group: refactor parseGroupLine function
monstermunchkin fa8d2f3
pkg/uid: add function to unshift uid/gid
monstermunchkin 4213490
stage1: support non-numerical uid/gid
monstermunchkin d9bd82e
CHANGELOG: add entry for non-numerical UID/GID
monstermunchkin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| // Copyright 2016 The rkt Authors | ||
| // | ||
| // 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 passwd | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/hashicorp/errwrap" | ||
| ) | ||
|
|
||
| const ( | ||
| passwdFilePath = "/etc/passwd" | ||
| ) | ||
|
|
||
| // User represents an entry in the passwd file. | ||
| type User struct { | ||
| Name string | ||
| Pass string | ||
| Uid int | ||
| Gid int | ||
| Comment string | ||
| Home string | ||
| Interpreter string | ||
| } | ||
|
|
||
| // LookupUidFromFile reads the passwd file specified by passwdFile, and returns the | ||
| // uid of the user specified by userName. | ||
| func LookupUidFromFile(userName, passwdFile string) (uid int, err error) { | ||
| users, err := parsePasswdFile(passwdFile) | ||
| if err != nil { | ||
| return -1, errwrap.Wrap(fmt.Errorf("error parsing %q file", passwdFile), err) | ||
| } | ||
|
|
||
| user, ok := users[userName] | ||
| if !ok { | ||
| return -1, fmt.Errorf("%q user not found", userName) | ||
| } | ||
|
|
||
| return user.Uid, nil | ||
| } | ||
|
|
||
| // LookupUid reads the passwd file and returns the uid of the user | ||
| // specified by userName. | ||
| func LookupUid(userName string) (uid int, err error) { | ||
| return LookupUidFromFile(userName, passwdFilePath) | ||
| } | ||
|
|
||
| func parsePasswdFile(path string) (user map[string]*User, err error) { | ||
| passwdFile, err := os.Open(path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer passwdFile.Close() | ||
|
|
||
| return parseUsers(passwdFile) | ||
| } | ||
|
|
||
| func parseUsers(r io.Reader) (user map[string]*User, err error) { | ||
| s := bufio.NewScanner(r) | ||
| out := make(map[string]*User) | ||
|
|
||
| for s.Scan() { | ||
| if err := s.Err(); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| text := s.Text() | ||
| if text == "" { | ||
| continue | ||
| } | ||
|
|
||
| p, err := parsePasswdLine(text) | ||
| if err != nil { | ||
| return nil, errwrap.Wrap(errors.New("error parsing line"), err) | ||
| } | ||
|
|
||
| out[p.Name] = p | ||
| } | ||
|
|
||
| return out, nil | ||
| } | ||
|
|
||
| func parsePasswdLine(line string) (*User, error) { | ||
| const ( | ||
| NameIdx = iota | ||
| PassIdx | ||
| UidIdx | ||
| GidIdx | ||
| CommentIdx | ||
| HomeIdx | ||
| InterpreterIdx | ||
| ) | ||
| var err error | ||
|
|
||
| if line == "" { | ||
| return nil, errors.New("cannot parse empty line") | ||
| } | ||
|
|
||
| splits := strings.Split(line, ":") | ||
| if len(splits) < 7 { | ||
| return nil, fmt.Errorf("expected at least 7 fields, got %d", len(splits)) | ||
| } | ||
|
|
||
| user := &User{ | ||
| Name: splits[NameIdx], | ||
| Pass: splits[PassIdx], | ||
| Comment: splits[CommentIdx], | ||
| Home: splits[HomeIdx], | ||
| Interpreter: splits[InterpreterIdx], | ||
| } | ||
|
|
||
| user.Uid, err = strconv.Atoi(splits[UidIdx]) | ||
| if err != nil { | ||
| return nil, errwrap.Wrap(errors.New("unable to parse uid"), err) | ||
| } | ||
| user.Gid, err = strconv.Atoi(splits[GidIdx]) | ||
| if err != nil { | ||
| return nil, errwrap.Wrap(errors.New("unable to parse gid"), err) | ||
| } | ||
|
|
||
| return user, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should you test if
parsePasswdLinereally returned a user?p.Namecan be""if a line is not parsed correctly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that
pkg/group/group.gohas the same pattern./cc @krnowak
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't write the /etc/groups parser, but I don't like this pattern at all. It should just return a pointer to user struct and also it should return an error if parsing the file failed (in case of invalid number of fields). So a prototype like
func parsePasswdLine(line text) (*User, error)should be used.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
git blamesaid @krnowak so I thought you wrote it.Should "pkg/group" be changed first to be consistent? But @marineam might be working on it via #2105.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, because I moved that from common to pkg/group. And @marineam is probably working on a different code anyway.