xref: /dragonfly/usr.sbin/pw/grupd.c (revision 2b7dbe20)
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  * $FreeBSD: src/usr.sbin/pw/grupd.c,v 1.10 2001/08/30 06:32:17 dd Exp $
27  */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <stdarg.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/param.h>
37 
38 #include "pwupd.h"
39 
40 static char * grpath = _PATH_PWD;
41 
42 int
43 setgrdir(const char * dir)
44 {
45 	if (dir == NULL)
46 		return -1;
47 	else {
48 		char * d = malloc(strlen(dir)+1);
49 		if (d == NULL)
50 			return -1;
51 		grpath = strcpy(d, dir);
52 	}
53 	return 0;
54 }
55 
56 char *
57 getgrpath(const char * file)
58 {
59 	static char pathbuf[MAXPATHLEN];
60 
61 	snprintf(pathbuf, sizeof pathbuf, "%s/%s", grpath, file);
62 	return pathbuf;
63 }
64 
65 int
66 grdb(char *arg __unused, ...)
67 {
68 	/*
69 	 * This is a stub for now, but maybe eventually be functional
70 	 * if ever an indexed version of /etc/groups is implemented.
71 	 */
72 
73 	return 0;
74 }
75 
76 int
77 fmtgrentry(char **buf, int * buflen, struct group * grp, int type)
78 {
79 	int             i, l;
80 
81 	/*
82 	 * Since a group line is of arbitrary length,
83 	 * we need to calculate up-front just how long
84 	 * it will need to be...
85 	 */
86 	/*  groupname              :   password                 :  gid  : */
87 	l = strlen(grp->gr_name) + 1 + strlen(grp->gr_passwd) + 1 + 5 + 1;
88 	/* group members + comma separator */
89 	for (i = 0; grp->gr_mem[i] != NULL; i++) {
90 		l += strlen(grp->gr_mem[i]) + 1;
91 	}
92 	l += 2; /* For newline & NUL */
93 	if (extendline(buf, buflen, l) == -1)
94 		l = -1;
95 	else{
96 		/*
97 		 * Now we can safely format
98 		 */
99 		if (type == PWF_STANDARD)
100 			l = sprintf(*buf, "%s:*:%ld:", grp->gr_name, (long) grp->gr_gid);
101 		else
102 			l = sprintf(*buf, "%s:%s:%ld:", grp->gr_name, grp->gr_passwd, (long) grp->gr_gid);
103 
104 		/*
105 		 * List members
106 		 */
107 		for (i = 0; grp->gr_mem[i] != NULL; i++) {
108 			l += sprintf(*buf + l, "%s%s", i ? "," : "", grp->gr_mem[i]);
109 		}
110 
111 		(*buf)[l++] = '\n';
112 		(*buf)[l] = '\0';
113 	}
114 	return l;
115 }
116 
117 
118 int
119 fmtgrent(char **buf, int * buflen, struct group * grp)
120 {
121 	return fmtgrentry(buf, buflen, grp, PWF_STANDARD);
122 }
123 
124 
125 static int
126 gr_update(struct group * grp, char const * group, int mode)
127 {
128 	int             l;
129 	char            pfx[64];
130 	int		grbuflen = 0;
131 	char	       *grbuf = NULL;
132 
133 	ENDGRENT();
134 	l = snprintf(pfx, sizeof pfx, "%s:", group);
135 
136 	/*
137 	 * Update the group file
138 	 */
139 	if (grp != NULL && fmtgrentry(&grbuf, &grbuflen, grp, PWF_PASSWD) == -1)
140 		l = -1;
141 	else {
142 		l = fileupdate(getgrpath(_GROUP), 0644, grbuf, pfx, l, mode);
143 		if (l == 0)
144 			l = grdb(NULL);
145 	}
146 	if (grbuf != NULL)
147 		free(grbuf);
148 	return l;
149 }
150 
151 
152 int
153 addgrent(struct group * grp)
154 {
155 	return gr_update(grp, grp->gr_name, UPD_CREATE);
156 }
157 
158 int
159 chggrent(char const * login, struct group * grp)
160 {
161 	return gr_update(grp, login, UPD_REPLACE);
162 }
163 
164 int
165 delgrent(struct group * grp)
166 {
167 	return gr_update(NULL, grp->gr_name, UPD_DELETE);
168 }
169