4646
4747size_t fpm_pagesize ;
4848
49+
50+ static inline bool fpm_unix_is_id (const char * name )
51+ {
52+ return strlen (name ) == strspn (name , "0123456789" );
53+ }
54+
55+ static struct passwd * fpm_unix_get_passwd (struct fpm_worker_pool_s * wp , const char * name , int flags )
56+ {
57+ struct passwd * pwd = getpwnam (name );
58+ if (!pwd ) {
59+ zlog (flags , "[pool %s] cannot get uid for user '%s'" , wp -> config -> name , name );
60+ return NULL ;
61+ }
62+
63+ return pwd ;
64+ }
65+
66+ static inline bool fpm_unix_check_passwd (struct fpm_worker_pool_s * wp , const char * name , int flags )
67+ {
68+ return !name || fpm_unix_is_id (name ) || fpm_unix_get_passwd (wp , name , flags );
69+ }
70+
71+ static struct group * fpm_unix_get_group (struct fpm_worker_pool_s * wp , const char * name , int flags )
72+ {
73+ struct group * group = getgrnam (name );
74+ if (!group ) {
75+ zlog (flags , "[pool %s] cannot get gid for group '%s'" , wp -> config -> name , name );
76+ return NULL ;
77+ }
78+
79+ return group ;
80+ }
81+
82+ static inline bool fpm_unix_check_group (struct fpm_worker_pool_s * wp , const char * name , int flags )
83+ {
84+ return !name || fpm_unix_is_id (name ) || fpm_unix_get_group (wp , name , flags );
85+ }
86+
87+ bool fpm_unix_test_config (struct fpm_worker_pool_s * wp )
88+ {
89+ struct fpm_worker_pool_config_s * config = wp -> config ;
90+ return (
91+ fpm_unix_check_passwd (wp , config -> user , ZLOG_ERROR ) &&
92+ fpm_unix_check_group (wp , config -> group , ZLOG_ERROR ) &&
93+ fpm_unix_check_passwd (wp , config -> listen_owner , ZLOG_SYSERROR ) &&
94+ fpm_unix_check_group (wp , config -> listen_group , ZLOG_SYSERROR )
95+ );
96+ }
97+
4998int fpm_unix_resolve_socket_permissions (struct fpm_worker_pool_s * wp ) /* {{{ */
5099{
51100 struct fpm_worker_pool_config_s * c = wp -> config ;
@@ -105,11 +154,10 @@ int fpm_unix_resolve_socket_permissions(struct fpm_worker_pool_s *wp) /* {{{ */
105154 if ((end = strchr (p , ',' ))) {
106155 * end ++ = 0 ;
107156 }
108- pwd = getpwnam ( p );
157+ pwd = fpm_unix_get_passwd ( wp , p , ZLOG_SYSERROR );
109158 if (pwd ) {
110159 zlog (ZLOG_DEBUG , "[pool %s] user '%s' have uid=%d" , wp -> config -> name , p , pwd -> pw_uid );
111160 } else {
112- zlog (ZLOG_SYSERROR , "[pool %s] cannot get uid for user '%s'" , wp -> config -> name , p );
113161 acl_free (acl );
114162 efree (tmp );
115163 return -1 ;
@@ -138,11 +186,10 @@ int fpm_unix_resolve_socket_permissions(struct fpm_worker_pool_s *wp) /* {{{ */
138186 if ((end = strchr (p , ',' ))) {
139187 * end ++ = 0 ;
140188 }
141- grp = getgrnam ( p );
189+ grp = fpm_unix_get_group ( wp , p , ZLOG_SYSERROR );
142190 if (grp ) {
143191 zlog (ZLOG_DEBUG , "[pool %s] group '%s' have gid=%d" , wp -> config -> name , p , grp -> gr_gid );
144192 } else {
145- zlog (ZLOG_SYSERROR , "[pool %s] cannot get gid for group '%s'" , wp -> config -> name , p );
146193 acl_free (acl );
147194 efree (tmp );
148195 return -1 ;
@@ -175,14 +222,13 @@ int fpm_unix_resolve_socket_permissions(struct fpm_worker_pool_s *wp) /* {{{ */
175222#endif
176223
177224 if (c -> listen_owner && * c -> listen_owner ) {
178- if (strlen (c -> listen_owner ) == strspn ( c -> listen_owner , "0123456789" )) {
225+ if (fpm_unix_is_id (c -> listen_owner )) {
179226 wp -> socket_uid = strtoul (c -> listen_owner , 0 , 10 );
180227 } else {
181228 struct passwd * pwd ;
182229
183- pwd = getpwnam ( c -> listen_owner );
230+ pwd = fpm_unix_get_passwd ( wp , c -> listen_owner , ZLOG_SYSERROR );
184231 if (!pwd ) {
185- zlog (ZLOG_SYSERROR , "[pool %s] cannot get uid for user '%s'" , wp -> config -> name , c -> listen_owner );
186232 return -1 ;
187233 }
188234
@@ -192,14 +238,13 @@ int fpm_unix_resolve_socket_permissions(struct fpm_worker_pool_s *wp) /* {{{ */
192238 }
193239
194240 if (c -> listen_group && * c -> listen_group ) {
195- if (strlen (c -> listen_group ) == strspn ( c -> listen_group , "0123456789" )) {
241+ if (fpm_unix_is_id (c -> listen_group )) {
196242 wp -> socket_gid = strtoul (c -> listen_group , 0 , 10 );
197243 } else {
198244 struct group * grp ;
199245
200- grp = getgrnam ( c -> listen_group );
246+ grp = fpm_unix_get_group ( wp , c -> listen_group , ZLOG_SYSERROR );
201247 if (!grp ) {
202- zlog (ZLOG_SYSERROR , "[pool %s] cannot get gid for group '%s'" , wp -> config -> name , c -> listen_group );
203248 return -1 ;
204249 }
205250 wp -> socket_gid = grp -> gr_gid ;
@@ -279,7 +324,7 @@ static int fpm_unix_conf_wp(struct fpm_worker_pool_s *wp) /* {{{ */
279324
280325 if (is_root ) {
281326 if (wp -> config -> user && * wp -> config -> user ) {
282- if (strlen (wp -> config -> user ) == strspn ( wp -> config -> user , "0123456789" )) {
327+ if (fpm_unix_is_id (wp -> config -> user )) {
283328 wp -> set_uid = strtoul (wp -> config -> user , 0 , 10 );
284329 pwd = getpwuid (wp -> set_uid );
285330 if (pwd ) {
@@ -289,9 +334,8 @@ static int fpm_unix_conf_wp(struct fpm_worker_pool_s *wp) /* {{{ */
289334 } else {
290335 struct passwd * pwd ;
291336
292- pwd = getpwnam (wp -> config -> user );
337+ pwd = fpm_unix_get_passwd (wp , wp -> config -> user , ZLOG_ERROR );
293338 if (!pwd ) {
294- zlog (ZLOG_ERROR , "[pool %s] cannot get uid for user '%s'" , wp -> config -> name , wp -> config -> user );
295339 return -1 ;
296340 }
297341
@@ -304,14 +348,13 @@ static int fpm_unix_conf_wp(struct fpm_worker_pool_s *wp) /* {{{ */
304348 }
305349
306350 if (wp -> config -> group && * wp -> config -> group ) {
307- if (strlen (wp -> config -> group ) == strspn ( wp -> config -> group , "0123456789" )) {
351+ if (fpm_unix_is_id (wp -> config -> group )) {
308352 wp -> set_gid = strtoul (wp -> config -> group , 0 , 10 );
309353 } else {
310354 struct group * grp ;
311355
312- grp = getgrnam (wp -> config -> group );
356+ grp = fpm_unix_get_group (wp , wp -> config -> group , ZLOG_ERROR );
313357 if (!grp ) {
314- zlog (ZLOG_ERROR , "[pool %s] cannot get gid for group '%s'" , wp -> config -> name , wp -> config -> group );
315358 return -1 ;
316359 }
317360 wp -> set_gid = grp -> gr_gid ;
0 commit comments