120253Sjoerg/*- 220302Sjoerg * Copyright (C) 1996 320302Sjoerg * David L. Nugent. All rights reserved. 420253Sjoerg * 520253Sjoerg * Redistribution and use in source and binary forms, with or without 620253Sjoerg * modification, are permitted provided that the following conditions 720253Sjoerg * are met: 820253Sjoerg * 1. Redistributions of source code must retain the above copyright 920302Sjoerg * notice, this list of conditions and the following disclaimer. 1020253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright 1120253Sjoerg * notice, this list of conditions and the following disclaimer in the 1220253Sjoerg * documentation and/or other materials provided with the distribution. 1320253Sjoerg * 1420302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 1520253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1620253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1720302Sjoerg * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 1820253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1920253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2020253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2120253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2220253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2320253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2420253Sjoerg * SUCH DAMAGE. 2520253Sjoerg */ 2620253Sjoerg 2730259Scharnier#ifndef lint 2830259Scharnierstatic const char rcsid[] = 2950479Speter "$FreeBSD$"; 3030259Scharnier#endif /* not lint */ 3130259Scharnier 3220253Sjoerg#include <stdio.h> 3320253Sjoerg#include <stdlib.h> 3420253Sjoerg#include <string.h> 3520253Sjoerg#include <unistd.h> 3620253Sjoerg#include <stdarg.h> 3720253Sjoerg#include <errno.h> 3820253Sjoerg#include <sys/types.h> 3920253Sjoerg#include <sys/stat.h> 4044229Sdavidn#include <sys/param.h> 4120253Sjoerg#include <sys/wait.h> 4220253Sjoerg 4320253Sjoerg#include "pwupd.h" 4420253Sjoerg 4520267Sjoerg#define HAVE_PWDB_C 1 4661758Sdavidn#define HAVE_PWDB_U 1 4720267Sjoerg 4844229Sdavidnstatic char pathpwd[] = _PATH_PWD; 4944229Sdavidnstatic char * pwpath = pathpwd; 5044229Sdavidn 5144229Sdavidnint 5244229Sdavidnsetpwdir(const char * dir) 5344229Sdavidn{ 5444229Sdavidn if (dir == NULL) 5544229Sdavidn return -1; 56246280Seadler else 57246280Seadler pwpath = strdup(dir); 58246280Seadler if (pwpath == NULL) 59246280Seadler return -1; 6044229Sdavidn return 0; 6144229Sdavidn} 6244229Sdavidn 6344229Sdavidnchar * 6444229Sdavidngetpwpath(char const * file) 6544229Sdavidn{ 6644229Sdavidn static char pathbuf[MAXPATHLEN]; 6744229Sdavidn 6844229Sdavidn snprintf(pathbuf, sizeof pathbuf, "%s/%s", pwpath, file); 6944229Sdavidn return pathbuf; 7044229Sdavidn} 7144229Sdavidn 7244229Sdavidnint 7320253Sjoergpwdb(char *arg,...) 7420253Sjoerg{ 7520253Sjoerg int i = 0; 7620253Sjoerg pid_t pid; 7720253Sjoerg va_list ap; 7844229Sdavidn char *args[10]; 7920253Sjoerg 8020253Sjoerg args[i++] = _PATH_PWD_MKDB; 8120253Sjoerg va_start(ap, arg); 8220253Sjoerg while (i < 6 && arg != NULL) { 8320253Sjoerg args[i++] = arg; 8420253Sjoerg arg = va_arg(ap, char *); 8520253Sjoerg } 8644229Sdavidn if (pwpath != pathpwd) { 8744229Sdavidn args[i++] = "-d"; 8844229Sdavidn args[i++] = pwpath; 8944229Sdavidn } 9044229Sdavidn args[i++] = getpwpath(_MASTERPASSWD); 9120253Sjoerg args[i] = NULL; 9220253Sjoerg 9320253Sjoerg if ((pid = fork()) == -1) /* Error (errno set) */ 9452502Sdavidn i = errno; 9520253Sjoerg else if (pid == 0) { /* Child */ 9620253Sjoerg execv(args[0], args); 9720253Sjoerg _exit(1); 9820253Sjoerg } else { /* Parent */ 9920253Sjoerg waitpid(pid, &i, 0); 10052502Sdavidn if (WEXITSTATUS(i)) 10152502Sdavidn i = EIO; 10220253Sjoerg } 10320253Sjoerg return i; 10420253Sjoerg} 10520253Sjoerg 10620253Sjoergint 10720253Sjoergfmtpwentry(char *buf, struct passwd * pwd, int type) 10820253Sjoerg{ 10920253Sjoerg int l; 11020253Sjoerg char *pw; 11120253Sjoerg 112109749Sfjoe pw = (type == PWF_MASTER) ? 113109749Sfjoe ((pwd->pw_passwd == NULL) ? "" : pwd->pw_passwd) : "*"; 11420253Sjoerg 11520253Sjoerg if (type == PWF_PASSWD) 11620253Sjoerg l = sprintf(buf, "%s:*:%ld:%ld:%s:%s:%s\n", 11720253Sjoerg pwd->pw_name, (long) pwd->pw_uid, (long) pwd->pw_gid, 11820253Sjoerg pwd->pw_gecos ? pwd->pw_gecos : "User &", 11920253Sjoerg pwd->pw_dir, pwd->pw_shell); 12020253Sjoerg else 12120253Sjoerg l = sprintf(buf, "%s:%s:%ld:%ld:%s:%lu:%lu:%s:%s:%s\n", 12220253Sjoerg pwd->pw_name, pw, (long) pwd->pw_uid, (long) pwd->pw_gid, 12320253Sjoerg pwd->pw_class ? pwd->pw_class : "", 12420253Sjoerg (unsigned long) pwd->pw_change, 12520253Sjoerg (unsigned long) pwd->pw_expire, 12620253Sjoerg pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 12720253Sjoerg return l; 12820253Sjoerg} 12920253Sjoerg 13020253Sjoerg 13120253Sjoergint 13220253Sjoergfmtpwent(char *buf, struct passwd * pwd) 13320253Sjoerg{ 13420253Sjoerg return fmtpwentry(buf, pwd, PWF_STANDARD); 13520253Sjoerg} 13620253Sjoerg 13720253Sjoergstatic int 13820253Sjoergpw_update(struct passwd * pwd, char const * user, int mode) 13920253Sjoerg{ 14020253Sjoerg int rc = 0; 14120253Sjoerg 14244229Sdavidn ENDPWENT(); 14320253Sjoerg 14420253Sjoerg /* 14520253Sjoerg * First, let's check the see if the database is alright 14633259Swosch * Note: -C is only available in FreeBSD 2.2 and above 14720253Sjoerg */ 14820267Sjoerg#ifdef HAVE_PWDB_C 149180565Sjhb rc = pwdb("-C", (char *)NULL); /* Check only */ 150180565Sjhb if (rc == 0) { 15120267Sjoerg#else 15233259Swosch { /* No -C */ 15320267Sjoerg#endif 15461758Sdavidn char pfx[PWBUFSZ]; 15520747Sdavidn char pwbuf[PWBUFSZ]; 15661758Sdavidn int l = snprintf(pfx, PWBUFSZ, "%s:", user); 15761758Sdavidn#ifdef HAVE_PWDB_U 15862097Sdavidn int isrename = pwd!=NULL && strcmp(user, pwd->pw_name); 15961758Sdavidn#endif 16020253Sjoerg 16120253Sjoerg /* 16220253Sjoerg * Update the passwd file first 16320253Sjoerg */ 16420253Sjoerg if (pwd == NULL) 16520253Sjoerg *pwbuf = '\0'; 16620253Sjoerg else 16720253Sjoerg fmtpwentry(pwbuf, pwd, PWF_PASSWD); 16820253Sjoerg 16981982Sbrian if (l < 0) 17081977Sbrian l = 0; 17152502Sdavidn rc = fileupdate(getpwpath(_PASSWD), 0644, pwbuf, pfx, l, mode); 17252502Sdavidn if (rc == 0) { 17352502Sdavidn 17420253Sjoerg /* 17520253Sjoerg * Then the master.passwd file 17620253Sjoerg */ 17720253Sjoerg if (pwd != NULL) 17820253Sjoerg fmtpwentry(pwbuf, pwd, PWF_MASTER); 17988280Snectar rc = fileupdate(getpwpath(_MASTERPASSWD), 0600, pwbuf, pfx, l, mode); 18052511Sdavidn if (rc == 0) { 18161758Sdavidn#ifdef HAVE_PWDB_U 18261758Sdavidn if (mode == UPD_DELETE || isrename) 18361758Sdavidn#endif 18452502Sdavidn rc = pwdb(NULL); 18561758Sdavidn#ifdef HAVE_PWDB_U 18650653Ssheldonh else 187136087Sstefanf rc = pwdb("-u", user, (char *)NULL); 18861758Sdavidn#endif 18950653Ssheldonh } 19020253Sjoerg } 19120253Sjoerg } 19220253Sjoerg return rc; 19320253Sjoerg} 19420253Sjoerg 19520253Sjoergint 19620253Sjoergaddpwent(struct passwd * pwd) 19720253Sjoerg{ 19820253Sjoerg return pw_update(pwd, pwd->pw_name, UPD_CREATE); 19920253Sjoerg} 20020253Sjoerg 20120253Sjoergint 20220253Sjoergchgpwent(char const * login, struct passwd * pwd) 20320253Sjoerg{ 20420253Sjoerg return pw_update(pwd, login, UPD_REPLACE); 20520253Sjoerg} 20620253Sjoerg 20720253Sjoergint 20820253Sjoergdelpwent(struct passwd * pwd) 20920253Sjoerg{ 21020253Sjoerg return pw_update(NULL, pwd->pw_name, UPD_DELETE); 21120253Sjoerg} 212