xref: /freebsd/usr.sbin/pw/grupd.c (revision 6137b5f7b8c183ee8806d79b3f1d8e5e3ddb3df3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 1996
5  *	David L. Nugent.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <err.h>
30 #include <grp.h>
31 #include <libutil.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 
36 #include "pwupd.h"
37 
38 char *
39 getgrpath(const char * file)
40 {
41 	static char pathbuf[MAXPATHLEN];
42 
43 	snprintf(pathbuf, sizeof pathbuf, "%s/%s", conf.etcpath, file);
44 
45 	return (pathbuf);
46 }
47 
48 static int
49 gr_update(struct group * grp, char const * group)
50 {
51 	int pfd, tfd;
52 	struct group *gr = NULL;
53 	struct group *old_gr = NULL;
54 
55 	if (grp != NULL)
56 		gr = gr_dup(grp);
57 
58 	if (group != NULL)
59 		old_gr = GETGRNAM(group);
60 
61 	if (gr_init(conf.etcpath, NULL))
62 		err(1, "gr_init()");
63 
64 	if ((pfd = gr_lock()) == -1) {
65 		gr_fini();
66 		err(1, "gr_lock()");
67 	}
68 	if ((tfd = gr_tmp(-1)) == -1) {
69 		gr_fini();
70 		err(1, "gr_tmp()");
71 	}
72 	if (gr_copy(pfd, tfd, gr, old_gr) == -1) {
73 		gr_fini();
74 		close(tfd);
75 		err(1, "gr_copy()");
76 	}
77 	fsync(tfd);
78 	close(tfd);
79 	if (gr_mkdb() == -1) {
80 		gr_fini();
81 		err(1, "gr_mkdb()");
82 	}
83 	free(gr);
84 	gr_fini();
85 	return 0;
86 }
87 
88 
89 int
90 addgrent(struct group * grp)
91 {
92 	return gr_update(grp, NULL);
93 }
94 
95 int
96 chggrent(char const * login, struct group * grp)
97 {
98 	return gr_update(grp, login);
99 }
100 
101 int
102 delgrent(struct group * grp)
103 {
104 
105 	return (gr_update(NULL, grp->gr_name));
106 }
107