xref: /freebsd/usr.sbin/pw/grupd.c (revision 4f29da19bd44f0e99f021510460a81bf754c21d2)
1 /*-
2  * Copyright (C) 1996
3  *	David L. Nugent.  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.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef lint
28 static const char rcsid[] =
29   "$FreeBSD$";
30 #endif /* not lint */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <stdarg.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/param.h>
40 
41 #include "pwupd.h"
42 
43 static char * grpath = _PATH_PWD;
44 
45 int
46 setgrdir(const char * dir)
47 {
48 	if (dir == NULL)
49 		return -1;
50 	else {
51 		char * d = malloc(strlen(dir)+1);
52 		if (d == NULL)
53 			return -1;
54 		grpath = strcpy(d, dir);
55 	}
56 	return 0;
57 }
58 
59 char *
60 getgrpath(const char * file)
61 {
62 	static char pathbuf[MAXPATHLEN];
63 
64 	snprintf(pathbuf, sizeof pathbuf, "%s/%s", grpath, file);
65 	return pathbuf;
66 }
67 
68 int
69 grdb(char *arg,...)
70 {
71 	/*
72 	 * This is a stub for now, but maybe eventually be functional
73 	 * if ever an indexed version of /etc/groups is implemented.
74 	 */
75 	arg=arg;
76 	return 0;
77 }
78 
79 int
80 fmtgrentry(char **buf, int * buflen, struct group * grp, int type)
81 {
82 	int             i, l;
83 
84 	/*
85 	 * Since a group line is of arbitrary length,
86 	 * we need to calculate up-front just how long
87 	 * it will need to be...
88 	 */
89 	/*  groupname              :   password                 :  gid  : */
90 	l = strlen(grp->gr_name) + 1 + strlen(grp->gr_passwd) + 1 + 5 + 1;
91 	/* group members + comma separator */
92 	for (i = 0; grp->gr_mem[i] != NULL; i++) {
93 		l += strlen(grp->gr_mem[i]) + 1;
94 	}
95 	l += 2; /* For newline & NUL */
96 	if (extendline(buf, buflen, l) == -1)
97 		l = -1;
98 	else{
99 		/*
100 		 * Now we can safely format
101 		 */
102 		if (type == PWF_STANDARD)
103 			l = sprintf(*buf, "%s:*:%ld:", grp->gr_name, (long) grp->gr_gid);
104 		else
105 			l = sprintf(*buf, "%s:%s:%ld:", grp->gr_name, grp->gr_passwd, (long) grp->gr_gid);
106 
107 		/*
108 		 * List members
109 		 */
110 		for (i = 0; grp->gr_mem[i] != NULL; i++) {
111 			l += sprintf(*buf + l, "%s%s", i ? "," : "", grp->gr_mem[i]);
112 		}
113 
114 		(*buf)[l++] = '\n';
115 		(*buf)[l] = '\0';
116 	}
117 	return l;
118 }
119 
120 
121 int
122 fmtgrent(char **buf, int * buflen, struct group * grp)
123 {
124 	return fmtgrentry(buf, buflen, grp, PWF_STANDARD);
125 }
126 
127 
128 static int
129 gr_update(struct group * grp, char const * group, int mode)
130 {
131 	int             l;
132 	char            pfx[64];
133 	int		grbuflen = 0;
134 	char	       *grbuf = NULL;
135 
136 	ENDGRENT();
137 	l = snprintf(pfx, sizeof pfx, "%s:", group);
138 
139 	/*
140 	 * Update the group file
141 	 */
142 	if (grp != NULL && fmtgrentry(&grbuf, &grbuflen, grp, PWF_PASSWD) == -1)
143 		l = -1;
144 	else {
145 		l = fileupdate(getgrpath(_GROUP), 0644, grbuf, pfx, l, mode);
146 		if (l == 0)
147 			l = grdb(NULL);
148 	}
149 	if (grbuf != NULL)
150 		free(grbuf);
151 	return l;
152 }
153 
154 
155 int
156 addgrent(struct group * grp)
157 {
158 	return gr_update(grp, grp->gr_name, UPD_CREATE);
159 }
160 
161 int
162 chggrent(char const * login, struct group * grp)
163 {
164 	return gr_update(grp, login, UPD_REPLACE);
165 }
166 
167 int
168 delgrent(struct group * grp)
169 {
170 	return gr_update(NULL, grp->gr_name, UPD_DELETE);
171 }
172