Beispiel #1
0
static          uid_t
pw_uidpolicy(struct userconf * cnf, struct cargs * args)
{
	struct passwd  *pwd;
	uid_t           uid = (uid_t) - 1;
	struct carg    *a_uid = getarg(args, 'u');

	/*
	 * Check the given uid, if any
	 */
	if (a_uid != NULL) {
		uid = (uid_t) atol(a_uid->val);

		if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL)
			errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid);
	} else {
		struct bitmap   bm;

		/*
		 * We need to allocate the next available uid under one of
		 * two policies a) Grab the first unused uid b) Grab the
		 * highest possible unused uid
		 */
		if (cnf->min_uid >= cnf->max_uid) {	/* Sanity
							 * claus^H^H^H^Hheck */
			cnf->min_uid = 1000;
			cnf->max_uid = 32000;
		}
		bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);

		/*
		 * Now, let's fill the bitmap from the password file
		 */
		SETPWENT();
		while ((pwd = GETPWENT()) != NULL)
			if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid)
				bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
		ENDPWENT();

		/*
		 * Then apply the policy, with fallback to reuse if necessary
		 */
		if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
			uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);

		/*
		 * Another sanity check
		 */
		if (uid < cnf->min_uid || uid > cnf->max_uid)
			errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used");
		bm_dealloc(&bm);
	}
	return uid;
}
Beispiel #2
0
int
pw_user(struct userconf * cnf, int mode, struct cargs * args)
{
	int	        rc, edited = 0;
	char           *p = NULL;
	char					 *passtmp;
	struct carg    *a_name;
	struct carg    *a_uid;
	struct carg    *arg;
	struct passwd  *pwd = NULL;
	struct group   *grp;
	struct stat     st;
	char            line[_PASSWORD_LEN+1];
	FILE	       *fp;
	char *dmode_c;
	void *set = NULL;

	static struct passwd fakeuser =
	{
		NULL,
		"*",
		-1,
		-1,
		0,
		"",
		"User &",
		"/nonexistent",
		"/bin/sh",
		0
#if defined(__FreeBSD__)
		,0
#endif
	};


	/*
	 * With M_NEXT, we only need to return the
	 * next uid to stdout
	 */
	if (mode == M_NEXT)
	{
		uid_t next = pw_uidpolicy(cnf, args);
		if (getarg(args, 'q'))
			return next;
		printf("%ld:", (long)next);
		pw_group(cnf, mode, args);
		return EXIT_SUCCESS;
	}

	/*
	 * We can do all of the common legwork here
	 */

	if ((arg = getarg(args, 'b')) != NULL) {
		cnf->home = arg->val;
	}

	if ((arg = getarg(args, 'M')) != NULL) {
		dmode_c = arg->val;
		if ((set = setmode(dmode_c)) == NULL)
			errx(EX_DATAERR, "invalid directory creation mode '%s'",
			    dmode_c);
		cnf->homemode = getmode(set, _DEF_DIRMODE);
		free(set);
	}

	/*
	 * If we'll need to use it or we're updating it,
	 * then create the base home directory if necessary
	 */
	if (arg != NULL || getarg(args, 'm') != NULL) {
		int	l = strlen(cnf->home);

		if (l > 1 && cnf->home[l-1] == '/')	/* Shave off any trailing path delimiter */
			cnf->home[--l] = '\0';

		if (l < 2 || *cnf->home != '/')		/* Check for absolute path name */
			errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home);

		if (stat(cnf->home, &st) == -1) {
			char	dbuf[MAXPATHLEN];

			/*
			 * This is a kludge especially for Joerg :)
			 * If the home directory would be created in the root partition, then
			 * we really create it under /usr which is likely to have more space.
			 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
			 */
			if (strchr(cnf->home+1, '/') == NULL) {
				strcpy(dbuf, "/usr");
				strncat(dbuf, cnf->home, MAXPATHLEN-5);
				if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) {
					chown(dbuf, 0, 0);
					/*
					 * Skip first "/" and create symlink:
					 * /home -> usr/home
					 */
					symlink(dbuf+1, cnf->home);
				}
				/* If this falls, fall back to old method */
			}
			strlcpy(dbuf, cnf->home, sizeof(dbuf));
			p = dbuf;
			if (stat(dbuf, &st) == -1) {
				while ((p = strchr(p + 1, '/')) != NULL) {
					*p = '\0';
					if (stat(dbuf, &st) == -1) {
						if (mkdir(dbuf, _DEF_DIRMODE) == -1)
							goto direrr;
						chown(dbuf, 0, 0);
					} else if (!S_ISDIR(st.st_mode))
						errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf);
					*p = '/';
				}
			}
			if (stat(dbuf, &st) == -1) {
				if (mkdir(dbuf, _DEF_DIRMODE) == -1) {
				direrr:	err(EX_OSFILE, "mkdir '%s'", dbuf);
				}
				chown(dbuf, 0, 0);
			}
		} else if (!S_ISDIR(st.st_mode))
			errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home);
	}

	if ((arg = getarg(args, 'e')) != NULL)
		cnf->expire_days = atoi(arg->val);

	if ((arg = getarg(args, 'y')) != NULL)
		cnf->nispasswd = arg->val;

	if ((arg = getarg(args, 'p')) != NULL && arg->val)
		cnf->password_days = atoi(arg->val);

	if ((arg = getarg(args, 'g')) != NULL) {
		if (!*(p = arg->val))	/* Handle empty group list specially */
			cnf->default_group = "";
		else {
			if ((grp = GETGRNAM(p)) == NULL) {
				if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
					errx(EX_NOUSER, "group `%s' does not exist", p);
			}
			cnf->default_group = newstr(grp->gr_name);
		}
	}
	if ((arg = getarg(args, 'L')) != NULL)
		cnf->default_class = pw_checkname((u_char *)arg->val, 0);

	if ((arg = getarg(args, 'G')) != NULL && arg->val) {
		int i = 0;

		for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
			if ((grp = GETGRNAM(p)) == NULL) {
				if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
					errx(EX_NOUSER, "group `%s' does not exist", p);
			}
			if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
				cnf->groups[i++] = newstr(grp->gr_name);
		}
		while (i < cnf->numgroups)
			cnf->groups[i++] = NULL;
	}

	if ((arg = getarg(args, 'k')) != NULL) {
		if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode))
			errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir);
	}

	if ((arg = getarg(args, 's')) != NULL)
		cnf->shell_default = arg->val;

	if ((arg = getarg(args, 'w')) != NULL)
		cnf->default_password = boolean_val(arg->val, cnf->default_password);
	if (mode == M_ADD && getarg(args, 'D')) {
		if (getarg(args, 'n') != NULL)
			errx(EX_DATAERR, "can't combine `-D' with `-n name'");
		if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
			if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
				cnf->min_uid = 1000;
			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
				cnf->max_uid = 32000;
		}
		if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
			if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
				cnf->min_gid = 1000;
			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
				cnf->max_gid = 32000;
		}

		arg = getarg(args, 'C');
		if (write_userconfig(arg ? arg->val : NULL))
			return EXIT_SUCCESS;
		warn("config update");
		return EX_IOERR;
	}

	if (mode == M_PRINT && getarg(args, 'a')) {
		int             pretty = getarg(args, 'P') != NULL;
		int		v7 = getarg(args, '7') != NULL;
		SETPWENT();
		while ((pwd = GETPWENT()) != NULL)
			print_user(pwd, pretty, v7);
		ENDPWENT();
		return EXIT_SUCCESS;
	}

	if ((a_name = getarg(args, 'n')) != NULL)
		pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0));
	a_uid = getarg(args, 'u');

	if (a_uid == NULL) {
		if (a_name == NULL)
			errx(EX_DATAERR, "user name or id required");

		/*
		 * Determine whether 'n' switch is name or uid - we don't
		 * really don't really care which we have, but we need to
		 * know.
		 */
		if (mode != M_ADD && pwd == NULL
		    && strspn(a_name->val, "0123456789") == strlen(a_name->val)
		    && *a_name->val) {
			(a_uid = a_name)->ch = 'u';
			a_name = NULL;
		}
	}

	/*
	 * Update, delete & print require that the user exists
	 */
	if (mode == M_UPDATE || mode == M_DELETE ||
	    mode == M_PRINT  || mode == M_LOCK   || mode == M_UNLOCK) {

		if (a_name == NULL && pwd == NULL)	/* Try harder */
			pwd = GETPWUID(atoi(a_uid->val));

		if (pwd == NULL) {
			if (mode == M_PRINT && getarg(args, 'F')) {
				fakeuser.pw_name = a_name ? a_name->val : "nouser";
				fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1;
				return print_user(&fakeuser,
						  getarg(args, 'P') != NULL,
						  getarg(args, '7') != NULL);
			}
			if (a_name == NULL)
				errx(EX_NOUSER, "no such uid `%s'", a_uid->val);
			errx(EX_NOUSER, "no such user `%s'", a_name->val);
		}

		if (a_name == NULL)	/* May be needed later */
			a_name = addarg(args, 'n', newstr(pwd->pw_name));

		/*
		 * The M_LOCK and M_UNLOCK functions simply add or remove
		 * a "*LOCKED*" prefix from in front of the password to
		 * prevent it decoding correctly, and therefore prevents
		 * access. Of course, this only prevents access via
		 * password authentication (not ssh, kerberos or any
		 * other method that does not use the UNIX password) but
		 * that is a known limitation.
		 */

		if (mode == M_LOCK) {
			if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0)
				errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name);
			passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str));
			if (passtmp == NULL)	/* disaster */
				errx(EX_UNAVAILABLE, "out of memory");
			strcpy(passtmp, locked_str);
			strcat(passtmp, pwd->pw_passwd);
			pwd->pw_passwd = passtmp;
			edited = 1;
		} else if (mode == M_UNLOCK) {
			if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0)
				errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name);
			pwd->pw_passwd += sizeof(locked_str)-1;
			edited = 1;
		} else if (mode == M_DELETE) {
			/*
			 * Handle deletions now
			 */
			char            file[MAXPATHLEN];
			char            home[MAXPATHLEN];
			uid_t           uid = pwd->pw_uid;
			struct group    *gr;
			char            grname[LOGNAMESIZE];

			if (strcmp(pwd->pw_name, "root") == 0)
				errx(EX_DATAERR, "cannot remove user 'root'");

			if (!PWALTDIR()) {
				/*
				 * Remove opie record from /etc/opiekeys
		        	 */

				rmopie(pwd->pw_name);

				/*
				 * Remove crontabs
				 */
				snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name);
				if (access(file, F_OK) == 0) {
					sprintf(file, "crontab -u %s -r", pwd->pw_name);
					system(file);
				}
			}
			/*
			 * Save these for later, since contents of pwd may be
			 * invalidated by deletion
			 */
			sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
			strlcpy(home, pwd->pw_dir, sizeof(home));
			gr = GETGRGID(pwd->pw_gid);
			if (gr != NULL)
				strlcpy(grname, gr->gr_name, LOGNAMESIZE);
			else
				grname[0] = '\0';

			rc = delpwent(pwd);
			if (rc == -1)
				err(EX_IOERR, "user '%s' does not exist", pwd->pw_name);
			else if (rc != 0) {
				warn("passwd update");
				return EX_IOERR;
			}

			if (cnf->nispasswd && *cnf->nispasswd=='/') {
				rc = delnispwent(cnf->nispasswd, a_name->val);
				if (rc == -1)
					warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name);
				else if (rc != 0)
					warn("WARNING: NIS passwd update");
				/* non-fatal */
			}

			grp = GETGRNAM(a_name->val);
			if (grp != NULL &&
			    (grp->gr_mem == NULL || *grp->gr_mem == NULL) &&
			    strcmp(a_name->val, grname) == 0)
				delgrent(GETGRNAM(a_name->val));
			SETGRENT();
			while ((grp = GETGRENT()) != NULL) {
				int i, j;
				char group[MAXLOGNAME];
				if (grp->gr_mem != NULL) {
					for (i = 0; grp->gr_mem[i] != NULL; i++) {
						if (!strcmp(grp->gr_mem[i], a_name->val)) {
							for (j = i; grp->gr_mem[j] != NULL; j++)
								grp->gr_mem[j] = grp->gr_mem[j+1];
							strlcpy(group, grp->gr_name, MAXLOGNAME);
							chggrent(group, grp);
						}
					}
				}
			}
			ENDGRENT();

			pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);

			if (!PWALTDIR()) {
				/*
				 * Remove mail file
				 */
				remove(file);

				/*
				 * Remove at jobs
				 */
				if (getpwuid(uid) == NULL)
					rmat(uid);

				/*
				 * Remove home directory and contents
				 */
				if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) {
					if (stat(home, &st) != -1) {
						rm_r(home, uid);
						pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved",
						       a_name->val, (long) uid, home,
						       stat(home, &st) == -1 ? "" : "not completely ");
					}
				}
			}
			return EXIT_SUCCESS;
		} else if (mode == M_PRINT)
			return print_user(pwd,
					  getarg(args, 'P') != NULL,
					  getarg(args, '7') != NULL);

		/*
		 * The rest is edit code
		 */
		if ((arg = getarg(args, 'l')) != NULL) {
			if (strcmp(pwd->pw_name, "root") == 0)
				errx(EX_DATAERR, "can't rename `root' account");
			pwd->pw_name = pw_checkname((u_char *)arg->val, 0);
			edited = 1;
		}

		if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) {
			pwd->pw_uid = (uid_t) atol(arg->val);
			edited = 1;
			if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
				errx(EX_DATAERR, "can't change uid of `root' account");
			if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
				warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
		}

		if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) {	/* Already checked this */
			gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid;
			if (newgid != pwd->pw_gid) {
				edited = 1;
				pwd->pw_gid = newgid;
			}
		}

		if ((arg = getarg(args, 'p')) != NULL) {
			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
				if (pwd->pw_change != 0) {
					pwd->pw_change = 0;
					edited = 1;
				}
			}
			else {
				time_t          now = time(NULL);
				time_t          expire = parse_date(now, arg->val);

				if (pwd->pw_change != expire) {
					pwd->pw_change = expire;
					edited = 1;
				}
			}
		}

		if ((arg = getarg(args, 'e')) != NULL) {
			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
				if (pwd->pw_expire != 0) {
					pwd->pw_expire = 0;
					edited = 1;
				}
			}
			else {
				time_t          now = time(NULL);
				time_t          expire = parse_date(now, arg->val);

				if (pwd->pw_expire != expire) {
					pwd->pw_expire = expire;
					edited = 1;
				}
			}
		}

		if ((arg = getarg(args, 's')) != NULL) {
			char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
			if (shell == NULL)
				shell = "";
			if (strcmp(shell, pwd->pw_shell) != 0) {
				pwd->pw_shell = shell;
				edited = 1;
			}
		}

		if (getarg(args, 'L')) {
			if (cnf->default_class == NULL)
				cnf->default_class = "";
			if (strcmp(pwd->pw_class, cnf->default_class) != 0) {
				pwd->pw_class = cnf->default_class;
				edited = 1;
			}
		}

		if ((arg  = getarg(args, 'd')) != NULL) {
			if (strcmp(pwd->pw_dir, arg->val))
				edited = 1;
			if (stat(pwd->pw_dir = arg->val, &st) == -1) {
				if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0)
				  warnx("WARNING: home `%s' does not exist", pwd->pw_dir);
			} else if (!S_ISDIR(st.st_mode))
				warnx("WARNING: home `%s' is not a directory", pwd->pw_dir);
		}

		if ((arg = getarg(args, 'w')) != NULL &&
		    getarg(args, 'h') == NULL && getarg(args, 'H') == NULL) {
			login_cap_t *lc;

			lc = login_getpwclass(pwd);
			if (lc == NULL ||
			    login_setcryptfmt(lc, "sha512", NULL) == NULL)
				warn("setting crypt(3) format");
			login_close(lc);
			pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
			edited = 1;
		}

	} else {
		login_cap_t *lc;

		/*
		 * Add code
		 */

		if (a_name == NULL)	/* Required */
			errx(EX_DATAERR, "login name required");
		else if ((pwd = GETPWNAM(a_name->val)) != NULL)	/* Exists */
			errx(EX_DATAERR, "login name `%s' already exists", a_name->val);

		/*
		 * Now, set up defaults for a new user
		 */
		pwd = &fakeuser;
		pwd->pw_name = a_name->val;
		pwd->pw_class = cnf->default_class ? cnf->default_class : "";
		pwd->pw_uid = pw_uidpolicy(cnf, args);
		pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid);
		pwd->pw_change = pw_pwdpolicy(cnf, args);
		pwd->pw_expire = pw_exppolicy(cnf, args);
		pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
		pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
		lc = login_getpwclass(pwd);
		if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL)
			warn("setting crypt(3) format");
		login_close(lc);
		pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
		edited = 1;

		if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
			warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name);
	}

	/*
	 * Shared add/edit code
	 */
	if ((arg = getarg(args, 'c')) != NULL) {
		char	*gecos = pw_checkname((u_char *)arg->val, 1);
		if (strcmp(pwd->pw_gecos, gecos) != 0) {
			pwd->pw_gecos = gecos;
			edited = 1;
		}
	}

	if ((arg = getarg(args, 'h')) != NULL ||
	    (arg = getarg(args, 'H')) != NULL) {
		if (strcmp(arg->val, "-") == 0) {
			if (!pwd->pw_passwd || *pwd->pw_passwd != '*') {
				pwd->pw_passwd = "*";	/* No access */
				edited = 1;
			}
		} else {
			int             fd = atoi(arg->val);
			int		precrypt = (arg->ch == 'H');
			int             b;
			int             istty = isatty(fd);
			struct termios  t;
			login_cap_t	*lc;

			if (istty) {
				if (tcgetattr(fd, &t) == -1)
					istty = 0;
				else {
					struct termios  n = t;

					/* Disable echo */
					n.c_lflag &= ~(ECHO);
					tcsetattr(fd, TCSANOW, &n);
					printf("%s%spassword for user %s:",
					     (mode == M_UPDATE) ? "new " : "",
					     precrypt ? "encrypted " : "",
					     pwd->pw_name);
					fflush(stdout);
				}
			}
			b = read(fd, line, sizeof(line) - 1);
			if (istty) {	/* Restore state */
				tcsetattr(fd, TCSANOW, &t);
				fputc('\n', stdout);
				fflush(stdout);
			}
			if (b < 0) {
				warn("-%c file descriptor", precrypt ? 'H' :
				    'h');
				return EX_IOERR;
			}
			line[b] = '\0';
			if ((p = strpbrk(line, "\r\n")) != NULL)
				*p = '\0';
			if (!*line)
				errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
			if (precrypt) {
				if (strchr(line, ':') != NULL)
					return EX_DATAERR;
				pwd->pw_passwd = line;
			} else {
				lc = login_getpwclass(pwd);
				if (lc == NULL ||
				    login_setcryptfmt(lc, "sha512", NULL) == NULL)
					warn("setting crypt(3) format");
				login_close(lc);
				pwd->pw_passwd = pw_pwcrypt(line);
			}
			edited = 1;
		}
	}

	/*
	 * Special case: -N only displays & exits
	 */
	if (getarg(args, 'N') != NULL)
		return print_user(pwd,
				  getarg(args, 'P') != NULL,
				  getarg(args, '7') != NULL);

	if (mode == M_ADD) {
		edited = 1;	/* Always */
		rc = addpwent(pwd);
		if (rc == -1) {
			warnx("user '%s' already exists", pwd->pw_name);
			return EX_IOERR;
		} else if (rc != 0) {
			warn("passwd file update");
			return EX_IOERR;
		}
		if (cnf->nispasswd && *cnf->nispasswd=='/') {
			rc = addnispwent(cnf->nispasswd, pwd);
			if (rc == -1)
				warnx("User '%s' already exists in NIS passwd", pwd->pw_name);
			else
				warn("NIS passwd update");
			/* NOTE: we treat NIS-only update errors as non-fatal */
		}
	} else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) {
		if (edited) {	/* Only updated this if required */
			rc = chgpwent(a_name->val, pwd);
			if (rc == -1) {
				warnx("user '%s' does not exist (NIS?)", pwd->pw_name);
				return EX_IOERR;
			} else if (rc != 0) {
				warn("passwd file update");
				return EX_IOERR;
			}
			if ( cnf->nispasswd && *cnf->nispasswd=='/') {
				rc = chgnispwent(cnf->nispasswd, a_name->val, pwd);
				if (rc == -1)
					warn("User '%s' not found in NIS passwd", pwd->pw_name);
				else
					warn("NIS passwd update");
				/* NOTE: NIS-only update errors are not fatal */
			}
		}
	}

	/*
	 * Ok, user is created or changed - now edit group file
	 */

	if (mode == M_ADD || getarg(args, 'G') != NULL) {
		int i;
		for (i = 0; cnf->groups[i] != NULL; i++) {
			grp = GETGRNAM(cnf->groups[i]);
			grp = gr_add(grp, pwd->pw_name);
			/*
			 * grp can only be NULL in 2 cases:
			 * - the new member is already a member
			 * - a problem with memory occurs
			 * in both cases we want to skip now.
			 */
			if (grp == NULL)
				continue;
			chggrent(cnf->groups[i], grp);
			free(grp);
		}
	}


	/* go get a current version of pwd */
	pwd = GETPWNAM(a_name->val);
	if (pwd == NULL) {
		/* This will fail when we rename, so special case that */
		if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) {
			a_name->val = arg->val;		/* update new name */
			pwd = GETPWNAM(a_name->val);	/* refetch renamed rec */
		}
	}
	if (pwd == NULL)	/* can't go on without this */
		errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val);

	grp = GETGRGID(pwd->pw_gid);
	pw_log(cnf, mode, W_USER, "%s(%ld):%s(%ld):%s:%s:%s",
	       pwd->pw_name, (long) pwd->pw_uid,
	    grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1),
	       pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);

	/*
	 * If adding, let's touch and chown the user's mail file. This is not
	 * strictly necessary under BSD with a 0755 maildir but it also
	 * doesn't hurt anything to create the empty mailfile
	 */
	if (mode == M_ADD) {
		if (!PWALTDIR()) {
			sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
			close(open(line, O_RDWR | O_CREAT, 0600));	/* Preserve contents &
									 * mtime */
			chown(line, pwd->pw_uid, pwd->pw_gid);
		}
	}

	/*
	 * Let's create and populate the user's home directory. Note
	 * that this also `works' for editing users if -m is used, but
	 * existing files will *not* be overwritten.
	 */
	if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
		copymkdir(pwd->pw_dir, cnf->dotdir, cnf->homemode, pwd->pw_uid, pwd->pw_gid);
		pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
		       pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
	}


	/*
	 * Finally, send mail to the new user as well, if we are asked to
	 */
	if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
		FILE           *pfp = popen(_PATH_SENDMAIL " -t", "w");
		
		if (pfp == NULL)
			warn("sendmail");
		else {
			fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
			while (fgets(line, sizeof(line), fp) != NULL) {
				/* Do substitutions? */
				fputs(line, pfp);
			}
			pclose(pfp);
			pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
			    pwd->pw_name, (long) pwd->pw_uid);
		}
		fclose(fp);
	}

	return EXIT_SUCCESS;
}
Beispiel #3
0
int
editgroups(char *name, char **groups)
{
	int             rc = 0;
	int             infd;
	char		groupfile[MAXPATHLEN];
	char		grouptmp[MAXPATHLEN];

	strncpy(groupfile, getgrpath(_GROUP), MAXPATHLEN - 5);
	groupfile[MAXPATHLEN - 5] = '\0';
	strcpy(grouptmp, groupfile);
	strcat(grouptmp, ".new");

	if ((infd = open(groupfile, O_RDWR | O_CREAT | O_EXLOCK, 0644)) != -1) {
		FILE           *infp;

		if ((infp = fdopen(infd, "r+")) == NULL)
			close(infd);
		else {
			int             outfd;

			if ((outfd = open(grouptmp, O_RDWR | O_CREAT | O_TRUNC, 0644)) != -1) {
				FILE           *outfp;

				if ((outfp = fdopen(outfd, "w+")) == NULL)
					close(outfd);
				else {
					int		linelen = PWBUFSZ;
					int		outlen =  PWBUFSZ;
					int		memlen = 200; /* Arbitrary */
					char           *line = malloc(linelen);
					char           *outl = malloc(outlen);
					char	      **mems = malloc(memlen * sizeof(char *));
					int		namlen = strlen(name);

					if (line == NULL || outl == NULL || mems == NULL) {
					    mem_abort:
						rc = 0;
					} else {
						while (fgets(line, linelen, infp) != NULL) {
							char           *p;
							int		l;

							while ((p = strchr(line, '\n')) == NULL)
							{
								if (extendline(&line, &linelen, linelen + PWBUFSZ) == -1) {
									goto mem_abort;
								}
								l = strlen(line);
								if (fgets(line + l, linelen - l, infp) == NULL)
									break;	/* No newline terminator on last line */
							}
							l = strlen(line) + namlen + 1;
							if (extendline(&outl, &outlen, l) == -1) {
								goto mem_abort;
							}
							if (*line == '#')
								strcpy(outl, line);
							else if (*line == '\n')
								*outl = '\0';
							else {
								int             i,
									        mno = 0;
								char           *cp = line;
								char const     *sep = ":\n";
								struct group    grp;

								memset(&grp, 0, sizeof grp);
								for (i = 0; (p = strsep(&cp, sep)) != NULL; i++) {
									switch (i) {
									case 0:	/* Group name */
										grp.gr_name = p;
										break;
									case 1:	/* Group password */
										grp.gr_passwd = p;
										break;
									case 2:	/* Group id */
										grp.gr_gid = atoi(p);
										break;
									case 3:	/* Member list */
										cp = p;
										sep = ",\n";
										break;
									default:	/* Individual members */
										if (*p) {
											if (extendarray(&mems, &memlen, mno + 2) == -1) {
												goto mem_abort;
											}
											mems[mno++] = p;
										}
										break;
									}
								}
								if (i < 2)	/* Bail out - insufficient fields */
									continue;

								grp.gr_mem = mems;
								for (i = mno; i < memlen; i++)
									mems[i] = NULL;

								/*
								 * Delete from group, or add to group?
								 */
								if (groups == NULL || isingroup(grp.gr_name, groups) == -1) {	/* Delete */
									int             idx;

									while ((idx = isingroup(name, mems)) != -1) {
										for (i = idx; i < (memlen - 1); i++)
											mems[i] = mems[i + 1];
										mems[i] = NULL;
										--mno;
									}
									/*
									 * Special case - deleting user and group may be user's own
									 */
									if (groups == NULL && mems[0] == NULL && strcmp(name, grp.gr_name) == 0) {
										/*
										 * First, make _sure_ we don't have other members
										 */
										struct passwd  *pwd;

										SETPWENT();
										while ((pwd = GETPWENT()) != NULL && (gid_t)pwd->pw_gid != (gid_t)grp.gr_gid);
										ENDPWENT();
										if (pwd == NULL)	/* No members at all */
											continue;	/* Drop the group */
									}
								} else if (isingroup(name, mems) == -1) {
									if (extendarray(&mems, &memlen, mno + 2) == -1) {
										goto mem_abort;
									}
									grp.gr_mem = mems;    /* May have realloced() */
									mems[mno++] = name;
									mems[mno  ] = NULL;
								}
								fmtgrentry(&outl, &outlen, &grp, PWF_GROUP);
							}
							fputs(outl, outfp);
						}
						if (fflush(outfp) != EOF) {
							rc = 1;

							/*
							 * Copy data back into the original file and truncate
							 */
							rewind(infp);
							rewind(outfp);
							while (fgets(outl, outlen, outfp) != NULL)
								fputs(outl, infp);

							/*
							 * This is a gross hack, but we may have corrupted the
							 * original file.
							 */
							if (fflush(infp) == EOF || ferror(infp))
								rc = rename(grouptmp, groupfile) == 0;
							else
								ftruncate(infd, ftell(infp));
						}
					}
					free(mems);
					free(outl);
			    		free(line);
					fclose(outfp);
				}
				remove(grouptmp);
			}
			fclose(infp);
		}
	}
	return rc;
}