[MEDIUM] add user/groupname support
Patch from Marcus Rueckert for 1.2.17 :
"I added the attached patch to haproxy. I don't have a static uid/gid for
haproxy so i need to specify the username/groupname to run it as non
root user."
diff --git a/src/cfgparse.c b/src/cfgparse.c
index ae5433e..4c1f032 100644
--- a/src/cfgparse.c
+++ b/src/cfgparse.c
@@ -15,6 +15,8 @@
#include <string.h>
#include <netdb.h>
#include <ctype.h>
+#include <pwd.h>
+#include <grp.h>
#include <common/cfgparse.h>
#include <common/config.h>
@@ -269,7 +271,7 @@
}
else if (!strcmp(args[0], "uid")) {
if (global.uid != 0) {
- Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
+ Alert("parsing [%s:%d] : user/uid already specified. Continuing.\n", file, linenum);
return 0;
}
if (*(args[1]) == 0) {
@@ -280,7 +282,7 @@
}
else if (!strcmp(args[0], "gid")) {
if (global.gid != 0) {
- Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
+ Alert("parsing [%s:%d] : group/gid already specified. Continuing.\n", file, linenum);
return 0;
}
if (*(args[1]) == 0) {
@@ -289,6 +291,40 @@
}
global.gid = atol(args[1]);
}
+ /* user/group name handling */
+ else if (!strcmp(args[0], "user")) {
+ struct passwd *ha_user;
+ if (global.uid != 0) {
+ Alert("parsing [%s:%d] : user/uid already specified. Continuing.\n", file, linenum);
+ return 0;
+ }
+ errno = 0;
+ ha_user = getpwnam(args[1]);
+ if (ha_user != NULL) {
+ global.uid = (int)ha_user->pw_uid;
+ }
+ else {
+ Alert("parsing [%s:%d] : cannot find user id for '%s' (%d:%s)\n", file, linenum, args[1], errno, strerror(errno));
+ exit(1);
+ }
+ }
+ else if (!strcmp(args[0], "group")) {
+ struct group *ha_group;
+ if (global.gid != 0) {
+ Alert("parsing [%s:%d] : gid/group was already specified. Continuing.\n", file, linenum, args[0]);
+ return 0;
+ }
+ errno = 0;
+ ha_group = getgrnam(args[1]);
+ if (ha_group != NULL) {
+ global.gid = (int)ha_group->gr_gid;
+ }
+ else {
+ Alert("parsing [%s:%d] : cannot find group id for '%s' (%d:%s)\n", file, linenum, args[1], errno, strerror(errno));
+ exit(1);
+ }
+ }
+ /* end of user/group name handling*/
else if (!strcmp(args[0], "nbproc")) {
if (global.nbproc != 0) {
Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);