1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 /*! \file */
13 
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <pwd.h>
17 #include <stdio.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 
22 #include <confgen/os.h>
23 
24 int
set_user(FILE * fd,const char * user)25 set_user(FILE *fd, const char *user) {
26 	struct passwd *pw;
27 
28 	pw = getpwnam(user);
29 	if (pw == NULL) {
30 		errno = EINVAL;
31 		return (-1);
32 	}
33 	return (fchown(fileno(fd), pw->pw_uid, -1));
34 }
35