示例#1
0
/*
 * Delete requested members from a group.
 */
static void
delete_members(char ***members, int *grmembers, int *i, struct carg *arg,
    struct group *grp)
{
	bool matchFound;
	char *user;
	char *valueCopy;
	char *valuePtr;
	int k;
	struct passwd *pwd;

	if (grp->gr_mem == NULL)
		return;

	k = 0;
	while (grp->gr_mem[k] != NULL) {
		matchFound = false;
		if ((valueCopy = strdup(arg->val)) == NULL)
			errx(EX_UNAVAILABLE, "out of memory");
		valuePtr = valueCopy;
		while ((user = strsep(&valuePtr, ", \t")) != NULL) {
			pwd = lookup_pwent(user);
			if (strcmp(grp->gr_mem[k], pwd->pw_name) == 0) {
				matchFound = true;
				break;
			}
		}
		free(valueCopy);

		if (!matchFound &&
		    extendarray(members, grmembers, *i + 2) != -1)
			(*members)[(*i)++] = grp->gr_mem[k];

		k++;
	}

	return;
}
示例#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;
}
示例#3
0
int
pw_group(struct userconf * cnf, int mode, struct cargs * args)
{
	int		rc;
	struct carg    *a_name = getarg(args, 'n');
	struct carg    *a_gid = getarg(args, 'g');
	struct carg    *arg;
	struct group   *grp = NULL;
	int	        grmembers = 0;
	char           **members = NULL;

	static struct group fakegroup =
	{
		"nogroup",
		"*",
		-1,
		NULL
	};

	if (mode == M_LOCK || mode == M_UNLOCK)
		errx(EX_USAGE, "'lock' command is not available for groups");

	/*
	 * With M_NEXT, we only need to return the
	 * next gid to stdout
	 */
	if (mode == M_NEXT) {
		gid_t next = gr_gidpolicy(cnf, args);
		if (getarg(args, 'q'))
			return next;
		printf("%ld\n", (long)next);
		return EXIT_SUCCESS;
	}

	if (mode == M_PRINT && getarg(args, 'a')) {
		int             pretty = getarg(args, 'P') != NULL;

		SETGRENT();
		while ((grp = GETGRENT()) != NULL)
			print_group(grp, pretty);
		ENDGRENT();
		return EXIT_SUCCESS;
	}
	if (a_gid == NULL) {
		if (a_name == NULL)
			errx(EX_DATAERR, "group name or id required");

		if (mode != M_ADD && grp == NULL && isdigit((unsigned char)*a_name->val)) {
			(a_gid = a_name)->ch = 'g';
			a_name = NULL;
		}
	}
	grp = (a_name != NULL) ? GETGRNAM(a_name->val) : GETGRGID((gid_t) atoi(a_gid->val));

	if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) {
		if (a_name == NULL && grp == NULL)	/* Try harder */
			grp = GETGRGID(atoi(a_gid->val));

		if (grp == NULL) {
			if (mode == M_PRINT && getarg(args, 'F')) {
				char	*fmems[1];
				fmems[0] = NULL;
				fakegroup.gr_name = a_name ? a_name->val : "nogroup";
				fakegroup.gr_gid = a_gid ? (gid_t) atol(a_gid->val) : -1;
				fakegroup.gr_mem = fmems;
				return print_group(&fakegroup, getarg(args, 'P') != NULL);
			}
			errx(EX_DATAERR, "unknown group `%s'", a_name ? a_name->val : a_gid->val);
		}
		if (a_name == NULL)	/* Needed later */
			a_name = addarg(args, 'n', grp->gr_name);

		/*
		 * Handle deletions now
		 */
		if (mode == M_DELETE) {
			gid_t           gid = grp->gr_gid;

			rc = delgrent(grp);
			if (rc == -1)
				err(EX_IOERR, "group '%s' not available (NIS?)", grp->gr_name);
			else if (rc != 0) {
				warn("group update");
				return EX_IOERR;
			}
			pw_log(cnf, mode, W_GROUP, "%s(%ld) removed", a_name->val, (long) gid);
			return EXIT_SUCCESS;
		} else if (mode == M_PRINT)
			return print_group(grp, getarg(args, 'P') != NULL);

		if (a_gid)
			grp->gr_gid = (gid_t) atoi(a_gid->val);

		if ((arg = getarg(args, 'l')) != NULL)
			grp->gr_name = pw_checkname((u_char *)arg->val, 0);
	} else {
		if (a_name == NULL)	/* Required */
			errx(EX_DATAERR, "group name required");
		else if (grp != NULL)	/* Exists */
			errx(EX_DATAERR, "group name `%s' already exists", a_name->val);

		extendarray(&members, &grmembers, 200);
		members[0] = NULL;
		grp = &fakegroup;
		grp->gr_name = pw_checkname((u_char *)a_name->val, 0);
		grp->gr_passwd = "*";
		grp->gr_gid = gr_gidpolicy(cnf, args);
		grp->gr_mem = members;
	}

	/*
	 * This allows us to set a group password Group passwords is an
	 * antique idea, rarely used and insecure (no secure database) Should
	 * be discouraged, but it is apparently still supported by some
	 * software.
	 */

	if ((arg = getarg(args, 'h')) != NULL ||
	    (arg = getarg(args, 'H')) != NULL) {
		if (strcmp(arg->val, "-") == 0)
			grp->gr_passwd = "*";	/* No access */
		else {
			int             fd = atoi(arg->val);
			int		precrypt = (arg->ch == 'H');
			int             b;
			int             istty = isatty(fd);
			struct termios  t;
			char           *p, line[256];

			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("%sassword for group %s:", (mode == M_UPDATE) ? "New p" : "P", grp->gr_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("-h file descriptor");
				return EX_OSERR;
			}
			line[b] = '\0';
			if ((p = strpbrk(line, " \t\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;
				grp->gr_passwd = line;
			} else
				grp->gr_passwd = pw_pwcrypt(line);
		}
	}

	if (((arg = getarg(args, 'M')) != NULL ||
	    (arg = getarg(args, 'd')) != NULL ||
	    (arg = getarg(args, 'm')) != NULL) && arg->val) {
		int	i = 0;
		char   *p;
		struct passwd	*pwd;

		/* Make sure this is not stay NULL with -M "" */
		extendarray(&members, &grmembers, 200);
		if (arg->ch == 'd')
			delete_members(&members, &grmembers, &i, arg, grp);
		else if (arg->ch == 'm') {
			int	k = 0;

			if (grp->gr_mem != NULL) {
				while (grp->gr_mem[k] != NULL) {
					if (extendarray(&members, &grmembers, i + 2) != -1)
						members[i++] = grp->gr_mem[k];
					k++;
				}
			}
		}

		if (arg->ch != 'd')
			for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
				int	j;

				/*
				 * Check for duplicates
				 */
				pwd = lookup_pwent(p);
				for (j = 0; j < i && strcmp(members[j], pwd->pw_name) != 0; j++)
					;
				if (j == i && extendarray(&members, &grmembers, i + 2) != -1)
					members[i++] = newstr(pwd->pw_name);
			}
		while (i < grmembers)
			members[i++] = NULL;
		grp->gr_mem = members;
	}

	if (getarg(args, 'N') != NULL)
		return print_group(grp, getarg(args, 'P') != NULL);

	if (mode == M_ADD && (rc = addgrent(grp)) != 0) {
		if (rc == -1)
			warnx("group '%s' already exists", grp->gr_name);
		else
			warn("group update");
		return EX_IOERR;
	} else if (mode == M_UPDATE && (rc = chggrent(a_name->val, grp)) != 0) {
		if (rc == -1)
			warnx("group '%s' not available (NIS?)", grp->gr_name);
		else
			warn("group update");
		return EX_IOERR;
	}
	/* grp may have been invalidated */
	if ((grp = GETGRNAM(a_name->val)) == NULL)
		errx(EX_SOFTWARE, "group disappeared during update");

	pw_log(cnf, mode, W_GROUP, "%s(%ld)", grp->gr_name, (long) grp->gr_gid);

	free(members);

	return EXIT_SUCCESS;
}
示例#4
0
static struct group *
vnextgrent(char const * nam, gid_t gid, int doclose)
{
	struct group * gr = NULL;

	static char * grtmp = NULL;
	static int grlen = 0;
	static char ** mems = NULL;
	static int memlen = 0;

	extendline(&grtmp, &grlen, MAXPATHLEN);
	strlcpy(grtmp, getgrpath(_GROUP), MAXPATHLEN);

	if (grp_fp != NULL || (grp_fp = fopen(grtmp, "r")) != NULL) {
		int done = 0;

		static struct group grp;

		while (!done && fgets(grtmp, grlen, grp_fp) != NULL)
		{
			int i, quickout = 0;
			int mno = 0;
			char * q, * p;
			char * sep = ":\n";

			if ((p = strchr(grtmp, '\n')) == NULL) {
				int l;
				extendline(&grtmp, &grlen, grlen + PWBUFSZ);
				l = strlen(grtmp);
				if (fgets(grtmp + l, grlen - l, grp_fp) == NULL)
				  break;	/* No newline terminator on last line */
			}
			/* Skip comments and empty lines */
			if (*grtmp == '\n' || *grtmp == '#')
				continue;
			i = 0;
			q = p = grtmp;
			bzero(&grp, sizeof grp);
			extendarray(&mems, &memlen, 200);
			while (!quickout && (p = strsep(&q, sep)) != NULL) {
				switch (i++)
				{
				case 0:   /* groupname */
					grp.gr_name = p;
					if (nam) {
						if (strcmp(nam, p) == 0)
							done = 1;
						else
							quickout = 1;
					}
					break;
				case 1:   /* password */
					grp.gr_passwd = p;
					break;
				case 2:   /* gid */
					grp.gr_gid = atoi(p);
					if (gid != (gid_t)-1) {
						if (gid == (gid_t)grp.gr_gid)
							done = 1;
						else
							quickout = 1;
					} else if (nam == NULL)
						done = 1;
					break;
				case 3:
					q = p;
					sep = ",\n";
					break;
				default:
					if (*p) {
						extendarray(&mems, &memlen, mno + 2);
						mems[mno++] = p;
					}
					break;
				}
			}
			grp.gr_mem = mems;
			mems[mno] = NULL;
                }
		if (doclose)
			vendgrent();
		if (done && grp.gr_name) {
			gr = &grp;

			CKNULL(grp.gr_passwd);
		}
	}
	return gr;
}
示例#5
0
int
write_userconfig(char const * file)
{
	int             fd;

	if (file == NULL)
		file = _PATH_PW_CONF;

	if ((fd = open(file, O_CREAT | O_RDWR | O_TRUNC | O_EXLOCK, 0644)) != -1) {
		FILE           *fp;

		if ((fp = fdopen(fd, "w")) == NULL)
			close(fd);
		else {
			int             i, j, k;
			int		len = LNBUFSZ;
			char           *buf = malloc(len);

			for (i = _UC_NONE; i < _UC_FIELDS; i++) {
				int             quote = 1;
				char const     *val = buf;

				*buf = '\0';
				switch (i) {
				case _UC_DEFAULTPWD:
					val = boolean_str(config.default_password);
					break;
				case _UC_REUSEUID:
					val = boolean_str(config.reuse_uids);
					break;
				case _UC_REUSEGID:
					val = boolean_str(config.reuse_gids);
					break;
				case _UC_NISPASSWD:
					val = config.nispasswd ? config.nispasswd : "";
					quote = 0;
					break;
				case _UC_DOTDIR:
					val = config.dotdir ? config.dotdir : boolean_str(0);
					break;
				case _UC_NEWMAIL:
					val = config.newmail ? config.newmail : boolean_str(0);
					break;
				case _UC_LOGFILE:
					val = config.logfile ? config.logfile : boolean_str(0);
					break;
				case _UC_HOMEROOT:
					val = config.home;
					break;
				case _UC_HOMEMODE:
					sprintf(buf, "%04o", config.homemode);
					quote = 0;
					break;
				case _UC_SHELLPATH:
					val = config.shelldir;
					break;
				case _UC_SHELLS:
					for (j = k = 0; j < _UC_MAXSHELLS && system_shells[j] != NULL; j++) {
						char	lbuf[64];
						int	l = snprintf(lbuf, sizeof lbuf, "%s\"%s\"", k ? "," : "", system_shells[j]);
						if (l < 0)
							l = 0;
						if (l + k + 1 < len || extendline(&buf, &len, len + LNBUFSZ) != -1) {
							strcpy(buf + k, lbuf);
							k += l;
						}
					}
					quote = 0;
					break;
				case _UC_DEFAULTSHELL:
					val = config.shell_default ? config.shell_default : bourne_shell;
					break;
				case _UC_DEFAULTGROUP:
					val = config.default_group ? config.default_group : "";
					break;
				case _UC_EXTRAGROUPS:
					extendarray(&config.groups, &config.numgroups, 200);
					for (j = k = 0; j < config.numgroups && config.groups[j] != NULL; j++) {
						char	lbuf[64];
						int	l = snprintf(lbuf, sizeof lbuf, "%s\"%s\"", k ? "," : "", config.groups[j]);
						if (l < 0)
							l = 0;
						if (l + k + 1 < len || extendline(&buf, &len, len + 1024) != -1) {
							strcpy(buf + k, lbuf);
							k +=  l;
						}
					}
					quote = 0;
					break;
				case _UC_DEFAULTCLASS:
					val = config.default_class ? config.default_class : "";
					break;
				case _UC_MINUID:
					sprintf(buf, "%lu", (unsigned long) config.min_uid);
					quote = 0;
					break;
				case _UC_MAXUID:
					sprintf(buf, "%lu", (unsigned long) config.max_uid);
					quote = 0;
					break;
				case _UC_MINGID:
					sprintf(buf, "%lu", (unsigned long) config.min_gid);
					quote = 0;
					break;
				case _UC_MAXGID:
					sprintf(buf, "%lu", (unsigned long) config.max_gid);
					quote = 0;
					break;
				case _UC_EXPIRE:
					sprintf(buf, "%d", config.expire_days);
					quote = 0;
					break;
				case _UC_PASSWORD:
					sprintf(buf, "%d", config.password_days);
					quote = 0;
					break;
				case _UC_NONE:
					break;
				}

				if (comments[i])
					fputs(comments[i], fp);

				if (*kwds[i]) {
					if (quote)
						fprintf(fp, "%s = \"%s\"\n", kwds[i], val);
					else
						fprintf(fp, "%s = %s\n", kwds[i], val);
#if debugging
					printf("WROTE: %s = %s\n", kwds[i], val);
#endif
				}
			}
			free(buf);
			return fclose(fp) != EOF;
		}
	}
	return 0;
}
示例#6
0
struct userconf *
read_userconfig(char const * file)
{
	FILE	*fp;
	char	*buf, *p;
	size_t	linecap;
	ssize_t	linelen;

	buf = NULL;
	linecap = 0;

	extendarray(&config.groups, &config.numgroups, 200);
	memset(config.groups, 0, config.numgroups * sizeof(char *));
	if (file == NULL)
		file = _PATH_PW_CONF;

	if ((fp = fopen(file, "r")) != NULL) {
		while ((linelen = getline(&buf, &linecap, fp)) > 0) {
			if (*buf && (p = strtok(buf, " \t\r\n=")) != NULL && *p != '#') {
				static char const toks[] = " \t\r\n,=";
				char           *q = strtok(NULL, toks);
				int             i = 0;
				mode_t          *modeset;

				while (i < _UC_FIELDS && strcmp(p, kwds[i]) != 0)
					++i;
#if debugging
				if (i == _UC_FIELDS)
					printf("Got unknown kwd `%s' val=`%s'\n", p, q ? q : "");
				else
					printf("Got kwd[%s]=%s\n", p, q);
#endif
				switch (i) {
				case _UC_DEFAULTPWD:
					config.default_password = boolean_val(q, 1);
					break;
				case _UC_REUSEUID:
					config.reuse_uids = boolean_val(q, 0);
					break;
				case _UC_REUSEGID:
					config.reuse_gids = boolean_val(q, 0);
					break;
				case _UC_NISPASSWD:
					config.nispasswd = (q == NULL || !boolean_val(q, 1))
						? NULL : newstr(q);
					break;
				case _UC_DOTDIR:
					config.dotdir = (q == NULL || !boolean_val(q, 1))
						? NULL : newstr(q);
					break;
				case _UC_NEWMAIL:
					config.newmail = (q == NULL || !boolean_val(q, 1))
						? NULL : newstr(q);
					break;
				case _UC_LOGFILE:
					config.logfile = (q == NULL || !boolean_val(q, 1))
						? NULL : newstr(q);
					break;
				case _UC_HOMEROOT:
					config.home = (q == NULL || !boolean_val(q, 1))
						? "/home" : newstr(q);
					break;
				case _UC_HOMEMODE:
					modeset = setmode(q);
					config.homemode = (q == NULL || !boolean_val(q, 1))
						? _DEF_DIRMODE : getmode(modeset, _DEF_DIRMODE);
					free(modeset);
					break;
				case _UC_SHELLPATH:
					config.shelldir = (q == NULL || !boolean_val(q, 1))
						? "/bin" : newstr(q);
					break;
				case _UC_SHELLS:
					for (i = 0; i < _UC_MAXSHELLS && q != NULL; i++, q = strtok(NULL, toks))
						system_shells[i] = newstr(q);
					if (i > 0)
						while (i < _UC_MAXSHELLS)
							system_shells[i++] = NULL;
					break;
				case _UC_DEFAULTSHELL:
					config.shell_default = (q == NULL || !boolean_val(q, 1))
						? (char *) bourne_shell : newstr(q);
					break;
				case _UC_DEFAULTGROUP:
					q = unquote(q);
					config.default_group = (q == NULL || !boolean_val(q, 1) || GETGRNAM(q) == NULL)
						? NULL : newstr(q);
					break;
				case _UC_EXTRAGROUPS:
					for (i = 0; q != NULL; q = strtok(NULL, toks)) {
						if (extendarray(&config.groups, &config.numgroups, i + 2) != -1)
							config.groups[i++] = newstr(q);
					}
					if (i > 0)
						while (i < config.numgroups)
							config.groups[i++] = NULL;
					break;
				case _UC_DEFAULTCLASS:
					config.default_class = (q == NULL || !boolean_val(q, 1))
						? NULL : newstr(q);
					break;
				case _UC_MINUID:
					if ((q = unquote(q)) != NULL && isdigit(*q))
						config.min_uid = (uid_t) atol(q);
					break;
				case _UC_MAXUID:
					if ((q = unquote(q)) != NULL && isdigit(*q))
						config.max_uid = (uid_t) atol(q);
					break;
				case _UC_MINGID:
					if ((q = unquote(q)) != NULL && isdigit(*q))
						config.min_gid = (gid_t) atol(q);
					break;
				case _UC_MAXGID:
					if ((q = unquote(q)) != NULL && isdigit(*q))
						config.max_gid = (gid_t) atol(q);
					break;
				case _UC_EXPIRE:
					if ((q = unquote(q)) != NULL && isdigit(*q))
						config.expire_days = atoi(q);
					break;
				case _UC_PASSWORD:
					if ((q = unquote(q)) != NULL && isdigit(*q))
						config.password_days = atoi(q);
					break;
				case _UC_FIELDS:
				case _UC_NONE:
					break;
				}
			}
		}
		if (linecap > 0)
			free(buf);
		fclose(fp);
	}
	return &config;
}
示例#7
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;
}
示例#8
0
struct userconf *
read_userconfig(char const * file)
{
	FILE           *fp;

	extendarray(&config.groups, &config.numgroups, 200);
	memset(config.groups, 0, config.numgroups * sizeof(char *));
	if (file == NULL)
		file = _PATH_PW_CONF;
	if ((fp = fopen(file, "r")) != NULL) {
		int	    buflen = LNBUFSZ;
		char       *buf = malloc(buflen);

	nextline:
		while (fgets(buf, buflen, fp) != NULL) {
			char           *p;

			while ((p = strchr(buf, '\n')) == NULL) {
				int	  l;
				if (extendline(&buf, &buflen, buflen + LNBUFSZ) == -1) {
					int	ch;
					while ((ch = fgetc(fp)) != '\n' && ch != EOF);
					goto nextline;	/* Ignore it */
				}
				l = strlen(buf);
				if (fgets(buf + l, buflen - l, fp) == NULL)
					break;	/* Unterminated last line */
			}

			if (p != NULL)
				*p = '\0';

			if (*buf && (p = strtok(buf, " \t\r\n=")) != NULL && *p != '#') {
				static char const toks[] = " \t\r\n,=";
				char           *q = strtok(NULL, toks);
				int             i = 0;

				while (i < _UC_FIELDS && strcmp(p, kwds[i]) != 0)
					++i;
#if debugging
				if (i == _UC_FIELDS)
					printf("Got unknown kwd `%s' val=`%s'\n", p, q ? q : "");
				else
					printf("Got kwd[%s]=%s\n", p, q);
#endif
				switch (i) {
				case _UC_DEFAULTPWD:
					config.default_password = boolean_val(q, 1);
					break;
				case _UC_REUSEUID:
					config.reuse_uids = boolean_val(q, 0);
					break;
				case _UC_REUSEGID:
					config.reuse_gids = boolean_val(q, 0);
					break;
				case _UC_NISPASSWD:
					config.nispasswd = (q == NULL || !boolean_val(q, 1))
						? NULL : newstr(q);
					break;
				case _UC_DOTDIR:
					config.dotdir = (q == NULL || !boolean_val(q, 1))
						? NULL : newstr(q);
					break;
				case _UC_NEWMAIL:
					config.newmail = (q == NULL || !boolean_val(q, 1))
						? NULL : newstr(q);
					break;
				case _UC_LOGFILE:
					config.logfile = (q == NULL || !boolean_val(q, 1))
						? NULL : newstr(q);
					break;
				case _UC_HOMEROOT:
					config.home = (q == NULL || !boolean_val(q, 1))
						? "/home" : newstr(q);
					break;
				case _UC_SHELLPATH:
					config.shelldir = (q == NULL || !boolean_val(q, 1))
						? "/bin" : newstr(q);
					break;
				case _UC_SHELLS:
					for (i = 0; i < _UC_MAXSHELLS && q != NULL; i++, q = strtok(NULL, toks))
						system_shells[i] = newstr(q);
					if (i > 0)
						while (i < _UC_MAXSHELLS)
							system_shells[i++] = NULL;
					break;
				case _UC_DEFAULTSHELL:
					config.shell_default = (q == NULL || !boolean_val(q, 1))
						? (char *) bourne_shell : newstr(q);
					break;
				case _UC_DEFAULTGROUP:
					q = unquote(q);
					config.default_group = (q == NULL || !boolean_val(q, 1) || GETGRNAM(q) == NULL)
						? NULL : newstr(q);
					break;
				case _UC_EXTRAGROUPS:
					for (i = 0; q != NULL; q = strtok(NULL, toks)) {
						if (extendarray(&config.groups, &config.numgroups, i + 2) != -1)
							config.groups[i++] = newstr(q);
					}
					if (i > 0)
						while (i < config.numgroups)
							config.groups[i++] = NULL;
					break;
				case _UC_DEFAULTCLASS:
					config.default_class = (q == NULL || !boolean_val(q, 1))
						? NULL : newstr(q);
					break;
				case _UC_MINUID:
					if ((q = unquote(q)) != NULL && isdigit(*q))
						config.min_uid = (uid_t) atol(q);
					break;
				case _UC_MAXUID:
					if ((q = unquote(q)) != NULL && isdigit(*q))
						config.max_uid = (uid_t) atol(q);
					break;
				case _UC_MINGID:
					if ((q = unquote(q)) != NULL && isdigit(*q))
						config.min_gid = (gid_t) atol(q);
					break;
				case _UC_MAXGID:
					if ((q = unquote(q)) != NULL && isdigit(*q))
						config.max_gid = (gid_t) atol(q);
					break;
				case _UC_EXPIRE:
					if ((q = unquote(q)) != NULL && isdigit(*q))
						config.expire_days = atoi(q);
					break;
				case _UC_PASSWORD:
					if ((q = unquote(q)) != NULL && isdigit(*q))
						config.password_days = atoi(q);
					break;
				case _UC_FIELDS:
				case _UC_NONE:
					break;
				}
			}
		}
		free(buf);
		fclose(fp);
	}
	return &config;
}
示例#9
0
int
pw_group(int mode, char *name, long id, struct cargs * args)
{
	int		rc;
	struct carg    *arg;
	struct group   *grp = NULL;
	int	        grmembers = 0;
	char           **members = NULL;
	struct userconf	*cnf = conf.userconf;

	static struct group fakegroup =
	{
		"nogroup",
		"*",
		-1,
		NULL
	};

	if (mode == M_LOCK || mode == M_UNLOCK)
		errx(EX_USAGE, "'lock' command is not available for groups");

	/*
	 * With M_NEXT, we only need to return the
	 * next gid to stdout
	 */
	if (mode == M_NEXT) {
		gid_t next = gr_gidpolicy(cnf, id);
		if (getarg(args, 'q'))
			return next;
		printf("%u\n", next);
		return EXIT_SUCCESS;
	}

	if (mode == M_PRINT && getarg(args, 'a')) {
		SETGRENT();
		while ((grp = GETGRENT()) != NULL)
			print_group(grp);
		ENDGRENT();
		return EXIT_SUCCESS;
	}
	if (id < 0 && name == NULL)
		errx(EX_DATAERR, "group name or id required");

	grp = (name != NULL) ? GETGRNAM(name) : GETGRGID(id);

	if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) {
		if (name == NULL && grp == NULL)	/* Try harder */
			grp = GETGRGID(id);

		if (grp == NULL) {
			if (mode == M_PRINT && getarg(args, 'F')) {
				char	*fmems[1];
				fmems[0] = NULL;
				fakegroup.gr_name = name ? name : "nogroup";
				fakegroup.gr_gid = (gid_t) id;
				fakegroup.gr_mem = fmems;
				return print_group(&fakegroup);
			}
			if (name == NULL)
				errx(EX_DATAERR, "unknown group `%s'", name);
			else
				errx(EX_DATAERR, "unknown group `%ld'", id);
		}
		if (name == NULL)	/* Needed later */
			name = grp->gr_name;

		/*
		 * Handle deletions now
		 */
		if (mode == M_DELETE) {
			rc = delgrent(grp);
			if (rc == -1)
				err(EX_IOERR, "group '%s' not available (NIS?)",
				    name);
			else if (rc != 0) {
				err(EX_IOERR, "group update");
			}
			pw_log(cnf, mode, W_GROUP, "%s(%ld) removed", name, id);
			return EXIT_SUCCESS;
		} else if (mode == M_PRINT)
			return print_group(grp);

		if (id > 0)
			grp->gr_gid = (gid_t) id;

		if (conf.newname != NULL)
			grp->gr_name = pw_checkname(conf.newname, 0);
	} else {
		if (name == NULL)	/* Required */
			errx(EX_DATAERR, "group name required");
		else if (grp != NULL)	/* Exists */
			errx(EX_DATAERR, "group name `%s' already exists", name);

		extendarray(&members, &grmembers, 200);
		members[0] = NULL;
		grp = &fakegroup;
		grp->gr_name = pw_checkname(name, 0);
		grp->gr_passwd = "*";
		grp->gr_gid = gr_gidpolicy(cnf, id);
		grp->gr_mem = members;
	}

	/*
	 * This allows us to set a group password Group passwords is an
	 * antique idea, rarely used and insecure (no secure database) Should
	 * be discouraged, but it is apparently still supported by some
	 * software.
	 */

	if (conf.fd != -1)
		set_passwd(grp, mode == M_UPDATE);

	if (((arg = getarg(args, 'M')) != NULL ||
	    (arg = getarg(args, 'd')) != NULL ||
	    (arg = getarg(args, 'm')) != NULL) && arg->val) {
		int	i = 0;
		char   *p;
		struct passwd	*pwd;

		/* Make sure this is not stay NULL with -M "" */
		extendarray(&members, &grmembers, 200);
		if (arg->ch == 'd')
			delete_members(&members, &grmembers, &i, arg, grp);
		else if (arg->ch == 'm') {
			int	k = 0;

			if (grp->gr_mem != NULL) {
				while (grp->gr_mem[k] != NULL) {
					if (extendarray(&members, &grmembers, i + 2) != -1)
						members[i++] = grp->gr_mem[k];
					k++;
				}
			}
		}

		if (arg->ch != 'd')
			for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
				int	j;

				/*
				 * Check for duplicates
				 */
				pwd = lookup_pwent(p);
				for (j = 0; j < i && strcmp(members[j], pwd->pw_name) != 0; j++)
					;
				if (j == i && extendarray(&members, &grmembers, i + 2) != -1)
					members[i++] = newstr(pwd->pw_name);
			}
		while (i < grmembers)
			members[i++] = NULL;
		grp->gr_mem = members;
	}

	if (conf.dryrun)
		return print_group(grp);

	if (mode == M_ADD && (rc = addgrent(grp)) != 0) {
		if (rc == -1)
			errx(EX_IOERR, "group '%s' already exists",
			    grp->gr_name);
		else
			err(EX_IOERR, "group update");
	} else if (mode == M_UPDATE && (rc = chggrent(name, grp)) != 0) {
		if (rc == -1)
			errx(EX_IOERR, "group '%s' not available (NIS?)",
			    grp->gr_name);
		else
			err(EX_IOERR, "group update");
	}

	if (conf.newname != NULL)
		name = conf.newname;
	/* grp may have been invalidated */
	if ((grp = GETGRNAM(name)) == NULL)
		errx(EX_SOFTWARE, "group disappeared during update");

	pw_log(cnf, mode, W_GROUP, "%s(%u)", grp->gr_name, grp->gr_gid);

	free(members);

	return EXIT_SUCCESS;
}