X-Git-Url: https://git.cameronkatri.com/pw-darwin.git/blobdiff_plain/6924c20486d31cd8c84dfbcd4eecf8df045c8d57..c060b30250b3b666ae19dc91192abdfc89250b62:/pw/grupd.c diff --git a/pw/grupd.c b/pw/grupd.c index cf0584e..3f78e95 100644 --- a/pw/grupd.c +++ b/pw/grupd.c @@ -22,108 +22,107 @@ * 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$ */ +#ifndef lint +static const char rcsid[] = + "$FreeBSD$"; +#endif /* not lint */ + +#include +#include +#include #include #include #include #include #include -#include #include #include +#include #include "pwupd.h" +static char * grpath = _PATH_PWD; + int -fmtgrentry(char **buf, int * buflen, struct group * grp, int type) +setgrdir(const char * dir) { - int i, l; - - /* - * Since a group line is of arbitrary length, - * we need to calculate up-front just how long - * it will need to be... - */ - /* groupname : password : gid : */ - l = strlen(grp->gr_name) + 1 + strlen(grp->gr_passwd) + 1 + 5 + 1; - /* group members + comma separator */ - for (i = 0; grp->gr_mem[i] != NULL; i++) { - l += strlen(grp->gr_mem[i]) + 1; - } - l += 2; /* For newline & NUL */ - if (extendline(buf, buflen, l) == -1) - l = -1; - else{ - /* - * Now we can safely format - */ - 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); - - /* - * List members - */ - for (i = 0; grp->gr_mem[i] != NULL; i++) { - l += sprintf(*buf + l, "%s%s", i ? "," : "", grp->gr_mem[i]); - } - - (*buf)[l++] = '\n'; - (*buf)[l] = '\0'; - } - return l; -} + if (dir == NULL) + return -1; + else + grpath = strdup(dir); + if (grpath == NULL) + return -1; + return 0; +} -int -fmtgrent(char **buf, int * buflen, struct group * grp) +char * +getgrpath(const char * file) { - return fmtgrentry(buf, buflen, grp, PWF_STANDARD); -} + static char pathbuf[MAXPATHLEN]; + snprintf(pathbuf, sizeof pathbuf, "%s/%s", grpath, file); + 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[64]; - int grbuflen = 0; - char *grbuf = NULL; - - endgrent(); - l = snprintf(pfx, sizeof pfx, "%s:", group); - - /* - * Update the group file - */ - if (grp != NULL && fmtgrentry(&grbuf, &grbuflen, grp, PWF_PASSWD) == -1) - l = -1; - else - l = fileupdate(_PATH_GROUP, 0644, grbuf, pfx, l, mode); - if (grbuf != NULL) - free(grbuf); - return l; + 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(grpath, 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); + char group[MAXLOGNAME]; + + strlcpy(group, grp->gr_name, MAXLOGNAME); + + return gr_update(NULL, group); }