summaryrefslogtreecommitdiffstats
path: root/ipa-python/ipaadminutil.py
blob: d94ced4d9c8e4c5026cdca5c3f5b0fad3415abca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Authors: Rob Crittenden <rcritten@redhat.com>
#
# Copyright (C) 2007    Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

import string
import tempfile
import logging
import subprocess
import os

def select_user(counter, users):
    """counter is the number of User objects in users
       users is a list of User objects

       This purposely doesn't catch KeyboardInterrupt
    """
    i = 1
    print "%s entries were found. Which one would you like to display?" % counter
    for ent in users:
        print "%s: %s (%s)" % (i, ent.getValues('cn'), ent.getValues('uid'))
        i += 1
    while True:
        resp = raw_input("Choose one: (1 - %s), 0 for all, q to quit: " % counter)
        if resp == "q":
            return "q"
        if resp == "0":
            userindex = -1
            break
        try:
            userindex = int(resp) - 1
            if (userindex >= 0 and userindex < counter):
                break
        except:
            # fall through to the error msg
            pass
 
        print "Please enter a number between 1 and %s" % counter

    return userindex

def select_group(counter, groups):
    """counter is the number of Group objects in users
       users is a list of Group objects

       This purposely doesn't catch KeyboardInterrupt
    """
    i = 1
    print "%s entries were found. Which one would you like to display?" % counter
    for ent in groups:
        print "%s: %s" % (i, ent.getValues('cn'))
        i += 1
    while True:
        resp = raw_input("Choose one: (1 - %s), 0 for all, q to quit: " % counter)
        if resp == "q":
            return "q"
        if resp == "0":
            groupindex = -1
            break
        try:
            groupindex = int(resp) - 1
            if (groupindex >= 0 and groupindex < counter):
                break
        except:
            # fall through to the error msg
            pass

        print "Please enter a number between 1 and %s" % counter

    return groupindex
351'>351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
/*
  Copyright (c) 2010-2013 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

#include "server.h"
#include "server-helpers.h"
#include "server-messages.h"


int
server_resolve_all (call_frame_t *frame);
int
resolve_entry_simple (call_frame_t *frame);
int
resolve_inode_simple (call_frame_t *frame);
int
resolve_continue (call_frame_t *frame);
int
resolve_anonfd_simple (call_frame_t *frame);

int
resolve_loc_touchup (call_frame_t *frame)
{
        server_state_t       *state = NULL;
        server_resolve_t     *resolve = NULL;
        loc_t                *loc = NULL;

        state = CALL_STATE (frame);

        resolve = state->resolve_now;
        loc     = state->loc_now;

        loc_touchup (loc, resolve->bname);
        return 0;
}


int
resolve_gfid_entry_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                        int op_ret, int op_errno, inode_t *inode,
                        struct iatt *buf, dict_t *xdata,
                        struct iatt *postparent)
{
        server_state_t       *state = NULL;
        server_resolve_t     *resolve = NULL;
        inode_t              *link_inode = NULL;
        loc_t                *resolve_loc = NULL;

        state = CALL_STATE (frame);
        resolve = state->resolve_now;
        resolve_loc = &resolve->resolve_loc;

        if (op_ret == -1) {
                if (op_errno == ENOENT) {
                        gf_msg_debug (this->name, 0,
                                      "%s/%s: failed to resolve (%s)",
                                      uuid_utoa (resolve_loc->pargfid),
                                      resolve_loc->name, strerror (op_errno));
                        if (resolve->type == RESOLVE_NOT) {
                                do {
                                        inode = inode_grep (state->itable,
                                                            resolve_loc->parent,
                                                            resolve->bname);

                                        if (inode) {
                                                gf_msg_debug (this->name, 0, "%s/%s: "
                                                              "removing stale dentry",
                                                              uuid_utoa (resolve_loc->pargfid),
                                                              resolve->bname);
                                                inode_unlink (inode,
                                                              resolve_loc->parent,
                                                              resolve->bname);
                                        }
                                } while (inode);
                        }
                } else {
                        gf_msg (this->name, GF_LOG_WARNING, op_errno,
                                PS_MSG_GFID_RESOLVE_FAILED, "%s/%s: failed to "
                                "resolve (%s)",
                                uuid_utoa (resolve_loc->pargfid),
                                resolve_loc->name, strerror (op_errno));
                }
                goto out;
        }

        link_inode = inode_link (inode, resolve_loc->parent,
                                 resolve_loc->name, buf);

        if (!link_inode)
                goto out;

        inode_lookup (link_inode);

        inode_unref (link_inode);

out:
        loc_wipe (resolve_loc);

        resolve_continue (frame);
        return 0;
}


int
resolve_gfid_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                  int op_ret, int op_errno, inode_t *inode, struct iatt *buf,
                  dict_t *xdata, struct iatt *postparent)
{
        server_state_t       *state = NULL;
        server_resolve_t     *resolve = NULL;
        inode_t              *link_inode = NULL;
        loc_t                *resolve_loc = NULL;
        dict_t               *dict = NULL;

        state = CALL_STATE (frame);
        resolve = state->resolve_now;
        resolve_loc = &resolve->resolve_loc;

        if (op_ret == -1) {
                if (op_errno == ENOENT) {
                        gf_msg_debug (this->name, GF_LOG_DEBUG,
                                      "%s: failed to resolve (%s)",
                                      uuid_utoa (resolve_loc->gfid),
                                      strerror (op_errno));
                } else {
                        gf_msg (this->name, GF_LOG_WARNING, op_errno,
                                PS_MSG_GFID_RESOLVE_FAILED,
                                "%s: failed to resolve (%s)",
                                uuid_utoa (resolve_loc->gfid),
                                strerror (op_errno));
                }
                loc_wipe (&resolve->resolve_loc);
                goto out;
        }

        link_inode = inode_link (inode, NULL, NULL, buf);

        if (!link_inode) {
                loc_wipe (resolve_loc);
                goto out;
        }

        inode_lookup (link_inode);

        /* wipe the loc only after the inode has been linked to the inode
           table. Otherwise before inode gets linked to the inode table,
           inode would have been unrefed (this might have been destroyed
           if refcount becomes 0, and put back to mempool). So once the
           inode gets destroyed, inode_link is a redundant operation. But
           without knowing that the destroyed inode's pointer is saved in
           the resolved_loc as parent (while constructing loc for resolving
           the entry) and the inode_new call for resolving the entry will
           return the same pointer to the inode as the parent (because in
           reality the inode is a free inode present in cold list of the
           inode mem-pool).
        */
        loc_wipe (resolve_loc);

        if (gf_uuid_is_null (resolve->pargfid)) {
                inode_unref (link_inode);
                goto out;
        }

        resolve_loc->parent = link_inode;
        gf_uuid_copy (resolve_loc->pargfid, resolve_loc->parent->gfid);

        resolve_loc->name = resolve->bname;

        resolve_loc->inode = server_inode_new (state->itable,
                                               resolve_loc->gfid);

        inode_path (resolve_loc->parent, resolve_loc->name,
                    (char **) &resolve_loc->path);

        if (state->xdata) {
                dict = dict_copy_with_ref (state->xdata, NULL);
                if (!dict)
                        gf_msg (this->name, GF_LOG_ERROR, ENOMEM, PS_MSG_NO_MEMORY,
                                "BUG: dict allocation failed (pargfid: %s, name: %s), "
                                "still continuing", uuid_utoa (resolve_loc->gfid),
                                resolve_loc->name);
        }

        STACK_WIND (frame, resolve_gfid_entry_cbk,
                    frame->root->client->bound_xl,
                    frame->root->client->bound_xl->fops->lookup,
                    &resolve->resolve_loc, dict);
        if (dict)
                dict_unref (dict);
        return 0;
out:
        resolve_continue (frame);
        return 0;
}


int
resolve_gfid (call_frame_t *frame)
{
        server_state_t       *state = NULL;
        xlator_t             *this = NULL;
        server_resolve_t     *resolve = NULL;
        loc_t                *resolve_loc = NULL;
        dict_t               *xdata = NULL;

        state = CALL_STATE (frame);
        this  = frame->this;
        resolve = state->resolve_now;
        resolve_loc = &resolve->resolve_loc;

        if (!gf_uuid_is_null (resolve->pargfid))
                gf_uuid_copy (resolve_loc->gfid, resolve->pargfid);
        else if (!gf_uuid_is_null (resolve->gfid))
                gf_uuid_copy (resolve_loc->gfid, resolve->gfid);

        resolve_loc->inode = server_inode_new (state->itable,
                                               resolve_loc->gfid);
        (void) loc_path (resolve_loc, NULL);

        if (state->xdata) {
                xdata = dict_copy_with_ref (state->xdata, NULL);
                if (!xdata)
                        gf_msg (this->name, GF_LOG_ERROR, ENOMEM, PS_MSG_NO_MEMORY,
                                "BUG: dict allocation failed (gfid: %s), "
                                "still continuing",
                                uuid_utoa (resolve_loc->gfid));
        }

        STACK_WIND (frame, resolve_gfid_cbk,
                    frame->root->client->bound_xl,
                    frame->root->client->bound_xl->fops->lookup,
                    &resolve->resolve_loc, xdata);

        if (xdata)
                dict_unref (xdata);

        return 0;
}

int
resolve_continue (call_frame_t *frame)
{
        server_state_t       *state = NULL;
        xlator_t             *this = NULL;
        server_resolve_t     *resolve = NULL;
        int                   ret = 0;

        state = CALL_STATE (frame);
        this  = frame->this;
        resolve = state->resolve_now;

        resolve->op_ret   = 0;
        resolve->op_errno = 0;

        if (resolve->fd_no != -1) {
                ret = resolve_anonfd_simple (frame);
                goto out;
        } else if (!gf_uuid_is_null (resolve->pargfid))
                ret = resolve_entry_simple (frame);
        else if (!gf_uuid_is_null (resolve->gfid))
                ret = resolve_inode_simple (frame);
        if (ret)
                gf_msg_debug (this->name, 0, "return value of resolve_*_"
                              "simple %d", ret);

        resolve_loc_touchup (frame);
out:
        server_resolve_all (frame);

        return 0;
}


/*
  Check if the requirements are fulfilled by entries in the inode cache itself
  Return value:
  <= 0 - simple resolution was decisive and complete (either success or failure)
  > 0  - indecisive, need to perform deep resolution
*/

int
resolve_entry_simple (call_frame_t *frame)
{
        server_state_t     *state = NULL;
        xlator_t           *this = NULL;
        server_resolve_t   *resolve = NULL;
        inode_t            *parent = NULL;
        inode_t            *inode = NULL;
        int                 ret = 0;

        state = CALL_STATE (frame);
        this  = frame->this;
        resolve = state->resolve_now;

        parent = inode_find (state->itable, resolve->pargfid);
        if (!parent) {
                /* simple resolution is indecisive. need to perform
                   deep resolution */
                resolve->op_ret   = -1;
                resolve->op_errno = ESTALE;
                ret = 1;
                goto out;
        }

        /* expected @parent was found from the inode cache */
        gf_uuid_copy (state->loc_now->pargfid, resolve->pargfid);
        state->loc_now->parent = inode_ref (parent);

        if (strstr (resolve->bname, "../")) {
                /* Resolving outside the parent's tree is not allowed */
                gf_msg (this->name, GF_LOG_ERROR, EPERM,
                        PS_MSG_GFID_RESOLVE_FAILED,
                        "%s: path sent by client not allowed",
                        resolve->bname);
                resolve->op_ret   = -1;
                resolve->op_errno = EPERM;
                ret = 1;
                goto out;
        }
        state->loc_now->name = resolve->bname;

        inode = inode_grep (state->itable, parent, resolve->bname);
        if (!inode) {
                switch (resolve->type) {
                case RESOLVE_DONTCARE:
                case RESOLVE_NOT:
                        ret = 0;
                        break;
                case RESOLVE_MAY:
                        ret = 1;
                        break;
                default:
                        resolve->op_ret   = -1;
                        resolve->op_errno = ENOENT;
                        ret = 1;
                        break;
                }

                goto out;
        }

        if (resolve->type == RESOLVE_NOT) {
                gf_msg_debug (this->name, 0, "inode (pointer: %p gfid:%s found"
                              " for path (%s) while type is RESOLVE_NOT. "
                              "Performing lookup on backend to rule out any "
                              "possible stale dentries in inode table",
                              inode, uuid_utoa (inode->gfid), resolve->path);
                resolve->op_ret   = -1;
                resolve->op_errno = EEXIST;
                ret = 1;
                goto out;
        }

        ret = 0;

        state->loc_now->inode  = inode_ref (inode);

out:
        if (parent)
                inode_unref (parent);

        if (inode)
                inode_unref (inode);

        return ret;
}


int
server_resolve_entry (call_frame_t *frame)
{
        server_state_t     *state = NULL;
        int                 ret = 0;
        loc_t              *loc = NULL;

        state = CALL_STATE (frame);
        loc  = state->loc_now;

        ret = resolve_entry_simple (frame);

        if (ret > 0) {
                loc_wipe (loc);
                resolve_gfid (frame);
                return 0;
        }

        if (ret == 0)
                resolve_loc_touchup (frame);

        server_resolve_all (frame);

        return 0;
}


int
resolve_inode_simple (call_frame_t *frame)
{
        server_state_t     *state = NULL;
        server_resolve_t   *resolve = NULL;
        inode_t            *inode = NULL;
        int                 ret = 0;

        state = CALL_STATE (frame);
        resolve = state->resolve_now;

        inode = inode_find (state->itable, resolve->gfid);

        if (!inode) {
                resolve->op_ret   = -1;
                resolve->op_errno = ESTALE;
                ret = 1;
                goto out;
        }

        ret = 0;

        state->loc_now->inode = inode_ref (inode);
        gf_uuid_copy (state->loc_now->gfid, resolve->gfid);

out:
        if (inode)
                inode_unref (inode);

        return ret;
}


int
server_resolve_inode (call_frame_t *frame)
{
        server_state_t     *state = NULL;
        int                 ret = 0;
        loc_t              *loc = NULL;

        state = CALL_STATE (frame);
        loc  = state->loc_now;

        ret = resolve_inode_simple (frame);

        if (ret > 0) {
                loc_wipe (loc);
                resolve_gfid (frame);
                return 0;
        }

        if (ret == 0)
                resolve_loc_touchup (frame);

        server_resolve_all (frame);

        return 0;
}


int
resolve_anonfd_simple (call_frame_t *frame)
{
        server_state_t     *state = NULL;
        server_resolve_t   *resolve = NULL;
        inode_t            *inode = NULL;
        int                 ret = 0;

        state = CALL_STATE (frame);
        resolve = state->resolve_now;

        inode = inode_find (state->itable, resolve->gfid);

        if (!inode) {
                resolve->op_ret   = -1;
                resolve->op_errno = ENOENT;
                ret = 1;
                goto out;
        }

        ret = 0;

        if (frame->root->op == GF_FOP_READ || frame->root->op == GF_FOP_WRITE)
                state->fd = fd_anonymous_with_flags (inode, state->flags);
        else
                state->fd = fd_anonymous (inode);
out:
        if (inode)
                inode_unref (inode);

        if (ret != 0)
                gf_msg_debug ("server", 0, "inode for the gfid"
                              "(%s) is not found. anonymous fd creation failed",
                              uuid_utoa (resolve->gfid));
        return ret;
}


int
server_resolve_anonfd (call_frame_t *frame)
{
        server_state_t     *state = NULL;
        int                 ret = 0;
        loc_t              *loc = NULL;

        state = CALL_STATE (frame);
        loc  = state->loc_now;