@@ -33,6 +33,32 @@ type Plugin struct {
3333 factory pool.Factory
3434}
3535
36+ // resolveUser looks the user up in the user database and returns its uid/gid.
37+ func resolveUser (name string ) (int , int , error ) {
38+ usr , err := user .Lookup (name )
39+ if err != nil {
40+ return 0 , 0 , err
41+ }
42+
43+ return parseIDs (usr )
44+ }
45+
46+ // parseIDs converts the user database's textual ids to ints; non-numeric ids
47+ // are rejected.
48+ func parseIDs (usr * user.User ) (int , int , error ) {
49+ uid , err := strconv .Atoi (usr .Uid )
50+ if err != nil {
51+ return 0 , 0 , errors .Errorf ("failed to parse the user id %q: %v" , usr .Uid , err )
52+ }
53+
54+ gid , err := strconv .Atoi (usr .Gid )
55+ if err != nil {
56+ return 0 , 0 , errors .Errorf ("failed to parse the group id %q: %v" , usr .Gid , err )
57+ }
58+
59+ return uid , gid , nil
60+ }
61+
3662// Init application provider.
3763func (p * Plugin ) Init (cfg Configurer , log NamedLogger ) error {
3864 const op = errors .Op ("server_plugin_init" )
@@ -67,17 +93,7 @@ func (p *Plugin) Init(cfg Configurer, log NamedLogger) error {
6793 return errors .E (op , errors .Init , errors .Str ("server.user is not supported on windows" ))
6894 }
6995
70- usr , err := user .Lookup (p .cfg .User )
71- if err != nil {
72- return errors .E (op , errors .Init , err )
73- }
74-
75- p .uid , err = strconv .Atoi (usr .Uid )
76- if err != nil {
77- return errors .E (op , errors .Init , err )
78- }
79-
80- p .gid , err = strconv .Atoi (usr .Gid )
96+ p .uid , p .gid , err = resolveUser (p .cfg .User )
8197 if err != nil {
8298 return errors .E (op , errors .Init , err )
8399 }
0 commit comments