Ejemplo n.º 1
0
int
do_set(
	const char *path_p,
	const struct stat *st,
	const seq_t seq)
{
	acl_t old_acl = NULL, old_default_acl = NULL;
	acl_t acl = NULL, default_acl = NULL;
	acl_t *xacl, *old_xacl;
	acl_entry_t ent;
	cmd_t cmd;
	int which_entry;
	int errors = 0, error;
	char *acl_text;
	int acl_modified = 0, default_acl_modified = 0;
	int acl_mask_provided = 0, default_acl_mask_provided = 0;

	/* Execute the commands in seq (read ACLs on demand) */
	error = seq_get_cmd(seq, SEQ_FIRST_CMD, &cmd);
	if (error == 0)
		return 0;
	while (error == 1) {
		if (cmd->c_type == ACL_TYPE_ACCESS) {
			xacl = &acl;
			old_xacl = &old_acl;
			acl_modified = 1;
			if (cmd->c_tag == ACL_MASK)
				acl_mask_provided = 1;
		} else {
			xacl = &default_acl;
			old_xacl = &old_default_acl;
			default_acl_modified = 1;
			if (cmd->c_tag == ACL_MASK)
				default_acl_mask_provided = 1;
		}

		RETRIEVE_ACL(cmd->c_type);

		/* Check for `X', and replace with `x' as appropriate. */
		if (cmd->c_perm & CMD_PERM_COND_EXECUTE) {
			cmd->c_perm &= ~CMD_PERM_COND_EXECUTE;
			if (S_ISDIR(st->st_mode) || has_execute_perms(*xacl))
				cmd->c_perm |= CMD_PERM_EXECUTE;
		}

		switch(cmd->c_cmd) {
			case CMD_ENTRY_REPLACE:
				ent = find_entry(*xacl, cmd->c_tag, cmd->c_id);
				if (!ent) {
					if (acl_create_entry(xacl, &ent) != 0)
						goto fail;
					acl_set_tag_type(ent, cmd->c_tag);
					if (cmd->c_id != ACL_UNDEFINED_ID)
						acl_set_qualifier(ent,
								  &cmd->c_id);
				}
				set_perm(ent, cmd->c_perm, ~cmd->c_perm);
				break;

			case CMD_ENTRY_ADD:
				ent = find_entry(*xacl, cmd->c_tag, cmd->c_id);
				if (ent)
					set_perm(ent, cmd->c_perm, 0);
				break;

			case CMD_ENTRY_SUBTRACT:
				ent = find_entry(*xacl, cmd->c_tag, cmd->c_id);
				if (ent)
					set_perm(ent, 0, cmd->c_perm);
				break;

			case CMD_REMOVE_ENTRY:
				ent = find_entry(*xacl, cmd->c_tag, cmd->c_id);
				if (ent)
					acl_delete_entry(*xacl, ent);
				else
					/* ignore */;
				break;

			case CMD_REMOVE_EXTENDED_ACL:
				remove_extended_entries(acl);
				break;

			case CMD_REMOVE_ACL:
				acl_free(*xacl);
				*xacl = acl_init(5);
				if (!*xacl)
					goto fail;
				break;

			default:
				errno = EINVAL;
				goto fail;
		}

		error = seq_get_cmd(seq, SEQ_NEXT_CMD, &cmd);
	}

	if (error < 0)
		goto fail;

	/* Try to fill in missing entries */
	if (default_acl && acl_entries(default_acl) != 0) {
		xacl = &acl;
		old_xacl = &old_acl;
	
		if (!find_entry(default_acl, ACL_USER_OBJ, ACL_UNDEFINED_ID)) {
			if (!acl)
				RETRIEVE_ACL(ACL_TYPE_ACCESS);
			clone_entry(acl, ACL_USER_OBJ,
			            &default_acl, ACL_USER_OBJ);
		}
		if (!find_entry(default_acl, ACL_GROUP_OBJ, ACL_UNDEFINED_ID)) {
			if (!acl)
				RETRIEVE_ACL(ACL_TYPE_ACCESS);
			clone_entry(acl, ACL_GROUP_OBJ,
			            &default_acl, ACL_GROUP_OBJ);
		}
		if (!find_entry(default_acl, ACL_OTHER, ACL_UNDEFINED_ID)) {
			if (!acl)
				RETRIEVE_ACL(ACL_TYPE_ACCESS);
			clone_entry(acl, ACL_OTHER,
			            &default_acl, ACL_OTHER);
		}
	}

	/* update mask entries and check if ACLs are valid */
	if (acl && acl_modified) {
		if (acl_equiv_mode(acl, NULL) != 0) {
			if (!acl_mask_provided &&
			    !find_entry(acl, ACL_MASK, ACL_UNDEFINED_ID))
				clone_entry(acl, ACL_GROUP_OBJ,
				            &acl, ACL_MASK);
			if (opt_recalculate != -1 &&
			    (!acl_mask_provided || opt_recalculate == 1))
				acl_calc_mask(&acl);
		}

		error = acl_check(acl, &which_entry);
		if (error < 0)
			goto fail;
		if (error > 0) {
			acl_text = acl_to_any_text(acl, NULL, ',', 0);
			fprintf(stderr, gettext("%s: %s: Malformed access ACL "
				"`%s': %s at entry %d\n"), progname, path_p,
				acl_text, acl_error(error), which_entry+1);
			acl_free(acl_text);
			errors++;
			goto cleanup;
		}
	}

	if (default_acl && acl_entries(default_acl) != 0 &&
	    default_acl_modified) {
		if (acl_equiv_mode(default_acl, NULL) != 0) {
			if (!default_acl_mask_provided &&
			    !find_entry(default_acl,ACL_MASK,ACL_UNDEFINED_ID))
				clone_entry(default_acl, ACL_GROUP_OBJ,
				            &default_acl, ACL_MASK);
			if (opt_recalculate != -1 &&
			    (!default_acl_mask_provided ||
			     opt_recalculate == 1))
				acl_calc_mask(&default_acl);
		}

		error = acl_check(default_acl, &which_entry);
		if (error < 0)
			goto fail;
		if (error > 0) {
			acl_text = acl_to_any_text(default_acl, NULL, ',', 0);
			fprintf(stderr, gettext("%s: %s: Malformed default ACL "
			                  "`%s': %s at entry %d\n"),
				progname, path_p, acl_text,
				acl_error(error), which_entry+1);
			acl_free(acl_text);
			errors++;
			goto cleanup;
		}
	}

	/* Only directores can have default ACLs */
	if (default_acl && !S_ISDIR(st->st_mode) && opt_recursive) {
		/* In recursive mode, ignore default ACLs for files */
		acl_free(default_acl);
		default_acl = NULL;
	}

	/* check which ACLs have changed */
	if (acl && old_acl && acl_cmp(old_acl, acl) == 0) {
		acl_free(acl);
		acl = NULL;
	}
	if ((default_acl && old_default_acl &&
	    acl_cmp(old_default_acl, default_acl) == 0)) {
		acl_free(default_acl);
		default_acl = NULL;
	}

	/* update the file system */
	if (opt_test) {
		print_test(stdout, path_p, st,
		           acl, default_acl);
		goto cleanup;
	}
	if (acl) {
		if (acl_set_file(path_p, ACL_TYPE_ACCESS, acl) != 0) {
			if (errno == ENOSYS || errno == ENOTSUP) {
				int saved_errno = errno;
				mode_t mode;

				if (acl_equiv_mode(acl, &mode) != 0) {
					errno = saved_errno;
					goto fail;
				} else if (chmod(path_p, mode) != 0)
					goto fail;
			} else
				goto fail;
		}
	}
	if (default_acl) {
		if (S_ISDIR(st->st_mode)) {
			if (acl_entries(default_acl) == 0) {
				if (acl_delete_def_file(path_p) != 0 &&
				    errno != ENOSYS && errno != ENOTSUP)
					goto fail;
			} else {
				if (acl_set_file(path_p, ACL_TYPE_DEFAULT,
						 default_acl) != 0)
					goto fail;
			}
		} else {
			if (acl_entries(default_acl) != 0) {
				fprintf(stderr, gettext(
						"%s: %s: Only directories "
						"can have default ACLs\n"),
					progname, path_p);
				errors++;
				goto cleanup;
			}
		}
	}

	error = 0;

cleanup:
	if (acl)
		acl_free(acl);
	if (old_acl)
		acl_free(old_acl);
	if (default_acl)
		acl_free(default_acl);
	if (old_default_acl)
		acl_free(old_default_acl);
	return errors;
	
fail:
	fprintf(stderr, "%s: %s: %s\n", progname, path_p, strerror(errno));
	errors++;
	goto cleanup;
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
    struct posix_acl *user_acl;
    int f;
    int err;
    int uid;
    char *perm = NULL;
    int index;
    int c;
    int op = 1;
    int time;

    uid_t current_uid;
    struct stat sb;

    user_acl = malloc(sizeof(struct posix_acl));
    user_acl->a_count = 0;

    while ((c = getopt(argc, argv, "hu:g:s:p:t:m:")) != -1) {
        switch (c) {
        case 'h':
            print_help();
            op++;
            return 1;
        case 'u':
            index = user_acl->a_count;
            if (!strcmp(optarg, "o")) {
                user_acl->a_entries[index].e_tag = ACL_USER_OBJ;
                user_acl->a_entries[index].e_id =
                    ACL_UNDEFINED_ID;
            } else {
                user_acl->a_entries[index].e_tag = ACL_USER;
                user_acl->a_entries[index].e_id = atoi(optarg);
            }
            user_acl->a_count = index + 1;
            op = op + 2;
            break;
        case 'g':
            index = user_acl->a_count;
            if (!strcmp(optarg, "g")) {
                user_acl->a_entries[index].e_tag =
                    ACL_GROUP_OBJ;
                user_acl->a_entries[index].e_id =
                    ACL_UNDEFINED_ID;
            } else {
                user_acl->a_entries[index].e_tag = ACL_GROUP;
                user_acl->a_entries[index].e_id = atoi(optarg);
            }
            user_acl->a_count = index + 1;
            op = op + 2;
            break;
        case 's':
            index = user_acl->a_count;
            user_acl->a_entries[index].e_tag = ACL_SESSION;
            user_acl->a_entries[index].e_id = atoi(optarg);
            user_acl->a_count = index + 1;
            op = op + 2;
            break;
        case 'p':
            index = user_acl->a_count;
            user_acl->a_entries[index].e_tag = ACL_PROCESS;
            user_acl->a_entries[index].e_id = atoi(optarg);
            user_acl->a_count = index + 1;
            op = op + 2;
            break;
        case 't':
            printf("check t: optarg = %s\n", optarg);
            index = user_acl->a_count;
            time = parse_time(optarg);
            if (time < 0) {
                printf("error pase time\n");
                return -1;
            }
            user_acl->a_entries[index].e_tag = ACL_TIME;
            user_acl->a_entries[index].e_id = time;
            user_acl->a_count = index + 1;
            op = op + 2;
            break;
        case 'm':
            perm = optarg;
            break;
        }
    }

    if (perm != NULL) {
        set_perm(user_acl, perm);
        op = op + 2;
    }
    print_acl(user_acl);

    current_uid = getuid();
    if (stat(argv[op], &sb) == -1) {
        printf("error get file stat");
        return -1;
    }
    if (getuid() != sb.st_uid) {
        printf("You can't set acl on this file, because you do not own this file.");
        return -1;
    }

    f = open(argv[op], O_RDWR);
    if (f < 0) {
        printf("error open file\n");
        return -1;
    }

    printf("etag: %d", user_acl->a_entries[0].e_tag);

    err = ioctl(f, ECRYPTFS_SETACL, user_acl);
    if (err < 0) {
        printf("Error set acl\n");
        return -1;
    }
    return 0;

}
Ejemplo n.º 3
0
Archivo: tbver.c Proyecto: prvn16/tb
int main(int argc, char **argv)
{
    int i, j;
    int color;
    int val, longindex;
    int pcs[16];
    int wdl_only = 0;
#ifdef SUICIDE
    int switched = 0;
#endif

    numthreads = 1;

    do {
//    val = getopt_long(argc, argv, "t:lwc", options, &longindex);
        val = getopt_long(argc, argv, "t:ld", options, &longindex);
        switch (val) {
        case 't':
            numthreads = atoi(optarg);
            break;
        case 'l':
            log = 1;
            break;
        case 'w':
            wdl_only = 1;
            break;
        case 'd':
            use_envdirs = 1;
            break;
        }
    } while (val != EOF);

    if (optind >= argc) {
        fprintf(stderr, "No tablebase specified.\n");
        exit(1);
    }
    tablename = argv[optind];

    init_tablebases();

    for (i = 0; i < 16; i++)
        pcs[i] = 0;

    numpcs = strlen(tablename) - 1;
    color = 0;
    j = 0;
    for (i = 0; i < strlen(tablename); i++)
        switch (tablename[i]) {
        case 'P':
            pcs[PAWN | color]++;
            pt[j++] = PAWN | color;
            break;
        case 'N':
            pcs[KNIGHT | color]++;
            pt[j++] = KNIGHT | color;
            break;
        case 'B':
            pcs[BISHOP | color]++;
            pt[j++] = BISHOP | color;
            break;
        case 'R':
            pcs[ROOK | color]++;
            pt[j++] = ROOK | color;
            break;
        case 'Q':
            pcs[QUEEN | color]++;
            pt[j++] = QUEEN | color;
            break;
        case 'K':
            pcs[KING | color]++;
            pt[j++] = KING | color;
            break;
        case 'v':
            if (color) exit(1);
            color = 0x08;
            break;
        default:
            exit(1);
        }
    if (!color) exit(1);

    if (pcs[WPAWN] || pcs[BPAWN]) {
        fprintf(stderr, "Can't handle pawns.\n");
        exit(1);
    }

    if (numthreads < 1) numthreads = 1;
    else if (numthreads > MAX_THREADS) numthreads = MAX_THREADS;

    printf("number of threads = %d\n", numthreads);

    if (numthreads == 1)
        total_work = 1;
    else
        total_work = 100 + 10 * numthreads;

    for (i = 0; i < numpcs; i++) {
        shift[i] = (numpcs - i - 1) * 6;
        mask[i] = 0x3fULL << shift[i];
    }

#ifndef SMALL
    size = 10ULL << (6 * (numpcs-1));
#else
    size = 462ULL << (6 * (numpcs-2));

    mask[0] = 0x1ffULL << shift[1];
#endif

    work_g = create_work(total_work, size, 0x3f);
#ifndef SMALL
    work_piv = create_work(total_work, 1ULL << shift[0], 0);
#else
    work_piv0 = create_work(total_work, 1ULL << shift[0], 0);
    work_piv1 = create_work(total_work, 10ULL << shift[1], 0);
#endif

    static int piece_order[16] = {
        0, 0, 3, 5, 7, 9, 1, 0,
        0, 0, 4, 6, 8, 10, 2, 0
    };

#ifdef SUICIDE
    j = pt[0];
    for (i = 1; i < numpcs; i++)
        if (piece_order[pt[i]] < piece_order[j])
            j = pt[i];
    if (j & 0x08) {
        for (i = 0; i < numpcs; i++)
            pt[i] ^= 0x08;
        for (i = 0; i < 8; i++) {
            int tmp = pcs[i];
            pcs[i] = pcs[i + 8];
            pcs[i + 8] = tmp;
        }
        switched = 1;
    }
#endif

    for (i = 0; i < numpcs; i++)
        for (j = i + 1; j < numpcs; j++)
            if (piece_order[pt[i]] > piece_order[pt[j]]) {
                int tmp = pt[i];
                pt[i] = pt[j];
                pt[j] = tmp;
            }

    for (i = 0, j = 0; i < numpcs; i++)
        if (!(pt[i] & 0x08))
            white_pcs[j++] = i;
    white_pcs[j] = -1;

    for (i = 0, j = 0; i < numpcs; i++)
        if (pt[i] & 0x08)
            black_pcs[j++] = i;
    black_pcs[j] = -1;

    idx_mask1[numpcs - 1] = 0xffffffffffffffc0ULL;
    idx_mask2[numpcs - 1] = 0;
    for (i = numpcs - 2; i >= 0; i--) {
        idx_mask1[i] = idx_mask1[i + 1] << 6;
        idx_mask2[i] = (idx_mask2[i + 1] << 6) | 0x3f;
    }

#ifndef SUICIDE
    for (i = 0; i < numpcs; i++)
        if (pt[i] == WKING)
            white_king = i;

    for (i = 0; i < numpcs; i++)
        if (pt[i] == BKING)
            black_king = i;
#endif

    table_w = alloc_huge(2 * size);
    table_b = table_w + size;

    printf("Verifying %s.\n", tablename);
    if (log) {
        L = fopen(LOGFILE, "a");
        fprintf(L, "Verifying %s...", tablename);
        fflush(L);
    }

    init_threads(0);
    init_tables();

    gettimeofday(&start_time, NULL);
    cur_time = start_time;

    printf("Initialising broken positions.\n");
    run_threaded(calc_broken, work_g, 1);
    printf("Calculating white captures.\n");
    calc_captures_w();
    printf("Calculating black captures.\n");
    calc_captures_b();
#ifndef SUICIDE
    printf("Calculating mate positions.\n");
    run_threaded(calc_mates, work_g, 1);
#else
    init_capt_threat();
    printf("Calculating white threats.\n");
    iter_table = table_b;
    iter_table_opp = table_w;
    iter_pcs_opp = white_pcs;
    run_threaded(calc_threats, work_g, 1);
    printf("Calculating black threats.\n");
    iter_table = table_w;
    iter_table_opp = table_b;
    iter_pcs_opp = black_pcs;
    run_threaded(calc_threats, work_g, 1);
#endif

    decomp_init_piece(pcs);

    // open wdl table
    struct tb_handle *H = open_tb(tablename, 1);
    decomp_init_table(H);
    load_entry = (struct TBEntry_piece *)get_entry(H);

    if (!wdl_only) {
        // white, wdl
        printf("Loading wdl, white.\n");
        tb_table = decompress_table(H, 0, 0);
        set_perm(H, 0, 0, tb_perm, pt);
        load_table = table_w;
        load_bside = 0;
        run_threaded(load_wdl, work_g, 1);

        // black, wdl
        printf("Loading wdl, black.\n");
        tb_table = decompress_table(H, 1, 0);
        set_perm(H, 1, 0, tb_perm, pt);
        load_table = table_b;
        load_bside = 1;
        run_threaded(load_wdl, work_g, 1);
        close_tb(H);

        // open dtz table
        H = open_tb(tablename, 0);
        decomp_init_table(H);
        load_entry = (struct TBEntry_piece *)get_entry(H);
        ply_accurate_win = get_ply_accurate_win(H, 0);
        ply_accurate_loss = get_ply_accurate_loss(H, 0);
        load_map = get_dtz_map(H, 0);
        int dtz_side = get_dtz_side(H, 0);
        init_wdl_dtz();

        // dtz
        printf("Loading dtz, %s.\n" , dtz_side == 0 ? "white" : "black");
        tb_table = decompress_table(H, 0, 0);
        set_perm(H, 0, 0, tb_perm, pt);
        load_table = dtz_side == 0 ? table_w : table_b;
        load_bside = 0;
        if (load_map)
            run_threaded(load_dtz_mapped, work_g, 1);
        else
            run_threaded(load_dtz, work_g, 1);
        close_tb(H);

        if (dtz_side == 0) {
            load_table = table_w;
            load_opp_table = table_b;
            load_pieces = white_pcs;
            load_opp_pieces = black_pcs;
        } else {
            load_table = table_b;
            load_opp_table = table_w;
            load_pieces = black_pcs;
            load_opp_pieces = white_pcs;
        }

        printf("Verifying %s.\n", dtz_side ? "white" : "black");
        run_threaded(verify_opp, work_g, 1);
        printf("Verifying %s.\n", dtz_side ? "black" : "white");
        run_threaded(verify_dtz, work_g, 1);
    } else { // currently broken (and disabled)
        // white, wdl
        printf("Loading wdl, white.\n");
        tb_table = decompress_table(H, 0, 0);
        set_perm(H, 0, 0, tb_perm, pt);
        load_table = table_w;
        load_bside = 0;
        run_threaded(wdl_load_wdl, work_g, 1);

        // black, wdl
        printf("Loading wdl, black.\n");
        tb_table = decompress_table(H, 1, 0);
        set_perm(H, 1, 0, tb_perm, pt);
        load_table = table_b;
        load_bside = 1;
        run_threaded(wdl_load_wdl, work_g, 1);
        close_tb(H);

        ply_accurate_win = get_ply_accurate_win(H, 0);
        ply_accurate_loss = get_ply_accurate_loss(H, 0);
        init_wdl();
        load_table = table_w;
        load_opp_table = table_b;
        load_pieces = white_pcs;
        printf("Verifying white.\n");
        run_threaded(verify_wdl, work_g, 1);

        ply_accurate_win = get_ply_accurate_loss(H, 0);
        ply_accurate_loss = get_ply_accurate_win(H, 0);
        init_wdl();
        load_table = table_b;
        load_opp_table = table_w;
        load_pieces = black_pcs;
        printf("Verifying black.\n");
        run_threaded(verify_wdl, work_g, 1);
    }

    if (num_errors == 0) {
        printf("No errors.\n");
        if (log) fprintf(L, " No errors.\n");
    }
    if (log) fclose(L);

    return 0;
}