From: Willy Tarreau Date: Fri, 28 Aug 2020 09:54:59 +0000 (+0200) Subject: REORG: unix: move UNIX bind/server keywords from proto_uxst.c to cfgparse-unix.c X-Git-Tag: v2.3-dev4~62 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1318034317ef14d7640ee242d874290d5dd84418;p=thirdparty%2Fhaproxy.git REORG: unix: move UNIX bind/server keywords from proto_uxst.c to cfgparse-unix.c Let's finish the cleanup and get rid of all bind and server keywords parsers from proto_uxst.c. They're now moved to cfgparse-unix.c. Now proto_uxst.c is clean and only contains code related to binding and connecting. --- diff --git a/Makefile b/Makefile index 854194eadf..2372af21dc 100644 --- a/Makefile +++ b/Makefile @@ -819,7 +819,8 @@ OBJS = src/mux_fcgi.o src/mux_h1.o src/mux_h2.o src/backend.o \ src/ebsttree.o src/pipe.o src/hpack-enc.o src/fcgi.o \ src/eb64tree.o src/dict.o src/shctx.o src/ebimtree.o \ src/eb32tree.o src/ebtree.o src/dgram.o src/proto_udp.o \ - src/hpack-huff.o src/cfgparse-tcp.o src/base64.o src/version.o + src/hpack-huff.o src/cfgparse-tcp.o src/base64.o src/version.o \ + src/cfgparse-unix.o ifneq ($(TRACE),) OBJS += src/calltrace.o diff --git a/src/cfgparse-unix.c b/src/cfgparse-unix.c new file mode 100644 index 0000000000..b1631c9013 --- /dev/null +++ b/src/cfgparse-unix.c @@ -0,0 +1,136 @@ +/* + * Configuration parsing for UNIX sockets (bind and server keywords) + * + * Copyright 2000-2020 Willy Tarreau + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* parse the "mode" bind keyword */ +static int bind_parse_mode(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) +{ + char *endptr; + + conf->ux.mode = strtol(args[cur_arg + 1], &endptr, 8); + + if (!*args[cur_arg + 1] || *endptr) { + memprintf(err, "'%s' : missing or invalid mode '%s' (octal integer expected)", args[cur_arg], args[cur_arg + 1]); + return ERR_ALERT | ERR_FATAL; + } + + return 0; +} + +/* parse the "gid" bind keyword */ +static int bind_parse_gid(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) +{ + if (!*args[cur_arg + 1]) { + memprintf(err, "'%s' : missing value", args[cur_arg]); + return ERR_ALERT | ERR_FATAL; + } + + conf->ux.gid = atol(args[cur_arg + 1]); + return 0; +} + +/* parse the "group" bind keyword */ +static int bind_parse_group(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) +{ + struct group *group; + + if (!*args[cur_arg + 1]) { + memprintf(err, "'%s' : missing group name", args[cur_arg]); + return ERR_ALERT | ERR_FATAL; + } + + group = getgrnam(args[cur_arg + 1]); + if (!group) { + memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]); + return ERR_ALERT | ERR_FATAL; + } + + conf->ux.gid = group->gr_gid; + return 0; +} + +/* parse the "uid" bind keyword */ +static int bind_parse_uid(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) +{ + if (!*args[cur_arg + 1]) { + memprintf(err, "'%s' : missing value", args[cur_arg]); + return ERR_ALERT | ERR_FATAL; + } + + conf->ux.uid = atol(args[cur_arg + 1]); + return 0; +} + +/* parse the "user" bind keyword */ +static int bind_parse_user(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) +{ + struct passwd *user; + + if (!*args[cur_arg + 1]) { + memprintf(err, "'%s' : missing user name", args[cur_arg]); + return ERR_ALERT | ERR_FATAL; + } + + user = getpwnam(args[cur_arg + 1]); + if (!user) { + memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]); + return ERR_ALERT | ERR_FATAL; + } + + conf->ux.uid = user->pw_uid; + return 0; +} + +/* Note: must not be declared as its list will be overwritten. + * Please take care of keeping this list alphabetically sorted, doing so helps + * all code contributors. + * Optional keywords are also declared with a NULL ->parse() function so that + * the config parser can report an appropriate error when a known keyword was + * not enabled. + */ +static struct bind_kw_list bind_kws = { "UNIX", { }, { + { "gid", bind_parse_gid, 1 }, /* set the socket's gid */ + { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */ + { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/ + { "uid", bind_parse_uid, 1 }, /* set the socket's uid */ + { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */ + { NULL, NULL, 0 }, +}}; + +INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws); diff --git a/src/proto_uxst.c b/src/proto_uxst.c index 2a4610f68a..f64c890ab2 100644 --- a/src/proto_uxst.c +++ b/src/proto_uxst.c @@ -13,8 +13,6 @@ #include #include #include -#include -#include #include #include #include @@ -650,103 +648,6 @@ static int uxst_unbind_listeners(struct protocol *proto) return ERR_NONE; } -/* parse the "mode" bind keyword */ -static int bind_parse_mode(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) -{ - char *endptr; - - conf->ux.mode = strtol(args[cur_arg + 1], &endptr, 8); - - if (!*args[cur_arg + 1] || *endptr) { - memprintf(err, "'%s' : missing or invalid mode '%s' (octal integer expected)", args[cur_arg], args[cur_arg + 1]); - return ERR_ALERT | ERR_FATAL; - } - - return 0; -} - -/* parse the "gid" bind keyword */ -static int bind_parse_gid(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) -{ - if (!*args[cur_arg + 1]) { - memprintf(err, "'%s' : missing value", args[cur_arg]); - return ERR_ALERT | ERR_FATAL; - } - - conf->ux.gid = atol(args[cur_arg + 1]); - return 0; -} - -/* parse the "group" bind keyword */ -static int bind_parse_group(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) -{ - struct group *group; - - if (!*args[cur_arg + 1]) { - memprintf(err, "'%s' : missing group name", args[cur_arg]); - return ERR_ALERT | ERR_FATAL; - } - - group = getgrnam(args[cur_arg + 1]); - if (!group) { - memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]); - return ERR_ALERT | ERR_FATAL; - } - - conf->ux.gid = group->gr_gid; - return 0; -} - -/* parse the "uid" bind keyword */ -static int bind_parse_uid(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) -{ - if (!*args[cur_arg + 1]) { - memprintf(err, "'%s' : missing value", args[cur_arg]); - return ERR_ALERT | ERR_FATAL; - } - - conf->ux.uid = atol(args[cur_arg + 1]); - return 0; -} - -/* parse the "user" bind keyword */ -static int bind_parse_user(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) -{ - struct passwd *user; - - if (!*args[cur_arg + 1]) { - memprintf(err, "'%s' : missing user name", args[cur_arg]); - return ERR_ALERT | ERR_FATAL; - } - - user = getpwnam(args[cur_arg + 1]); - if (!user) { - memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]); - return ERR_ALERT | ERR_FATAL; - } - - conf->ux.uid = user->pw_uid; - return 0; -} - -/* Note: must not be declared as its list will be overwritten. - * Please take care of keeping this list alphabetically sorted, doing so helps - * all code contributors. - * Optional keywords are also declared with a NULL ->parse() function so that - * the config parser can report an appropriate error when a known keyword was - * not enabled. - */ -static struct bind_kw_list bind_kws = { "UNIX", { }, { - { "gid", bind_parse_gid, 1 }, /* set the socket's gid */ - { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */ - { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/ - { "uid", bind_parse_uid, 1 }, /* set the socket's uid */ - { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */ - { NULL, NULL, 0 }, -}}; - -INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws); - /* * Local variables: * c-indent-level: 8