grupd.c revision 20253
1/*-
2 * Copyright (c) 1996 by David L. Nugent <davidn@blaze.net.au>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer as
10 *    the first lines of this file unmodified.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *	This product includes software developed by David L. Nugent.
17 * 4. The name of the author may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE DAVID L. NUGENT ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	$Id$
33 */
34
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <unistd.h>
39#include <stdarg.h>
40#include <errno.h>
41#include <sys/types.h>
42#include <sys/stat.h>
43
44#include "pwupd.h"
45
46int
47fmtgrentry(char *buf, struct group * grp, int type)
48{
49	int             i, l;
50
51	if (type == PWF_STANDARD)
52		l = sprintf(buf, "%s:*:%ld:", grp->gr_name, (long) grp->gr_gid);
53	else
54		l = sprintf(buf, "%s:%s:%ld:", grp->gr_name, grp->gr_passwd, (long) grp->gr_gid);
55
56	/*
57	 * Now, list members
58	 */
59	for (i = 0; i < 200 && grp->gr_mem[i]; i++)
60		l += sprintf(buf + l, "%s%s", i ? "," : "", grp->gr_mem[i]);
61	buf[l++] = '\n';
62	buf[l] = '\0';
63	return l;
64}
65
66
67int
68fmtgrent(char *buf, struct group * grp)
69{
70	return fmtgrentry(buf, grp, PWF_STANDARD);
71}
72
73
74static int
75gr_update(struct group * grp, char const * group, int mode)
76{
77	int             l;
78	char            pfx[32];
79	char            grbuf[MAXPWLINE];
80
81	endgrent();
82	l = sprintf(pfx, "%s:", group);
83
84	/*
85	 * Update the group file
86	 */
87	if (grp == NULL)
88		*grbuf = '\0';
89	else
90		fmtgrentry(grbuf, grp, PWF_PASSWD);
91	return fileupdate(_PATH_GROUP, 0644, grbuf, pfx, l, mode);
92}
93
94
95int
96addgrent(struct group * grp)
97{
98	return gr_update(grp, grp->gr_name, UPD_CREATE);
99}
100
101int
102chggrent(char const * login, struct group * grp)
103{
104	return gr_update(grp, login, UPD_REPLACE);
105}
106
107int
108delgrent(struct group * grp)
109{
110	return gr_update(NULL, grp->gr_name, UPD_DELETE);
111}
112