Пример #1
0
/*
 * setup() - performs all ONE TIME setup for this test.
 */
void setup()
{
	char *cur_dir = NULL;

	/* make sure the process ID is root */
	if (geteuid() != 0) {
		tst_brkm(TBROK, tst_exit, "Test must be run as root.");
	}

	/* capture signals */
	tst_sig(FORK, DEF_HANDLER, cleanup);

	/* Pause if that option was specified */
	TEST_PAUSE;

	/* make a temporary directory and cd to it */
	tst_tmpdir();

	/* get the currect directory name */
	if ((cur_dir = getcwd(cur_dir, 0)) == NULL) {
		tst_brkm(TBROK|TERRNO, cleanup, "Couldn't get current directory name");
	}

	sprintf(good_dir, "%s/%d", cur_dir, getpid());

	ltpuser1 = my_getpwnam(user1name);
	ltpuser2 = my_getpwnam(user2name);
}
Пример #2
0
/*
 * dochild1()
 */
void dochild2()
{
	int retval = 0;
	struct passwd *nobody = my_getpwnam(user1name);

	/* set to nobody */
	if (seteuid(nobody->pw_uid) == -1) {
		retval = 1;
		tst_brkm(TBROK, cleanup, "setreuid failed to "
			 "set effective uid to %d", nobody->pw_uid);
	 /*NOTREACHED*/}

	/* rmdir tstdir4 */
	TEST(rmdir(tstdir4));

	if (TEST_ERRNO) {
		TEST_ERROR_LOG(TEST_ERRNO);
	}

	if (TEST_RETURN != -1) {
		retval = 1;
		tst_resm(TFAIL, "call succeeded unexpectedly");
	} else if (TEST_ERRNO != EACCES) {
		retval = 1;
		tst_resm(TFAIL, "Expected EACCES got %d", TEST_ERRNO);
	} else {
		tst_resm(TPASS, "rmdir() produced EACCES");
	}

	if (seteuid(0) == -1) {
		retval = 1;
		tst_brkm(TBROK, cleanup, "seteuid(0) failed");
	}
	exit(retval);
}
Пример #3
0
int main (int argc, char *argv[]) {
  if (argc != 2) {
    usageErr("%s login-name\n", argv[0]);
  }

  errno = 0;
  struct passwd *pw = my_getpwnam(argv[1]);
  if (pw == NULL) {
    if (errno != 0) {
      errExit("my_getpwnam\n");
    } else {
      cmdLineErr("user %s doesn't exist\n", argv[1]);
    }
  }

  printf("Login name:         %s\n", pw->pw_name);
  printf("Encrypted password: %s\n", pw->pw_passwd);
  printf("User ID:            %ld\n", (long) pw->pw_uid);
  printf("Group ID:           %ld\n", (long) pw->pw_gid);
  printf("Comment:            %s\n", pw->pw_gecos);
  printf("Working directory:  %s\n", pw->pw_dir);
  printf("Login shell:        %s\n", pw->pw_shell);

  exit(EXIT_SUCCESS);
}
Пример #4
0
void find_user(const char *name) {
  struct passwd *result;

  if ( (result = my_getpwnam(name)) != NULL)
    printf("user %s found with id %d\n", result->pw_name, result->pw_uid);
  else
    printf("user %s not found\n", name);
}
Пример #5
0
int main(int argc, char **argv)
{
	int lc;
	char *msg;

	pid_t pid;
	int retval, status;

	struct passwd *nobody;

	if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) {
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
	}

	setup();

	for (lc = 0; TEST_LOOPING(lc); lc++) {
		/* reset Tst_count in case we are looping */
		Tst_count = 0;

		nobody = my_getpwnam(user1name);

		if ((pid = FORK_OR_VFORK()) < 0) {
			tst_brkm(TFAIL, cleanup, "fork failed");
		} else if (pid > 0) {	/* parent */
			waitpid(pid, &status, 0);
			_exit(0);	/*
					 * Exit here and let the child clean up.
					 * This allows the errno information set
					 * by the TEST_ERROR_LOG macro and the
					 * PASS/FAIL status to be preserved for
					 * use during cleanup.
					 */

		} else {	/* child */
			retval = setreuid(nobody->pw_uid, nobody->pw_uid);
			if (retval < 0) {
				perror("setreuid");
				tst_brkm(TFAIL, cleanup, "setreuid failed");
			}
			TEST(vhangup());
			if (TEST_RETURN != -1) {
				tst_brkm(TFAIL, cleanup, "vhangup() failed to "
					 "fail");
			} else if (TEST_ERRNO == EPERM) {
				TEST_ERROR_LOG(TEST_ERRNO);
				tst_resm(TPASS, "Got EPERM as expected.");
			} else {
				TEST_ERROR_LOG(TEST_ERRNO);
				tst_resm(TFAIL, "expected EPERM got %d",
					 TEST_ERRNO);
			}
		}
	}
	cleanup();
	tst_exit();

}
int
start_stop_daemon_main(int argc, char **argv)
{
	unsigned long opt;
	char *signame = NULL;
	char *startas = NULL;

	bb_applet_long_options = ssd_long_options;

	bb_opt_complementaly = "K~S:S~K";
	opt = bb_getopt_ulflags(argc, argv, "KSba:n:s:u:x:", 
			&startas, &cmdname, &signame, &userspec, &execname);

	/* Check one and only one context option was given */
	if ((opt & 0x80000000UL) || (opt & (SSD_CTX_STOP | SSD_CTX_START)) == 0) {
		bb_show_usage();
	}

	if (signame) {
		signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
	}

	if (!execname && !userspec)
		bb_error_msg_and_die ("need at least one of -x or -u");

	if (!startas)
		startas = execname;

	if ((opt & SSD_CTX_START) && !startas)
		bb_error_msg_and_die ("-S needs -x or -a");

	argc -= optind;
	argv += optind;

	if (userspec && sscanf(userspec, "%d", &user_id) != 1)
		user_id = my_getpwnam(userspec);

	do_procfs();

	if (opt & SSD_CTX_STOP) {
		do_stop();
		return EXIT_SUCCESS;
	}

	if (found) {
		printf("%s already running.\n%d\n", execname ,found->pid);
		return EXIT_SUCCESS;
	}
	*--argv = startas;
	if (opt & SSD_OPT_BACKGROUND) {
		if (daemon(0, 0) == -1)
			bb_perror_msg_and_die ("unable to fork");
	}
	setsid();
	execv(startas, argv);
	bb_perror_msg_and_die ("unable to start %s", startas);
}
Пример #7
0
int
start_stop_daemon_main(int argc, char **argv)
{
	int flags;
	char *signame = NULL;
	bb_applet_long_options = ssd_long_options;

	flags = bb_getopt_ulflags(argc, argv, "KSba:n:s:u:x:", 
			&startas, &cmdname, &signame, &userspec, &execname);

	/* Be sneaky and avoid branching */
	stop = (flags & 1);
	start = (flags & 2);
	fork_before_exec = (flags & 4);

	if (signame) {
		signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
	}

	if (start == stop)
		bb_error_msg_and_die ("need exactly one of -S or -K");

	if (!execname && !userspec)
		bb_error_msg_and_die ("need at least one of -x or -u");

	if (!startas)
		startas = execname;

	if (start && !startas)
		bb_error_msg_and_die ("-S needs -x or -a");

	argc -= optind;
	argv += optind;

	if (userspec && sscanf(userspec, "%d", &user_id) != 1)
		user_id = my_getpwnam(userspec);

	do_procfs();

	if (stop) {
		do_stop();
		return EXIT_SUCCESS;
	}

	if (found) {
		printf("%s already running.\n%d\n", execname ,found->pid);
		return EXIT_SUCCESS;
	}
	*--argv = startas;
	if (fork_before_exec) {
		if (daemon(0, 0) == -1)
			bb_perror_msg_and_die ("unable to fork");
	}
	setsid();
	execv(startas, argv);
	bb_perror_msg_and_die ("unable to start %s", startas);
}
Пример #8
0
void setup()
{
	char *cur_dir = NULL;

	tst_require_root(NULL);

	tst_sig(FORK, DEF_HANDLER, cleanup);

	TEST_PAUSE;

	tst_tmpdir();

	if ((cur_dir = getcwd(cur_dir, 0)) == NULL)
		tst_brkm(TBROK|TERRNO, cleanup, "getcwd failed");

	sprintf(good_dir, "%s/%d", cur_dir, getpid());

	ltpuser1 = my_getpwnam(user1name);
	ltpuser2 = my_getpwnam(user2name);
}
Пример #9
0
/*
 * setup() - performs all ONE TIME setup for this test.
 */
void setup()
{
    /* must run as root */
    if (geteuid() != 0) {
        tst_brkm(TBROK, NULL, "Must run test as root");
    }

    tst_sig(FORK, DEF_HANDLER, cleanup);

    TEST_PAUSE;

    /* Create a temporary directory and make it current. */
    tst_tmpdir();

    umask(0);

    sprintf(fdir, "tdir_%d", getpid());
    sprintf(mdir, "rndir_%d", getpid());
    sprintf(fname, "%s/tfile_%d", fdir, getpid());
    sprintf(mname, "%s/rnfile_%d", mdir, getpid());

    nobody = my_getpwnam(user1name);
    bin = my_getpwnam(user2name);
}
Пример #10
0
/*
 * setup() - performs all ONE TIME setup for this test.
 */
void setup()
{
	/* must run as root */
	if (geteuid() != 0) {
		tst_brkm(TBROK, NULL, "Must run this as root");
	}

	tst_sig(FORK, DEF_HANDLER, cleanup);

	TEST_PAUSE;

	/* Create a temporary directory and make it current. */
	tst_tmpdir();

	umask(0);

	sprintf(fdir, "./tdir_%d", getpid());
	sprintf(fname, "%s/tfile_%d", fdir, getpid());
	sprintf(mname, "%s/rnfile_%d", fdir, getpid());

	/* create a directory */
	if (mkdir(fdir, PERMS) == -1) {
		tst_brkm(TBROK, cleanup, "Could not create directory %s", fdir);
	}

	if (stat(fdir, &buf1) == -1) {
		tst_brkm(TBROK, cleanup, "failed to stat directory %s", fdir);

	}

	/* set the sticky bit */
	if (chmod(fdir, buf1.st_mode | S_ISVTX) != 0) {
		tst_brkm(TBROK, cleanup, "failed to set the S_ISVTX bit");

	}

	/* create a file under fdir */
	do_file_setup(fname);

	/* get nobody password file info */
	nobody = my_getpwnam(user1name);
}
Пример #11
0
/*
 * setup() - performs all ONE TIME setup for this test.
 */
void setup()
{
	/* test must be run as root */
	if (geteuid() != 0) {
		tst_brkm(TBROK, NULL, "Must run test as root");
	}

	tst_sig(FORK, DEF_HANDLER, cleanup);

	TEST_PAUSE;

	/* make a temporary directory and cd to it */
	tst_tmpdir();

	sprintf(fname, "file.%d", getpid());

	nobody = my_getpwnam(user1name);

	if ((fd = open(fname, O_RDWR | O_CREAT, 0700)) == -1) {
		tst_brkm(TBROK, cleanup, "open() failed, errno: %d", errno);
	 }
}
Пример #12
0
void setup(char *argv0)
{
	char *cmd, *pwd = NULL;
	char test_path[MAXPATHLEN];

	tst_require_root(NULL);

	if (test_app[0] == '/')
		strncpy(test_path, test_app, sizeof(test_path));
	else {
		if ((pwd = get_current_dir_name()) == NULL)
			tst_brkm(TBROK|TERRNO, NULL, "getcwd failed");

		snprintf(test_path, sizeof(test_path), "%s/%s",
		    pwd, basename(test_app));

		free(pwd);
	}

	cmd = malloc(strlen(test_path) + strlen("cp -p \"") + strlen("\" .") +
	    1);
	if (cmd == NULL)
		tst_brkm(TBROK|TERRNO, NULL, "Cannot alloc command string");

	tst_sig(FORK, DEF_HANDLER, cleanup);

	tst_tmpdir();

	sprintf(cmd, "cp -p \"%s\" .", test_path);
	if (system(cmd) != 0)
		tst_brkm(TBROK, NULL, "command failed: %s", cmd);
	free(cmd);

	umask(0);

	ltpuser1 = my_getpwnam(user1name);
}
Пример #13
0
extern int id_main(int argc, char **argv)
{
	char user[9], group[9];
	long pwnam, grnam;
	int uid, gid;
	int flags;
#ifdef CONFIG_SELINUX
	int is_flask_enabled_flag = is_flask_enabled();
#endif

	flags = bb_getopt_ulflags(argc, argv, "ugrn");

	if (((flags & (JUST_USER | JUST_GROUP)) == (JUST_USER | JUST_GROUP))
		|| (argc > optind + 1)
	) {
		bb_show_usage();
	}

	if (argv[optind] == NULL) {
		if (flags & PRINT_REAL) {
			uid = getuid();
			gid = getgid();
		} else {
			uid = geteuid();
			gid = getegid();
		}
		my_getpwuid(user, uid);
	} else {
		safe_strncpy(user, argv[optind], sizeof(user));
	    gid = my_getpwnamegid(user);
	}
	my_getgrgid(group, gid);

	pwnam=my_getpwnam(user);
	grnam=my_getgrnam(group);

	if (flags & (JUST_GROUP | JUST_USER)) {
		char *s = group;
		if (flags & JUST_USER) {
			s = user;
			grnam = pwnam;
		}
		if (flags & NAME_NOT_NUMBER) {
			puts(s);
		} else {
			printf("%ld\n", grnam);
		}
	} else {
#ifdef CONFIG_SELINUX
		printf("uid=%ld(%s) gid=%ld(%s)", pwnam, user, grnam, group);
		if(is_flask_enabled_flag)
		{
			security_id_t mysid = getsecsid();
			char context[80];
			int len = sizeof(context);
			context[0] = '\0';
			if(security_sid_to_context(mysid, context, &len))
				strcpy(context, "unknown");
			printf(" context=%s\n", context);
		}
		else
			printf("\n");
#else
		printf("uid=%ld(%s) gid=%ld(%s)\n", pwnam, user, grnam, group);
#endif

	}

	bb_fflush_stdout_and_exit(0);
}
Пример #14
0
int main(int ac, char **av)
{
	int lc;
	const char *msg;
	int rval;
	pid_t pid, pid1;
	int status;
	struct passwd *ltpuser1, *ltpuser2;

	/*
	 * parse standard options
	 */
	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
	}

	/*
	 * perform global setup for test
	 */
	setup();

	/* set the expected errnos... */
	TEST_EXP_ENOS(exp_enos);

	/*
	 * check looping state if -i option given
	 */
	for (lc = 0; TEST_LOOPING(lc); lc++) {

		tst_count = 0;

		/* Initialize the test directories name */
		sprintf(tstdir1, "tstdir1.%d", getpid());
		ltpuser1 = my_getpwnam(user1name);

		if ((pid = FORK_OR_VFORK()) < 0) {
			tst_brkm(TBROK, cleanup, "fork #1 failed");
		}

		if (pid == 0) {	/* first child */
			/* set to ltpuser1 */
			rval = setreuid(ltpuser1->pw_uid, ltpuser1->pw_uid);
			if (rval < 0) {
				tst_resm(TFAIL, "setreuid failed to "
					 "to set the real uid to %d and "
					 "effective uid to %d",
					 ltpuser1->pw_uid, ltpuser1->pw_uid);
				perror("setreuid");
				exit(1);
			}
			/* create the parent directory with 0700 permits */
			if (mkdir(tstdir1, PERMS) == -1) {
				tst_resm(TFAIL, "mkdir(%s, %#o) Failed",
					 tstdir1, PERMS);
				exit(1);
			}
			/* create tstdir1 succeeded */
			exit(0);
		}
		wait(&status);
		if (WEXITSTATUS(status) != 0) {
			tst_brkm(TFAIL, cleanup,
				 "Test to check mkdir EACCES failed"
				 "in create parent directory");
		}

		sprintf(tstdir2, "%s/tst", tstdir1);
		ltpuser2 = my_getpwnam(user2name);

		if ((pid1 = FORK_OR_VFORK()) < 0) {
			tst_brkm(TBROK, cleanup, "fork #2 failed");
		}

		if (pid1 == 0) {	/* second child */
			/* set to ltpuser2 */
			rval = setreuid(ltpuser2->pw_uid, ltpuser2->pw_uid);
			if (rval < 0) {
				tst_resm(TFAIL, "setreuid failed to "
					 "to set the real uid to %d and "
					 "effective uid to %d",
					 ltpuser2->pw_uid, ltpuser2->pw_uid);
				perror("setreuid");
				exit(1);
			}
			if (mkdir(tstdir2, PERMS) != -1) {
				tst_resm(TFAIL, "mkdir(%s, %#o) unexpected "
					 "succeeded", tstdir2, PERMS);
				exit(1);
			}
			if (errno != EACCES) {
				tst_resm(TFAIL, "Expected EACCES got %d",
					 errno);
				exit(1);
			}
			/* PASS */
			exit(0);
		}
		waitpid(pid1, &status, 0);
		if (WEXITSTATUS(status) == 0) {
			tst_resm(TPASS, "Test to attempt to creat a directory "
				 "in a directory having no permissions "
				 "SUCCEEDED in setting errno to EACCES");
		} else {
			tst_resm(TFAIL, "Test to attempt to creat a directory "
				 "in a directory having no permissions FAILED");
			cleanup();
		}
	}

	/*
	 * cleanup and exit
	 */
	cleanup();
	tst_exit();

}
Пример #15
0
extern int id_main(int argc, char **argv)
{
	struct passwd *p;
	uid_t uid;
	gid_t gid;
	unsigned long flags;
	short status;
#ifdef CONFIG_SELINUX
	int is_flask_enabled_flag = is_flask_enabled();
#endif

	bb_opt_complementaly = "u~g:g~u";
	flags = bb_getopt_ulflags(argc, argv, "rnug");

	if ((flags & BB_GETOPT_ERROR)
	/* Don't allow -n -r -nr */
	|| (flags <= 3 && flags > 0) 
	/* Don't allow more than one username */
	|| (argc > optind + 1))
		bb_show_usage();
	
	/* This values could be overwritten later */
	uid = geteuid();
	gid = getegid();
	if (flags & PRINT_REAL) {
		uid = getuid();
		gid = getgid();
	}
	
	if(argv[optind]) {
		p=getpwnam(argv[optind]);
		/* my_getpwnam is needed because it exits on failure */
		uid = my_getpwnam(argv[optind]);
		gid = p->pw_gid;
		/* in this case PRINT_REAL is the same */ 
	}

	if(flags & (JUST_GROUP | JUST_USER)) {
		/* JUST_GROUP and JUST_USER are mutually exclusive */
		if(flags & NAME_NOT_NUMBER) {
			/* my_getpwuid and my_getgrgid exit on failure so puts cannot segfault */
			puts((flags & JUST_USER) ? my_getpwuid(NULL, uid, -1 ) : my_getgrgid(NULL, gid, -1 ));
		} else {
			bb_printf("%u\n",(flags & JUST_USER) ? uid : gid);
		}
		/* exit */ 
		bb_fflush_stdout_and_exit(EXIT_SUCCESS);
	}

	/* Print full info like GNU id */
	/* my_getpwuid doesn't exit on failure here */
	status=printf_full(uid, my_getpwuid(NULL, uid, 0), 'u');
	putchar(' ');
	/* my_getgrgid doesn't exit on failure here */
	status|=printf_full(gid, my_getgrgid(NULL, gid, 0), 'g');
#ifdef CONFIG_SELINUX
	if(is_flask_enabled_flag) {
		security_id_t mysid = getsecsid();
		char context[80];
		int len = sizeof(context);
		context[0] = '\0';
		if(security_sid_to_context(mysid, context, &len))
			strcpy(context, "unknown");
		bb_printf(" context=%s", context);
	}
#endif
	putchar('\n');
	bb_fflush_stdout_and_exit(status);
}
Пример #16
0
int main(int ac, char **av)
{
    int lc;			/* loop counter */
    int retval = 0;
    char *msg;		/* message returned from parse_opts */

    pid_t pid, pid1;
    int i, status, fd;

    /* parse standard options */
    if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
        tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);

    setup();

    TEST_EXP_ENOS(exp_enos);

    for (lc = 0; TEST_LOOPING(lc); lc++) {

        /* reset Tst_count in case we are looping */
        Tst_count = 0;

        if ((pid = FORK_OR_VFORK()) == -1) {
            tst_brkm(TBROK|TERRNO, cleanup, "fork() #1 failed");
        }

        if (pid == 0) {	/* first child */
            if (mkdir(good_dir, DMODE) != 0) {
                perror("mkdir() failed");
                exit(1);
            }
            if ((fd = open(fname1, O_RDWR | O_CREAT, 0444)) == -1) {
                perror("open failed");
                exit(1);
            }
            exit(0);
        }
        wait(&status);

        /* make sure the child returned a good exit status */
        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
            tst_brkm(TBROK, cleanup, "child #1 failed");
        }

        if ((pid1 = FORK_OR_VFORK()) == -1) {
            tst_brkm(TBROK|TERRNO, cleanup, "fork() #2 failed");
        }

        if (pid1 == 0) {	/* second child */

            ltpuser1 = my_getpwnam(user1name);

            if (ltpuser1 == NULL) {
                perror("getpwnam");
                exit(1);
            }
            if (seteuid(ltpuser1->pw_uid) == -1) {
                perror("seteuid");
                exit(1);
            }

            /* loop through the test cases */
            for (i = 0; i < TST_TOTAL; i++) {

                TEST(creat(TC[i].fname, FMODE));

                if (TEST_RETURN != -1) {
                    retval = 1;
                    tst_resm(TFAIL, "call succeeded "
                             "unexpectedly");
                    continue;
                }

                TEST_ERROR_LOG(TEST_ERRNO);

                if (TEST_ERRNO != EACCES) {
                    retval = 1;
                    tst_resm(TFAIL|TTERRNO, "Expected EACCES");
                } else {
                    tst_resm(TPASS, "call failed with "
                             "EACCES as expected");
                }
            }

            seteuid(0);

            /* clean up things in case we are looping */
            unlink(fname);
            unlink(fname1);
            rmdir(good_dir);
            exit(retval);

        } else {	/* parent */
            /* wait for the child to finish */
            wait(&status);
            /* make sure the child returned a good exit status */
            if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
                tst_resm(TFAIL, "see failures reported above");
            }
        }
    }
    cleanup();

    tst_exit();
}
Пример #17
0
extern int id_main(int argc, char **argv)
{
	int no_user = 0, no_group = 0, print_real = 0;
	int name_not_number = 0;
	char user[9], group[9];
	long gid;
	long pwnam, grnam;
	int opt;
	
	gid = 0;

	while ((opt = getopt(argc, argv, "ugrn")) > 0) {
		switch (opt) {
			case 'u':
				no_group++;
				break;
			case 'g':
				no_user++;
				break;
			case 'r':
				print_real++;
				break;
			case 'n':
				name_not_number++;
				break;
			default:
				show_usage();
		}
	}

	if (no_user && no_group) show_usage();

	if (argv[optind] == NULL) {
		if (print_real) {
			my_getpwuid(user, getuid());
			my_getgrgid(group, getgid());
		} else {
			my_getpwuid(user, geteuid());
			my_getgrgid(group, getegid());
		}
	} else {
		strncpy(user, argv[optind], 8);
		user[8] = '\0';
	    gid = my_getpwnamegid(user);
		my_getgrgid(group, gid);
	}

	pwnam=my_getpwnam(user);
	grnam=my_getgrnam(group);

	if (no_group) {
		if(name_not_number && user)
			puts(user);
		else
			printf("%ld\n", pwnam);
	} else if (no_user) {
		if(name_not_number && group)
			puts(group);
		else
			printf("%ld\n", grnam);
	} else {
		printf("uid=%ld(%s) gid=%ld(%s)\n", pwnam, user, grnam, group);
	}
	return(0);
}
Пример #18
0
int main(int ac, char **av)
{
	int lc;			/* loop counter */
	char *msg;		/* message returned from parse_opts */

	struct passwd *nobody;
	pid_t pid;
	struct sched_param param;
	int status;

	/* parse standard options */
	if ((msg = parse_opts(ac, av, (option_t *) NULL, NULL)) != (char *)NULL) {
		tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
	 /*NOTREACHED*/}

	setup();

	TEST_EXP_ENOS(exp_enos);

	/* check looping state if -i option is given */
	for (lc = 0; TEST_LOOPING(lc); lc++) {

		/* reset Tst_count in case we are looping */
		Tst_count = 0;

		if ((pid = FORK_OR_VFORK()) == -1) {
			tst_brkm(TBROK, cleanup, "fork failed");
		}

		if (pid == 0) {	/* child */
			param.sched_priority = 1;

			nobody = my_getpwnam(user1name);

			if (seteuid(nobody->pw_uid) == -1) {
				tst_brkm(TBROK, cleanup, "seteuid() failed");
			}

			TEST(sched_setscheduler(pid, SCHED_FIFO, &param));

			if (TEST_ERRNO) {
				TEST_ERROR_LOG(TEST_ERRNO);
			}

			if (TEST_RETURN != -1) {
				tst_resm(TFAIL, "sched_setscheduler(2) passed "
					 "with non root priveledges");
			} else if (TEST_ERRNO != EPERM) {
				tst_resm(TFAIL, "Expected EPERM, got %d",
					 TEST_ERRNO);
			} else {
				tst_resm(TPASS, "got EPERM");
			}
		} else {	/* parent */
			/* let the child carry on */
			wait(&status);
			if (WIFEXITED(status) != 0) {	/* Exit with errors */
				exit(WEXITSTATUS(status));
			} else {
				exit(0);
			}
		}

		if (seteuid(0) == -1) {
			tst_brkm(TBROK, cleanup, "seteuid(0) failed");
		}
	}
	cleanup();
	 /*NOTREACHED*/ return 0;

}
Пример #19
0
int main(int ac, char **av)
{
	int lc;			/* loop counter */
	char *msg;		/* message returned from parse_opts */
	struct stat buf, buf1;
	pid_t pid, pid1;
	struct passwd *ltpuser1;
	struct passwd *ltpuser2;
	int rval, status;

	/*
	 * parse standard options
	 */
	if ((msg = parse_opts(ac, av, (option_t *) NULL, NULL)) != (char *)NULL) {
		tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
	}

	/*
	 * perform global setup for test
	 */
	setup();

	/*
	 * check looping state if -i option given
	 */
	for (lc = 0; TEST_LOOPING(lc); lc++) {

		/* reset Tst_count in case we are looping. */
		Tst_count = 0;

		/* check the inherited group ID */

		/*
		 * first, fork the first child, set to ltpuser1's uid and gid,
		 * create a directory, with S_ISGID bit set
		 */

		ltpuser1 = my_getpwnam(user1name);
		sprintf(tstdir1, "tstdir1.%d", getpid());

		if ((pid = FORK_OR_VFORK()) < 0) {
			tst_brkm(TFAIL, cleanup, "fork() failed");
			/* NOTREACHED */
		}

		if (pid == 0) {	/* first child */
			rval = setregid(ltpuser1->pw_gid, ltpuser1->pw_gid);
			if (rval < 0) {
				perror("setregid");
				tst_resm(TFAIL, "setregid failed to "
					 "to set the real gid to %d and "
					 "effective gid to %d",
					 ltpuser1->pw_gid, ltpuser1->pw_gid);
				exit(1);
				/* NOTREACHED */
			}
			/* being ltupuser1 */
			rval = setreuid(ltpuser1->pw_uid, ltpuser1->pw_uid);
			if (rval < 0) {
				perror("setreuid");
				tst_resm(TFAIL, "setreuid failed to "
					 "to set the real uid to %d and "
					 "effective uid to %d",
					 ltpuser1->pw_uid, ltpuser1->pw_uid);
				exit(1);
				/* NOTREACHED */
			}

			/*
			 * create a direcoty with S_ISGID bit set
			 * and the group ID is ltpuser1
			 */
			if (mkdir(tstdir1, PERMS) != 0) {
				perror("mkdir");
				tst_resm(TFAIL, "mkdir() failed to create"
					 " a directory with Set "
					 " group ID turn on ");
				exit(1);
				/* NOTREACHED */
			}
			if (stat(tstdir1, &buf1) == -1) {
				perror("stat");
				tst_resm(TFAIL,
					 "failed to stat the new directory"
					 "in mkdir()");
				exit(1);
				/* NOTREACHED */
			}
			if (chmod(tstdir1, buf1.st_mode | S_ISGID) != 0) {
				perror("chmod");
				tst_resm(TFAIL, "failed to set S_ISGID bit");
				exit(1);
				/* NOTREACHED */
			}

			/* Successfully create the parent directory */
			exit(0);
			/* NOTREACHED */
		}
		wait(&status);
		if (WEXITSTATUS(status) != 0) {
			tst_brkm(TFAIL, cleanup,
				 "Test to attempt to make a directory"
				 " inherits group ID FAILED ");
		}
		/*
		 * fork the second child process, set to ltpuser2's uid and gid
		 * create a sub directory under the directory
		 * just created by child process 1
		 * check the group ID of the sub directory
		 * should inherit from parent directory
		 */

		ltpuser2 = my_getpwnam(user2name);
		sprintf(tstdir2, "%s/tstdir2.%d", tstdir1, getpid());
		if ((pid1 = FORK_OR_VFORK()) < 0) {
			perror("fork failed");
			tst_brkm(TFAIL, cleanup, "fork() failed");
			/* NOTREACHED */
		} else if (pid1 == 0) {	/* second child */

			/* being user ltpuser2 */
			rval = setregid(ltpuser2->pw_gid, ltpuser2->pw_gid);
			if (rval < 0) {
				tst_resm(TFAIL, "setregid failed to "
					 "to set the real gid to %d and "
					 "effective gid to %d",
					 ltpuser2->pw_gid, ltpuser2->pw_gid);
				perror("setregid");
				exit(1);
				/* NOTREACHED */
			}
			rval = setreuid(ltpuser2->pw_uid, ltpuser2->pw_uid);
			if (rval < 0) {
				tst_resm(TFAIL, "setreuid failed to "
					 "to set the real uid to %d and "
					 "effective uid to %d",
					 ltpuser2->pw_uid, ltpuser2->pw_uid);
				perror("setreuid");
				exit(1);
				/* NOTREACHED */

			}

			/*
			 * create a sub direcoty
			 * under the directory just created
			 * by ltpuser1
			 */
			if (mkdir(tstdir2, PERMS) != 0) {
				tst_resm(TFAIL, "mkdir() failed to create"
					 " a directory %s under %s ", tstdir2,
					 tstdir1);
				exit(1);
				/* NOTREACHED */
			}
			/*
			 * check the group ID
			 * should not be the same as the current processs's
			 * since parent directory is set with S_ISGID bit
			 */
			if (stat(tstdir2, &buf) == -1) {
				tst_resm(TFAIL,
					 "failed to stat the new directory"
					 "in mkdir()");
				exit(1);
				/* NOTREACHED */
			}
			if (stat(tstdir1, &buf1) == -1) {
				tst_resm(TFAIL,
					 "failed to stat the new directory"
					 "in mkdir()");
				exit(1);
				/* NOTREACHED */
			}
			if (buf.st_gid != buf1.st_gid) {
				tst_resm(TFAIL, "mkdir() FAILED to inherit "
					 " the group ID %d from parent "
					 " directory %d",
					 buf.st_gid, buf1.st_gid);
				exit(1);
				/* NOTREACHED */
			}

			/* check the S_ISGID  bit */
			if (!(buf.st_mode & S_ISGID)) {
				tst_resm(TFAIL, "mkdir() FAILED to inherit "
					 " the S_ISGID bit from parent "
					 " directory");
				exit(1);
				/* NOTREACHED */
			}
			/* PASS */
			exit(0);
			/* NOTREACHED */
		}

		waitpid(pid1, &status, 0);
		if (WEXITSTATUS(status) == 0) {
			tst_resm(TPASS, "Test to attempt to make a directory"
				 " inherits group ID SUCCEEDED ");
		} else {
			tst_resm(TFAIL, "Test to attempt to make a directory"
				 " inherits group ID FAILED");
			cleanup();
		 /*NOTREACHED*/}

	}			/* End for TEST_LOOPING */

	/*
	 * cleanup and exit
	 */
	cleanup();

	 /*NOTREACHED*/ return 0;
}				/* End main */
int
start_stop_daemon_main(int argc, char **argv)
{
	unsigned long opt;
	char *signame = NULL;
	char *startas = NULL;

	bb_applet_long_options = ssd_long_options;

	bb_opt_complementaly = "K~S:S~K";
	opt = bb_getopt_ulflags(argc, argv, "KSbqma:n:s:u:x:p:",
			&startas, &cmdname, &signame, &userspec, &execname, &pidfile);

	/* Check one and only one context option was given */
	if ((opt & 0x80000000UL) || (opt & (SSD_CTX_STOP | SSD_CTX_START)) == 0) {
		bb_show_usage();
	}

	if (signame) {
		signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
	}

	if (!execname && !pidfile && !userspec && !cmdname)
		bb_error_msg_and_die ("need at least one of -x, -p, -u, or -n");

	if (!startas)
		startas = execname;

	if ((opt & SSD_CTX_START) && !startas)
		bb_error_msg_and_die ("-S needs -x or -a");

	if ((opt & SSD_OPT_MAKEPID) && pidfile == NULL)
		bb_error_msg_and_die ("-m needs -p");

	argc -= optind;
	argv += optind;

	if (userspec && sscanf(userspec, "%d", &user_id) != 1)
		user_id = my_getpwnam(userspec);

	if (opt & SSD_CTX_STOP) {
		do_stop();
		return EXIT_SUCCESS;
	}

	do_procinit();

	if (found) {
		if (!quiet)
			printf("%s already running.\n%d\n", execname ,found->pid);
		return EXIT_SUCCESS;
	}
	*--argv = startas;
	if (opt & SSD_OPT_BACKGROUND) {
		if (daemon(0, 0) == -1)
			bb_perror_msg_and_die ("unable to fork");
		setsid();
	}
	if (opt & SSD_OPT_MAKEPID) {
		/* user wants _us_ to make the pidfile */
		FILE *pidf = fopen(pidfile, "w");
		pid_t pidt = getpid();
		if (pidf == NULL)
			bb_perror_msg_and_die("Unable to write pidfile '%s'", pidfile);
		fprintf(pidf, "%d\n", pidt);
		fclose(pidf);
	}
	execv(startas, argv);
	bb_perror_msg_and_die ("unable to start %s", startas);
}