make config fields `chroot' and `user' fixed-size

This commit is contained in:
Omar Polo 2022-09-10 09:21:09 +00:00
parent aae8f6bf2b
commit 7277bb7dc2
3 changed files with 19 additions and 14 deletions

11
gmid.c
View File

@ -205,9 +205,6 @@ init_config(void)
init_mime(&conf.mime);
conf.chroot = NULL;
conf.user = NULL;
conf.prefork = 3;
}
@ -224,8 +221,6 @@ free_config(void)
v = conf.verbose;
free_mime(&conf.mime);
free(conf.chroot);
free(conf.user);
memset(&conf, 0, sizeof(conf));
conf.verbose = v;
@ -328,15 +323,15 @@ drop_priv(void)
{
struct passwd *pw = NULL;
if (conf.chroot != NULL && conf.user == NULL)
if (*conf.chroot != '\0' && *conf.user == '\0')
fatal("can't chroot without an user to switch to after.");
if (conf.user != NULL) {
if (*conf.user != '\0') {
if ((pw = getpwnam(conf.user)) == NULL)
fatal("can't find user %s", conf.user);
}
if (conf.chroot != NULL) {
if (*conf.chroot != '\0') {
if (chroot(conf.chroot) != 0 || chdir("/") != 0)
fatal("%s: %s", conf.chroot, strerror(errno));
}

4
gmid.h
View File

@ -203,8 +203,8 @@ struct conf {
int ipv6;
uint32_t protos;
struct mime mime;
char *chroot;
char *user;
char chroot[PATH_MAX];
char user[LOGIN_NAME_MAX];
int prefork;
};

18
parse.y
View File

@ -212,7 +212,12 @@ varset : STRING '=' string {
}
;
option : CHROOT string { conf.chroot = $2; }
option : CHROOT string {
if (strlcpy(conf.chroot, $2, sizeof(conf.chroot)) >=
sizeof(conf.chroot))
yyerror("chroot path too long");
free($2);
}
| IPV6 bool { conf.ipv6 = $2; }
| MIME STRING string {
yywarn("`mime MIME EXT' is deprecated and will be "
@ -235,7 +240,12 @@ option : CHROOT string { conf.chroot = $2; }
yyerror("invalid protocols string \"%s\"", $2);
free($2);
}
| USER string { conf.user = $2; }
| USER string {
if (strlcpy(conf.user, $2, sizeof(conf.user)) >=
sizeof(conf.user))
yyerror("user name too long");
free($2);
}
;
vhost : SERVER string {
@ -949,14 +959,14 @@ print_conf(void)
/* struct envlist *e; */
/* struct alist *a; */
if (conf.chroot != NULL)
if (*conf.chroot != '\0')
printf("chroot \"%s\"\n", conf.chroot);
printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
/* XXX: defined mimes? */
printf("port %d\n", conf.port);
printf("prefork %d\n", conf.prefork);
/* XXX: protocols? */
if (conf.user != NULL)
if (*conf.user != '\0')
printf("user \"%s\"\n", conf.user);
TAILQ_FOREACH(h, &hosts, vhosts) {