|
1 | 1 | package cruntime |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "os" |
4 | 5 | os_user "os/user" |
5 | 6 | "strconv" |
6 | 7 | "strings" |
7 | 8 | "syscall" |
8 | 9 | ) |
9 | 10 |
|
10 | | -func switchUser(user string) (err error) { |
| 11 | +type User struct { |
| 12 | + Credential *syscall.Credential |
| 13 | + User *os_user.User |
| 14 | +} |
11 | 15 |
|
12 | | - if user == "" { |
13 | | - return nil |
| 16 | +func getUser(ug string) (User User, err error) { |
| 17 | + if ug == "" { |
| 18 | + return |
14 | 19 | } |
15 | 20 |
|
16 | 21 | var ( |
17 | | - uid int |
18 | | - gid int |
| 22 | + uid uint64 |
| 23 | + gid uint64 |
19 | 24 | ) |
20 | | - credential := strings.Split(user, ":") |
| 25 | + user_group := strings.Split(ug, ":") |
21 | 26 |
|
22 | | - if uid, err = strconv.Atoi(credential[0]); err != nil { |
23 | | - user, err := os_user.Lookup(credential[0]) |
| 27 | + if uid, err = strconv.ParseUint(user_group[0], 10, 32); err != nil { |
| 28 | + User.User, err = os_user.Lookup(user_group[0]) |
24 | 29 | if err == nil { |
25 | | - uid, _ = strconv.Atoi(user.Uid) |
| 30 | + uid, _ = strconv.ParseUint(User.User.Uid, 10, 32) |
26 | 31 | // if usergroup is not presented, try gid info from user information |
27 | 32 |
|
28 | | - if len(credential) == 1 && user.Gid != "" { |
29 | | - credential = append(credential, user.Gid) |
| 33 | + if len(user_group) == 1 && User.User.Gid != "" { |
| 34 | + user_group = append(user_group, User.User.Gid) |
30 | 35 | } |
31 | 36 | } else { |
32 | | - return err |
| 37 | + return |
33 | 38 | } |
| 39 | + |
34 | 40 | } |
35 | 41 |
|
36 | 42 | // if usergroup is not presented, set group name identical to username |
37 | | - if len(credential) == 1 { |
38 | | - credential = append(credential, credential[0]) |
| 43 | + if len(user_group) == 1 { |
| 44 | + user_group = append(user_group, user_group[0]) |
39 | 45 | } |
40 | 46 |
|
41 | | - if gid, err = strconv.Atoi(credential[1]); err != nil { |
42 | | - group, err := os_user.LookupGroup(credential[1]) |
| 47 | + if gid, err = strconv.ParseUint(user_group[1], 10, 32); err != nil { |
| 48 | + group, err := os_user.LookupGroup(user_group[1]) |
43 | 49 | if err == nil { |
44 | | - gid, _ = strconv.Atoi(group.Gid) |
| 50 | + gid, _ = strconv.ParseUint(group.Gid, 10, 32) |
45 | 51 | } |
46 | 52 | } |
47 | 53 |
|
48 | | - err = syscall.Setgid(gid) |
| 54 | + User.Credential = &syscall.Credential{} |
| 55 | + User.Credential.Uid = uint32(uid) |
| 56 | + User.Credential.Gid = uint32(gid) |
| 57 | + |
| 58 | + return |
| 59 | +} |
| 60 | + |
| 61 | +func switchUser(user User) error { |
| 62 | + err := switchCredential(user.Credential) |
49 | 63 | if err != nil { |
50 | 64 | return err |
51 | 65 | } |
52 | | - err = syscall.Setuid(uid) |
| 66 | + |
| 67 | + if user.User == nil { |
| 68 | + return nil |
| 69 | + } |
| 70 | + |
| 71 | + if user.User.HomeDir != "" { |
| 72 | + os.Chdir(user.User.HomeDir) |
| 73 | + } |
| 74 | + |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +func switchCredential(Credential *syscall.Credential) (err error) { |
| 79 | + if Credential == nil { |
| 80 | + return |
| 81 | + } |
| 82 | + err = syscall.Setgid(int(Credential.Gid)) |
| 83 | + if err != nil { |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + err = syscall.Setuid(int(Credential.Uid)) |
| 88 | + |
53 | 89 | return |
54 | 90 | } |
0 commit comments