46
46
47
47
size_t fpm_pagesize ;
48
48
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
+
49
98
int fpm_unix_resolve_socket_permissions (struct fpm_worker_pool_s * wp ) /* {{{ */
50
99
{
51
100
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) /* {{{ */
105
154
if ((end = strchr (p , ',' ))) {
106
155
* end ++ = 0 ;
107
156
}
108
- pwd = getpwnam ( p );
157
+ pwd = fpm_unix_get_passwd ( wp , p , ZLOG_SYSERROR );
109
158
if (pwd ) {
110
159
zlog (ZLOG_DEBUG , "[pool %s] user '%s' have uid=%d" , wp -> config -> name , p , pwd -> pw_uid );
111
160
} else {
112
- zlog (ZLOG_SYSERROR , "[pool %s] cannot get uid for user '%s'" , wp -> config -> name , p );
113
161
acl_free (acl );
114
162
efree (tmp );
115
163
return -1 ;
@@ -138,11 +186,10 @@ int fpm_unix_resolve_socket_permissions(struct fpm_worker_pool_s *wp) /* {{{ */
138
186
if ((end = strchr (p , ',' ))) {
139
187
* end ++ = 0 ;
140
188
}
141
- grp = getgrnam ( p );
189
+ grp = fpm_unix_get_group ( wp , p , ZLOG_SYSERROR );
142
190
if (grp ) {
143
191
zlog (ZLOG_DEBUG , "[pool %s] group '%s' have gid=%d" , wp -> config -> name , p , grp -> gr_gid );
144
192
} else {
145
- zlog (ZLOG_SYSERROR , "[pool %s] cannot get gid for group '%s'" , wp -> config -> name , p );
146
193
acl_free (acl );
147
194
efree (tmp );
148
195
return -1 ;
@@ -175,14 +222,13 @@ int fpm_unix_resolve_socket_permissions(struct fpm_worker_pool_s *wp) /* {{{ */
175
222
#endif
176
223
177
224
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 )) {
179
226
wp -> socket_uid = strtoul (c -> listen_owner , 0 , 10 );
180
227
} else {
181
228
struct passwd * pwd ;
182
229
183
- pwd = getpwnam ( c -> listen_owner );
230
+ pwd = fpm_unix_get_passwd ( wp , c -> listen_owner , ZLOG_SYSERROR );
184
231
if (!pwd ) {
185
- zlog (ZLOG_SYSERROR , "[pool %s] cannot get uid for user '%s'" , wp -> config -> name , c -> listen_owner );
186
232
return -1 ;
187
233
}
188
234
@@ -192,14 +238,13 @@ int fpm_unix_resolve_socket_permissions(struct fpm_worker_pool_s *wp) /* {{{ */
192
238
}
193
239
194
240
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 )) {
196
242
wp -> socket_gid = strtoul (c -> listen_group , 0 , 10 );
197
243
} else {
198
244
struct group * grp ;
199
245
200
- grp = getgrnam ( c -> listen_group );
246
+ grp = fpm_unix_get_group ( wp , c -> listen_group , ZLOG_SYSERROR );
201
247
if (!grp ) {
202
- zlog (ZLOG_SYSERROR , "[pool %s] cannot get gid for group '%s'" , wp -> config -> name , c -> listen_group );
203
248
return -1 ;
204
249
}
205
250
wp -> socket_gid = grp -> gr_gid ;
@@ -279,7 +324,7 @@ static int fpm_unix_conf_wp(struct fpm_worker_pool_s *wp) /* {{{ */
279
324
280
325
if (is_root ) {
281
326
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 )) {
283
328
wp -> set_uid = strtoul (wp -> config -> user , 0 , 10 );
284
329
pwd = getpwuid (wp -> set_uid );
285
330
if (pwd ) {
@@ -289,9 +334,8 @@ static int fpm_unix_conf_wp(struct fpm_worker_pool_s *wp) /* {{{ */
289
334
} else {
290
335
struct passwd * pwd ;
291
336
292
- pwd = getpwnam (wp -> config -> user );
337
+ pwd = fpm_unix_get_passwd (wp , wp -> config -> user , ZLOG_ERROR );
293
338
if (!pwd ) {
294
- zlog (ZLOG_ERROR , "[pool %s] cannot get uid for user '%s'" , wp -> config -> name , wp -> config -> user );
295
339
return -1 ;
296
340
}
297
341
@@ -304,14 +348,13 @@ static int fpm_unix_conf_wp(struct fpm_worker_pool_s *wp) /* {{{ */
304
348
}
305
349
306
350
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 )) {
308
352
wp -> set_gid = strtoul (wp -> config -> group , 0 , 10 );
309
353
} else {
310
354
struct group * grp ;
311
355
312
- grp = getgrnam (wp -> config -> group );
356
+ grp = fpm_unix_get_group (wp , wp -> config -> group , ZLOG_ERROR );
313
357
if (!grp ) {
314
- zlog (ZLOG_ERROR , "[pool %s] cannot get gid for group '%s'" , wp -> config -> name , wp -> config -> group );
315
358
return -1 ;
316
359
}
317
360
wp -> set_gid = grp -> gr_gid ;
0 commit comments