示例#1
0
文件: pwd.c 项目: Alkzndr/freebsd
static int
pwd_command(const char *cmd, const nvlist_t *limits, nvlist_t *nvlin,
    nvlist_t *nvlout)
{
	int error;

	if (!pwd_allowed_cmd(limits, cmd))
		return (ENOTCAPABLE);

	if (strcmp(cmd, "getpwent") == 0 || strcmp(cmd, "getpwent_r") == 0)
		error = pwd_getpwent(limits, nvlin, nvlout);
	else if (strcmp(cmd, "getpwnam") == 0 || strcmp(cmd, "getpwnam_r") == 0)
		error = pwd_getpwnam(limits, nvlin, nvlout);
	else if (strcmp(cmd, "getpwuid") == 0 || strcmp(cmd, "getpwuid_r") == 0)
		error = pwd_getpwuid(limits, nvlin, nvlout);
	else if (strcmp(cmd, "setpassent") == 0)
		error = pwd_setpassent(limits, nvlin, nvlout);
	else if (strcmp(cmd, "setpwent") == 0)
		error = pwd_setpwent(limits, nvlin, nvlout);
	else if (strcmp(cmd, "endpwent") == 0)
		error = pwd_endpwent(limits, nvlin, nvlout);
	else
		error = EINVAL;

	return (error);
}
示例#2
0
/*
 * Find chown info for a group.
 */
chowninfo_t * chowninfo_find_by_group(char *group) {
	char *tmp;
	hashtable_t *env = get_context();
	chowninfo_t * i = malloc(sizeof(chowninfo_t));
	pwdfile *pass;
	grpfile_t *grp;

	i->uid = i->gid = -1;

	tmp = group_get_property(group, PROPERTY_GROUP_CHOWN_USER);
	if (tmp) {
		if (!strcmp(tmp, "USER"))
			pass = pwd_getpwnam(ht_get(env, PROPERTY_USER));
		else
			pass = pwd_getpwnam(tmp);

		if (pass)
			i->uid = pass->uid;
		else
			printf(" * WARNING: Could not get user.chown info for %s\n", group);
	}

	tmp = group_get_property(group, PROPERTY_GROUP_CHOWN_GROUP);
	if (tmp) {
		if (!strcmp(tmp, "GROUP"))
			grp = pwd_getgpnam(group);
		else
			grp = pwd_getgpnam(tmp);

		if (grp)
			i->gid = grp->gid;
		else
			printf(" * WARNING: Could not get grp.chown info for %s\n", group);
	}

	return i;
}
示例#3
0
/*
 * int
 * pfc_pwd_strtouid(const char *name, uid_t *uidp)
 *	Convert string to user ID.
 *
 *	If the string specified by `name' is a string representation of
 *	integer, and it is not a valid user name, it will be converted to
 *	uid_t.
 *
 * Calling/Exit State:
 *	Upon successful completion, converted user ID is stored to `*uidp',
 *	and zero is returned.
 *	Otherwise error number which indicates the cause of error is returned.
 */
int
pfc_pwd_strtouid(const char *name, uid_t *uidp)
{
    pwd_passwd_t	pctx;
    uint32_t	value;
    int		err;

    PFC_ASSERT(sizeof(uint32_t) == sizeof(uid_t));

    if (PFC_EXPECT_FALSE(name == NULL || *name == '\0' || uidp == NULL)) {
        return EINVAL;
    }

    /* Look for a name in password database file. */
    err = pwd_getpwnam(&pctx, name);
    if (PFC_EXPECT_TRUE(err == 0)) {
        *uidp = pctx.pp_passwd.pw_uid;
        goto out;
    }
    if (PFC_EXPECT_FALSE(err > 0)) {
        goto out;
    }

    /* Try to convert the given string into uid_t. */
    err = pfc_strtou32(name, &value);
    if (PFC_EXPECT_TRUE(err == 0 && (uid_t)value != (uid_t)-1)) {
        *uidp = value;
    }
    else {
        err = ENOENT;
    }

out:
    pwd_buffer_free(&pctx.pp_buffer);

    return err;
}
示例#4
0
int pre(char *section, char *dest, char *src, char *rel, char *group, char *argv[]) {
	hashtable_t *cfg, *env;

	char buf[1024], tbuf[50], ubuf[300], *tmp;
	filelist_t *files, *ftmp;
	creditlist_t *credits, *tcred;
	struct subdir_list *subdirs;
	float bcount;
	long fnum;
	int olduid, disks = 1;
	pwdfile *pass;
	chowninfo_t *chown;

	cfg = get_config();
	env = get_context();

	pass = pwd_getpwnam(ht_get(env, PROPERTY_USER));

	if (!pass) {
		printf(" * Error, cannot get your passwd entry! \n");
		return 0;
	}

	chown = chowninfo_find_by_group(group);

	olduid = getuid();
	if (setuid(0) == -1)
		quit(" * Error changing uid, bug sysop to fix perms on foo-pre !\n");

	// find filenames, and collect user's stats + do chowning of files.
	files = filelist_find_by_dir(0, src, "", chown);

	// get totals.
	flist_size(files, &bcount, &fnum);
	printf(" * Totals of this pre for announce: %dF %.0fB\n", fnum, bcount);

	// get credits.
	credits = creditlist_create_from_filelist(files);

	printf(" * Moving files to destination dir.. \n");
	printf("   -- %10.10s: %s\n", "From", src);
	printf("   -- %10.10s: %s", "To", dest);

	// dont forget to chown maindir
	chowninfo_apply_to_file(src, chown);

	// do the actual moving of dir.
	if (pre_move_catalog(src, dest) == -1)
		quit(" Failed!\n");
	else
		printf(" Done\n");

	// give credits to original uploaders
	printf(" * Updating userfiles ..\n");
	strcpy(ubuf, "");
	for (tcred = credits; tcred; tcred = tcred->next) {
		tmp = userfile_update(tcred);
		if (tmp)
			strcat(ubuf, tmp);
	}

	setuid(olduid);

	if (strlen(ubuf) > 2)
		ubuf[strlen(ubuf) - 2] = 0;

	// add to dupefile.
	tmp = ht_get(env, PROPERTY_USER);
	for (ftmp = files; ftmp; ftmp = ftmp->next)
		if (!gl_dupefile_add(flist_getfilename(ftmp), tmp))
			printf(" * Error adding to dupefile: %s\n", ftmp->file);

	subdirs = subdir_flistcollect(files);
	reverse_dirlog_add(subdirs, dest, rel, pass, chown);

	disks = subdirlist_count(subdirs);
	if (disks == 0)
		disks++;

	// create announce.
	tmp = group_get_property(group, PROPERTY_GROUP_ANNOUNCE);

	if (tmp) {

		strcpy(buf, tmp);
		sprintf(tbuf, "%.1f", (float)bcount/(1024*1024));
		pre_replace(buf, "%S", tbuf);
		sprintf(tbuf, "%ld", fnum);
		pre_replace(buf, "%W", ubuf);
		pre_replace(buf, "%F", tbuf);
		sprintf(tbuf, "%d", disks);
		pre_replace(buf, "%C", tbuf);
		pre_replace(buf, "%PP", ht_get(env, "RESOLVEDDESTINATION"));
		pre_replace(buf, "%P", section_get_property(section, PROPERTY_SECTION_DIR));
		pre_replace(buf, "%U", ht_get(env, PROPERTY_USER));
		pre_replace(buf, "%T", ht_get(env, PROPERTY_TAGLINE));
		pre_replace(buf, "%G", ht_get(env, PROPERTY_GROUP));
		pre_replace(buf, "%g", ht_get(env, PROPERTY_USERGROUP));
		pre_replace(buf, "%D", section_get_property(section, PROPERTY_SECTION_NAME));
		pre_replace(buf, "%R", rel);

		gl_gllog_add(buf);

	} else
		printf(" * WARNING: No announce setup ! Go bug sysop :( \n");

	// add to dupelog.
	if (!gl_dupelog_add(rel))
		printf(" * Error adding to dupelog !  (prolly wrong perms, ask sysop to fix)\n");

	sprintf(buf, "%s/%s", ht_get(env, "RESOLVEDDESTINATION"), rel);

	// do additional module stuff
	pre_do_modules(files, buf, argv, subdirs);

	return 1;
}