Example #1
0
static          gid_t
gr_gidpolicy(struct userconf * cnf, long id)
{
	struct group   *grp;
	gid_t           gid = (gid_t) - 1;

	/*
	 * Check the given gid, if any
	 */
	if (id > 0) {
		gid = (gid_t) id;

		if ((grp = GETGRGID(gid)) != NULL && conf.checkduplicate)
			errx(EX_DATAERR, "gid `%u' has already been allocated", grp->gr_gid);
	} else {
		struct bitmap   bm;

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

		/*
		 * Now, let's fill the bitmap from the password file
		 */
		SETGRENT();
		while ((grp = GETGRENT()) != NULL)
			if ((gid_t)grp->gr_gid >= (gid_t)cnf->min_gid &&
                            (gid_t)grp->gr_gid <= (gid_t)cnf->max_gid)
				bm_setbit(&bm, grp->gr_gid - cnf->min_gid);
		ENDGRENT();

		/*
		 * Then apply the policy, with fallback to reuse if necessary
		 */
		if (cnf->reuse_gids)
			gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
		else {
			gid = (gid_t) (bm_lastset(&bm) + 1);
			if (!bm_isset(&bm, gid))
				gid += cnf->min_gid;
			else
				gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
		}

		/*
		 * Another sanity check
		 */
		if (gid < cnf->min_gid || gid > cnf->max_gid)
			errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used");
		bm_dealloc(&bm);
	}
	return gid;
}
Example #2
0
static void set_unset_test() {
  printf("Set/Unset Tests\n");

  bitmask bm;
  bm_create(&bm);

  int bit, hi, low;

  hi = 0; low = 0;
  assert_int_equals(hi, bm_gethibit(&bm), "Basic Test Hi 1");
  assert_int_equals(low, bm_getlowbit(&bm), "Basic Test Low 1");

  bit = 5; hi = 5; low = 5;
  bm_set(&bm, bit);
  assert_int_equals(hi, bm_gethibit(&bm), "Basic Test Hi 1");
  assert_int_equals(low, bm_getlowbit(&bm), "Basic Test Low 1");
  assert_true(bm_isset(&bm, bit), "Basic Test isset 1");

  bit = 10; hi = 10; low = 5;
  bm_set(&bm, bit);
  assert_int_equals(hi, bm_gethibit(&bm), "Basic Test Hi 2");
  assert_int_equals(low, bm_getlowbit(&bm), "Basic Test Low 2");
  assert_true(bm_isset(&bm, bit), "Basic Test isset 2");

  bit = 7; hi = 10; low = 5;
  bm_set(&bm, bit);
  assert_int_equals(hi, bm_gethibit(&bm), "Basic Test Hi 3");
  assert_int_equals(low, bm_getlowbit(&bm), "Basic Test Low 2");
  assert_true(bm_isset(&bm, bit), "Basic Test isset 3");

  bit = 5; hi = 10; low = 7;
  bm_unset(&bm, bit);
  assert_int_equals(hi, bm_gethibit(&bm), "Basic Test Hi 3");
  assert_int_equals(low, bm_getlowbit(&bm), "Basic Test Low 2");
  assert_false(bm_isset(&bm, bit), "Basic Test isset 4");
}