* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
- * $Id: grupd.c,v 1.1.1.1 1996/12/09 14:05:35 joerg Exp $
*/
+#ifndef lint
+static const char rcsid[] =
+ "$FreeBSD$";
+#endif /* not lint */
+
+#include <grp.h>
+#include <libutil.h>
+#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <unistd.h>
-#include <stdarg.h>
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/stat.h>
+#include <sys/param.h>
#include "pwupd.h"
-int
-fmtgrentry(char *buf, struct group * grp, int type)
+char *
+getgrpath(const char * file)
{
- int i, l;
-
- if (type == PWF_STANDARD)
- l = sprintf(buf, "%s:*:%ld:", grp->gr_name, (long) grp->gr_gid);
- else
- l = sprintf(buf, "%s:%s:%ld:", grp->gr_name, grp->gr_passwd, (long) grp->gr_gid);
-
- /*
- * Now, list members
- */
- for (i = 0; i < 200 && grp->gr_mem[i]; i++)
- l += sprintf(buf + l, "%s%s", i ? "," : "", grp->gr_mem[i]);
- buf[l++] = '\n';
- buf[l] = '\0';
- return l;
-}
+ static char pathbuf[MAXPATHLEN];
+ snprintf(pathbuf, sizeof pathbuf, "%s/%s", conf.etcpath, file);
-int
-fmtgrent(char *buf, struct group * grp)
-{
- return fmtgrentry(buf, grp, PWF_STANDARD);
+ return (pathbuf);
}
-
static int
-gr_update(struct group * grp, char const * group, int mode)
+gr_update(struct group * grp, char const * group)
{
- int l;
- char pfx[32];
- char grbuf[MAXPWLINE];
-
- endgrent();
- l = sprintf(pfx, "%s:", group);
-
- /*
- * Update the group file
- */
- if (grp == NULL)
- *grbuf = '\0';
- else
- fmtgrentry(grbuf, grp, PWF_PASSWD);
- return fileupdate(_PATH_GROUP, 0644, grbuf, pfx, l, mode);
+ int pfd, tfd;
+ struct group *gr = NULL;
+ struct group *old_gr = NULL;
+
+ if (grp != NULL)
+ gr = gr_dup(grp);
+
+ if (group != NULL)
+ old_gr = GETGRNAM(group);
+
+ if (gr_init(conf.etcpath, NULL))
+ err(1, "gr_init()");
+
+ if ((pfd = gr_lock()) == -1) {
+ gr_fini();
+ err(1, "gr_lock()");
+ }
+ if ((tfd = gr_tmp(-1)) == -1) {
+ gr_fini();
+ err(1, "gr_tmp()");
+ }
+ if (gr_copy(pfd, tfd, gr, old_gr) == -1) {
+ gr_fini();
+ err(1, "gr_copy()");
+ }
+ if (gr_mkdb() == -1) {
+ gr_fini();
+ err(1, "gr_mkdb()");
+ }
+ free(gr);
+ gr_fini();
+ return 0;
}
int
addgrent(struct group * grp)
{
- return gr_update(grp, grp->gr_name, UPD_CREATE);
+ return gr_update(grp, NULL);
}
int
chggrent(char const * login, struct group * grp)
{
- return gr_update(grp, login, UPD_REPLACE);
+ return gr_update(grp, login);
}
int
delgrent(struct group * grp)
{
- return gr_update(NULL, grp->gr_name, UPD_DELETE);
+
+ return (gr_update(NULL, grp->gr_name));
}