(file) Return to CIMUserCommand.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Clients / cimuser

   1 mike  1.2 //%/////////////////////////////////////////////////////////////////////////////
   2           //
   3 kumpf 1.16 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
   4 mike  1.2  // The Open Group, Tivoli Systems
   5            //
   6            // Permission is hereby granted, free of charge, to any person obtaining a copy
   7            // of this software and associated documentation files (the "Software"), to
   8            // deal in the Software without restriction, including without limitation the
   9            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10            // sell copies of the Software, and to permit persons to whom the Software is
  11            // furnished to do so, subject to the following conditions:
  12 kumpf 1.16 // 
  13 mike  1.2  // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
  14            // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
  15            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
  16            // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  17            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19            // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21            //
  22            //==============================================================================
  23            //
  24            // Author: Sushma Fernandes, Hewlett Packard Company (sushma_fernandes@hp.com)
  25            //
  26 kumpf 1.4  // Modified By: Nag Boranna, Hewlett-Packard Company (nagaraja_boranna@hp.com)
  27 kumpf 1.21 //              Carol Ann Krug Graves, Hewlett-Packard Company
  28            //                (carolann_graves@hp.com)
  29 kumpf 1.4  //
  30 mike  1.2  //
  31            //%/////////////////////////////////////////////////////////////////////////////
  32            
  33            #include <Pegasus/Common/Config.h>
  34 kumpf 1.17 #include <Pegasus/Common/Constants.h>
  35 mike  1.2  
  36            #include <iostream>
  37            
  38            #include <Clients/cliutils/Command.h>
  39            #include <Clients/cliutils/CommandException.h>
  40            
  41            #include <Pegasus/getoopt/getoopt.h>
  42            
  43            #include <Pegasus/Client/CIMClient.h>
  44            #include <Pegasus/Common/CIMProperty.h>
  45 kumpf 1.13 #include <Pegasus/Common/CIMObjectPath.h>
  46 mike  1.2  #include <Pegasus/Common/CIMStatusCode.h>
  47            #include <Pegasus/Common/String.h>
  48            #include <Pegasus/Common/System.h>
  49 kumpf 1.11 #include <Pegasus/Common/PegasusVersion.h>
  50 mike  1.2  
  51            PEGASUS_USING_STD;
  52            
  53            PEGASUS_NAMESPACE_BEGIN
  54            
  55            /**
  56                The command name.
  57            */
  58            static const char COMMAND_NAME []              = "cimuser";
  59            
  60            /**
  61                This constant represents the name of the User name property in the schema
  62            */
  63 kumpf 1.21 static const CIMName PROPERTY_NAME_USER_NAME            = CIMName ("Username");
  64 mike  1.2  
  65            /**
  66                This constant represents the name of the Password property in the schema
  67            */
  68 kumpf 1.21 static const CIMName PROPERTY_NAME_PASSWORD             = CIMName ("Password");
  69 mike  1.2  
  70            /**
  71                The usage string for this command.  This string is displayed
  72                when an error occurs in parsing or validating the command line.
  73            */
  74            static const char USAGE []                     = "usage: ";
  75            
  76            /**
  77                This constant represents the getoopt argument designator
  78            */
  79            static const char GETOPT_ARGUMENT_DESIGNATOR   = ':';
  80            
  81            /*
  82                These constants represent the operation modes supported by the CLI.
  83                Any new operation should be added here.
  84            */
  85            
  86            /**
  87                This constant signifies that an operation option has not been recorded
  88            */
  89 kumpf 1.9  static const Uint32 OPERATION_TYPE_UNINITIALIZED  = 0;
  90 mike  1.2  
  91            /**
  92                This constant represents a add user operation
  93            */
  94 kumpf 1.9  static const Uint32 OPERATION_TYPE_ADD            = 1;
  95 mike  1.2  
  96            /**
  97                This constant represents a add user operation
  98            */
  99 kumpf 1.9  static const Uint32 OPERATION_TYPE_MODIFY         = 2;
 100 mike  1.2  
 101            /**
 102                This constant represents a remove user operation
 103            */
 104 kumpf 1.9  static const Uint32 OPERATION_TYPE_REMOVE         = 3;
 105 mike  1.2  
 106            /**
 107                This constant represents a list operation
 108            */
 109 kumpf 1.9  static const Uint32 OPERATION_TYPE_LIST           = 4;
 110 mike  1.2  
 111            
 112            /**
 113                The constants representing the messages.
 114            */
 115            
 116            static const char NOT_PRIVILEGED_USER []         = 
 117 kumpf 1.22              "You must have superuser privilege to run this command.";
 118            
 119 mike  1.2  static const char CIMOM_NOT_RUNNING []         = 
 120 kumpf 1.22                         "CIM Server may not be running.";
 121 mike  1.2  
 122            static const char ADD_USER_FAILURE []    = 
 123                                    "Failed to add user.";
 124            
 125            static const char REMOVE_USER_FAILURE []    = 
 126                                    "Failed to remove user.";
 127            
 128            static const char CHANGE_PASSWORD_FAILURE []  = 
 129                                    "Failed to change password.";
 130            
 131            static const char LIST_USERS_FAILURE [] = 
 132                                    "Failed to list the users. ";
 133            
 134            static const char ADD_USER_SUCCESS []    = 
 135                                    "User added successfully.";
 136            
 137            static const char REMOVE_USER_SUCCESS[]  = 
 138                                    "User removed successfully.";
 139            
 140            static const char CHANGE_PASSWORD_SUCCESS []  = 
 141                                    "Password changed successfully.";
 142 mike  1.2  
 143            static const char PASSWORD_BLANK []  = 
 144                                 "Password cannot be blank. Please re-enter your password.";
 145            
 146            static const char NO_USERS_FOUND[] =
 147            			"No users found for listing.";
 148            
 149            static const char AUTH_SCHEMA_NOT_LOADED []  =
 150 kumpf 1.22     "Please restore the internal repository on the CIM Server.";
 151 mike  1.2  
 152            static const char REQUIRED_ARGS_MISSING []        =
 153                                    "Required arguments missing.";
 154            
 155            static const char INVALID_ARGS []        =
 156                                    "Invalid arguments.";
 157            
 158            static const char USER_ALREADY_EXISTS []        =
 159 kumpf 1.22                         "Specified user name already exist.";
 160 mike  1.2  
 161            static const char USER_NOT_FOUND []        =
 162                                    "Specified user name was not found.";
 163            
 164            static const char USERNAME_REQUIRED []        =
 165                                    "User name is required.";
 166            
 167            /**
 168                The option character used to specify add user.
 169            */
 170            static const char   OPTION_ADD                 = 'a';
 171            
 172            /**
 173                The option character used to specify modify user.
 174            */
 175            static const char   OPTION_MODIFY              = 'm';
 176            
 177            /**
 178                The option character used to specify user name.
 179            */
 180            static const char   OPTION_USER_NAME           = 'u';
 181 mike  1.2  
 182            /**
 183                The option character used to specify password.
 184            */
 185            static const char   OPTION_PASSWORD            = 'w';
 186            
 187            /**
 188                The option character used to specify new password.
 189            */
 190            static const char   OPTION_NEW_PASSWORD        = 'n';
 191            
 192            /**
 193                The option character used to specify remove user.
 194            */
 195            static const char   OPTION_REMOVE              = 'r';
 196            
 197            /**
 198                The option character used to specify listing of users.
 199            */
 200            static const char   OPTION_LIST                = 'l';
 201            
 202 mike  1.2  /**
 203                The name of the Method that implements modify password
 204            */
 205 kumpf 1.21 static const CIMName   MODIFY_METHOD            = CIMName ("modifyPassword");
 206 mike  1.2  
 207            /**
 208                The input parameter name for old password 
 209            */
 210 kumpf 1.3  static const char   OLD_PASS_PARAM[]             = "OldPassword";
 211 mike  1.2  
 212            /**
 213                The input parameter name for new password 
 214            */
 215 kumpf 1.3  static const char   NEW_PASS_PARAM[]             = "NewPassword";
 216 mike  1.2  
 217            
 218            static const char   PASSWORD_PROMPT []  =
 219                                    "Please enter your password: ";
 220            
 221            static const char   OLD_PASSWORD_PROMPT []  =
 222                                    "Please enter your old password: ";
 223            
 224            static const char   RE_ENTER_PROMPT []  =
 225                                    "Please re-enter your password: ";
 226            
 227            static const char   NEW_PASSWORD_PROMPT []  =
 228                                    "Please enter your new password: ";
 229            
 230            static const char   PASSWORD_DOES_NOT_MATCH []  =
 231                                    "Passwords do not match. Please Re-enter.";
 232            
 233            static const char   PASSWORD_SAME_ERROR []  =
 234                                    "Error, new and old passwords cannot be same.";
 235            
 236            /**
 237 mike  1.2  This is a CLI used to manage users of the CIM Server.  This command supports 
 238            operations to add, modify, list and remove users.  
 239            
 240            @author Sushma Fernandes, Hewlett-Packard Company
 241            */
 242            
 243            class CIMUserCommand : public Command 
 244            {
 245            
 246            public:
 247            
 248                /**    
 249                    Constructs a CIMUserCommand and initializes instance variables.
 250                */
 251                CIMUserCommand ();
 252            
 253                //
 254                // Overrides the virtual function setCommand from Command class
 255                // This is defined as an empty function. 
 256                //
 257                void setCommand (
 258 mike  1.2  		      Uint32                   argc, 
 259            		      char*                    argv [])
 260            		      throw (CommandFormatException)
 261                {
 262                    // Empty function 
 263                }
 264            
 265                /**
 266                Parses the command line, validates the options, and sets instance 
 267                variables based on the option arguments. This implementation of
 268                setCommand includes the parameters for output and error stream. 
 269            
 270                @param  ostream    The stream to which command output is written.
 271                @param  ostream    The stream to which command errors are written.
 272                @param  args       The string array containing the command line arguments
 273                @param  argc       The int containing the arguments count
 274            
 275                @throws  CommandFormatException  if an error is encountered in parsing
 276                                                 the command line
 277                */
 278                void setCommand (
 279 mike  1.2                        ostream&                outPrintWriter, 
 280                                  ostream&                errPrintWriter,
 281            		      Uint32                  argc, 
 282            		      char*                   argv []);
 283            
 284                /**
 285                Executes the command and writes the results to the output streams.
 286            
 287                @param ostream    The stream to which command output is written.
 288                @param ostream    The stream to which command errors are written.
 289                @return  0        if the command is successful
 290                         1        if an error occurs in executing the command
 291                */
 292                Uint32 execute ( 
 293            		      ostream&                outPrintWriter,
 294                                  ostream&                errPrintWriter);
 295            
 296            
 297            private:
 298            
 299                //
 300 mike  1.2      // Add a new user to the CIM Server
 301                //
 302                // @param ostream        The stream to which command output is written.
 303                // @param ostream        The stream to which command errors are written.
 304                // 
 305 kumpf 1.19     // @exception Exception  if failed to add user
 306 mike  1.2      //
 307                void _AddUser
 308                    (
 309                    ostream&    		outPrintWriter, 
 310                    ostream&    		errPrintWriter
 311                    ); 
 312            
 313                //
 314                // Modify an existing user's password.
 315                //
 316                // @param ostream          The stream to which command output is written.
 317                // @param ostream          The stream to which command errors are written.
 318                // 
 319 kumpf 1.19     // @exception Exception  if failed to modify password
 320 mike  1.2      //
 321                void _ModifyUser
 322                    (
 323                    ostream&                 outPrintWriter,
 324                    ostream&                 errPrintWriter
 325                    );
 326            
 327                //
 328                // Remove an existing user from the CIM Server 
 329                //  
 330                // @param ostream          The stream to which command output is written.
 331                // @param ostream          The stream to which command errors are written.
 332                //
 333 kumpf 1.19     // @exception Exception  if failed to remove user
 334 mike  1.2      //
 335                void _RemoveUser
 336                    (
 337                    ostream&		outPrintWriter, 
 338                    ostream&		errPrintWriter
 339                    ); 
 340            
 341                //
 342                // List all users.       
 343                // 
 344                // @param ostream        The stream to which command output is written.
 345                // @param ostream        The stream to which command errors are written.
 346                //
 347                void _ListUsers
 348                (
 349            	ostream&                outPrintWriter,
 350            	ostream&                errPrintWriter
 351                );
 352            
 353                //
 354                // The CIM Client reference
 355 mike  1.2      //
 356                CIMClient*    _client;
 357            
 358                //
 359                // The host name. 
 360                //
 361                String        _hostName;
 362            
 363                //
 364                // The name of the user.
 365                //
 366                String        _userName;
 367            
 368                //
 369                // The password of the user. 
 370                //
 371                String        _password;
 372            
 373                //
 374                // The new password of the user. 
 375                //
 376 mike  1.2      String        _newpassword;
 377            
 378                //
 379                // The type of operation specified on the command line. 
 380                //
 381                Uint32        _operationType;
 382            
 383                //
 384                // Flags for command options
 385                //
 386                Boolean       _userNameSet;
 387                Boolean       _passwordSet;
 388                Boolean       _newpasswordSet;
 389            
 390            };
 391            
 392            /**
 393                Constructs a CIMUserCommand and initializes instance variables.
 394            */
 395            CIMUserCommand::CIMUserCommand ()
 396            {
 397 mike  1.2      /**
 398                    Initialize the instance variables.
 399                */
 400                _operationType       = OPERATION_TYPE_UNINITIALIZED;
 401                _userName            = String::EMPTY;
 402                _password            = String::EMPTY;
 403                _newpassword         = String::EMPTY;
 404                _hostName            = String::EMPTY;
 405                _passwordSet         = false;
 406                _newpasswordSet      = false;
 407                _userNameSet         = false;
 408            
 409                /**
 410                    Build the usage string for the config command.  
 411                */
 412 kumpf 1.12     String usage;
 413 kumpf 1.18     usage.reserveCapacity(200);
 414 kumpf 1.12     usage.append(USAGE);
 415                usage.append(COMMAND_NAME);
 416            
 417                usage.append(" -").append(OPTION_ADD);
 418                usage.append(" -").append(OPTION_USER_NAME).append(" username");
 419                usage.append(" [ -").append(OPTION_PASSWORD).append(" password")
 420                     .append(" ] \n");
 421            
 422                usage.append("               -").append(OPTION_MODIFY);
 423                usage.append(" -").append(OPTION_USER_NAME).append(" username");
 424                usage.append(" [ -").append(OPTION_PASSWORD).append(" old password")
 425                     .append(" ]");
 426                usage.append(" [ -").append(OPTION_NEW_PASSWORD).append(" new password")
 427                     .append(" ] \n");
 428 mike  1.2  
 429 kumpf 1.12     usage.append("               -").append(OPTION_REMOVE);
 430                usage.append(" -").append(OPTION_USER_NAME).append(" username \n");
 431 mike  1.2  
 432 kumpf 1.12     usage.append("               -").append(OPTION_LIST).append(" \n");
 433 mike  1.2  
 434                setUsage (usage);
 435            }
 436            
 437            
 438            /**
 439                Parses the command line, validates the options, and sets instance 
 440                variables based on the option arguments.
 441            */
 442            void CIMUserCommand::setCommand (
 443                                    ostream& outPrintWriter, 
 444                                    ostream& errPrintWriter,
 445            			Uint32   argc, 
 446            			char*    argv []) 
 447            {
 448                Uint32            i                = 0;
 449                Uint32            c                = 0;
 450                String            badOptionString  = String ();
 451                String            optString        = String ();
 452            
 453                //
 454 mike  1.2      //  Construct optString
 455                //
 456 kumpf 1.12     optString.append(OPTION_ADD);
 457                optString.append(OPTION_USER_NAME);
 458                optString.append(getoopt::GETOPT_ARGUMENT_DESIGNATOR);
 459                optString.append(OPTION_PASSWORD);
 460                optString.append(getoopt::GETOPT_ARGUMENT_DESIGNATOR);
 461                optString.append(OPTION_MODIFY);
 462                optString.append(OPTION_USER_NAME);
 463                optString.append(getoopt::GETOPT_ARGUMENT_DESIGNATOR);
 464                optString.append(OPTION_PASSWORD);
 465                optString.append(getoopt::GETOPT_ARGUMENT_DESIGNATOR);
 466                optString.append(OPTION_NEW_PASSWORD);
 467                optString.append(getoopt::GETOPT_ARGUMENT_DESIGNATOR);
 468                optString.append(OPTION_REMOVE);
 469                optString.append(OPTION_USER_NAME);
 470                optString.append(getoopt::GETOPT_ARGUMENT_DESIGNATOR);
 471                optString.append(OPTION_LIST);
 472 mike  1.2  
 473                //
 474                //  Initialize and parse options
 475                //
 476                getoopt options ("");
 477                options.addFlagspec(optString);
 478            
 479                options.parse (argc, argv);
 480            
 481                if (options.hasErrors ())
 482                {
 483                    CommandFormatException e (options.getErrorStrings () [0]);
 484                    throw e;
 485                }
 486                _operationType = OPERATION_TYPE_UNINITIALIZED;
 487            
 488                //
 489                //  Get options and arguments from the command line
 490                //
 491                for (i =  options.first (); i <  options.last (); i++)
 492                {
 493 mike  1.2          if (options [i].getType () == Optarg::LONGFLAG)
 494                    {
 495                        //
 496                        //  This path should not be hit
 497                        //  The cimuser command has no LONGFLAG options
 498                        //
 499                        c = options [i].getopt () [0];
 500            
 501                        UnexpectedOptionException e (c);
 502                        throw e;
 503                    } 
 504                    else if (options [i].getType () == Optarg::REGULAR)
 505                    {
 506                        //
 507                        //  The cimuser command has no non-option argument options
 508                        //
 509                        UnexpectedArgumentException e (options [i].Value ()); 
 510                        throw e;
 511                    } 
 512                    else /* if (options [i].getType () == Optarg::FLAG) */
 513                    {
 514 mike  1.2  
 515                        c = options [i].getopt () [0];
 516            
 517                        switch (c) 
 518                        {
 519                            case OPTION_ADD: 
 520                            {
 521                                if (_operationType != OPERATION_TYPE_UNINITIALIZED)
 522                                {
 523                                    //
 524                                    // More than one operation option was found
 525                                    //
 526                                    UnexpectedOptionException e (OPTION_ADD);
 527                                    throw e;
 528                                }
 529            
 530                                if (options.isSet (OPTION_ADD) > 1)
 531                                {
 532                                    //
 533                                    // More than one add user option was found
 534                                    //
 535 mike  1.2                          DuplicateOptionException e (OPTION_ADD); 
 536                                    throw e;
 537                                }
 538            
 539                                _operationType = OPERATION_TYPE_ADD;
 540            
 541                                break;
 542                            }
 543                            case OPTION_MODIFY:
 544                            {
 545                                if (_operationType != OPERATION_TYPE_UNINITIALIZED)
 546                                {
 547                                    //
 548                                    // More than one operation option was found
 549                                    //
 550                                    UnexpectedOptionException e (OPTION_MODIFY);
 551                                    throw e;
 552                                }
 553            
 554                                if (options.isSet (OPTION_MODIFY) > 1)
 555                                {
 556 mike  1.2                          //
 557                                    // More than one modify user option was found
 558                                    //
 559                                    DuplicateOptionException e (OPTION_MODIFY);
 560                                    throw e;
 561                                }
 562            
 563                                _operationType = OPERATION_TYPE_MODIFY;
 564            
 565                                break;
 566                            }
 567                            case OPTION_USER_NAME:
 568                            {
 569                                if (options.isSet (OPTION_USER_NAME) > 1)
 570                                {
 571                                    //
 572                                    // More than one username option was found
 573                                    //
 574                                    DuplicateOptionException e (OPTION_USER_NAME);
 575                                    throw e;
 576                                }
 577 mike  1.2  
 578                                _userName = options [i].Value ();
 579            
 580                                _userNameSet = true; 
 581            
 582                                break;
 583                            }
 584                            case OPTION_PASSWORD:
 585                            {
 586                                if (options.isSet (OPTION_PASSWORD) > 1)
 587                                {
 588                                    //
 589                                    // More than one password option was found
 590                                    //
 591                                    DuplicateOptionException e (OPTION_PASSWORD);
 592                                    throw e;
 593                                }
 594            
 595                                String password = options [i].Value ();
 596                                _password = password.subString(0,8);
 597            
 598 mike  1.2                      _passwordSet = true; 
 599            
 600                                break;
 601                            }
 602                            case OPTION_NEW_PASSWORD:
 603                            {
 604                                if (options.isSet (OPTION_NEW_PASSWORD) > 1)
 605                                {
 606                                    //
 607                                    // More than one new password option was found
 608                                    //
 609                                    DuplicateOptionException e (OPTION_NEW_PASSWORD);
 610                                    throw e;
 611                                }
 612            
 613                                String newpassword = options [i].Value ();
 614                                _newpassword = newpassword.subString(0,8);
 615            
 616                                _newpasswordSet = true; 
 617            
 618                                break;
 619 mike  1.2                  }
 620                            case OPTION_REMOVE: 
 621                            {
 622                                if (_operationType != OPERATION_TYPE_UNINITIALIZED)
 623                                {
 624                                    //
 625                                    // More than one operation option was found
 626                                    //
 627                                    UnexpectedOptionException e (OPTION_REMOVE);
 628                                    throw e;
 629                                }
 630            
 631                                if (options.isSet (OPTION_REMOVE) > 1)
 632                                {
 633                                    //
 634                                    // More than one remove user option was found
 635                                    //
 636                                    DuplicateOptionException e (OPTION_REMOVE); 
 637                                    throw e;
 638                                }
 639            
 640 mike  1.2                      _operationType = OPERATION_TYPE_REMOVE;
 641            
 642                                break;
 643                            }
 644            
 645                            case OPTION_LIST: 
 646                            {
 647                                if (_operationType != OPERATION_TYPE_UNINITIALIZED)
 648                                {
 649                                    //
 650                                    // More than one operation option was found
 651                                    //
 652                                    UnexpectedOptionException e (OPTION_LIST);
 653                                    throw e;
 654                                }
 655            
 656                                if (options.isSet (OPTION_LIST) > 1)
 657                                {
 658                                    //
 659                                    // More than one list option was found
 660                                    //
 661 mike  1.2                          DuplicateOptionException e (OPTION_LIST); 
 662                                    throw e;
 663                                }
 664                                _operationType = OPERATION_TYPE_LIST;
 665                                break;
 666                            }
 667            
 668                            default:
 669            		{ 
 670            		    // 
 671            		    // Should never get here
 672            		    //
 673            		    break;
 674                            }
 675                        }
 676                    }
 677                }
 678            
 679                // 
 680                // Some more validations
 681                //
 682 kumpf 1.3      if ( _operationType == OPERATION_TYPE_UNINITIALIZED )
 683 mike  1.2      {
 684 kumpf 1.3          //
 685                    // No operation type was specified 
 686                    // Show the usage 
 687                    //
 688 mike  1.2          CommandFormatException e ( REQUIRED_ARGS_MISSING );
 689                    throw e;
 690                }
 691            	
 692                if ( _operationType == OPERATION_TYPE_LIST  && 
 693            	 ( _userNameSet || _passwordSet || _newpasswordSet ) )
 694                {
 695 kumpf 1.3          CommandFormatException e("Unexpected Option.");	
 696 mike  1.2          throw e;
 697                }
 698            	
 699                if (_operationType == OPERATION_TYPE_ADD)
 700                {
 701            	if ( _newpasswordSet )
 702            	{
 703                        //
 704                        // An invalid option was encountered
 705                        //
 706 kumpf 1.3              UnexpectedOptionException e( OPTION_NEW_PASSWORD );
 707 mike  1.2              throw e;
 708                    }
 709                    if ( !_userNameSet )
 710                    {
 711                        //
 712                        // An invalid option was encountered
 713                        //
 714 kumpf 1.3              MissingOptionException e (OPTION_USER_NAME);
 715 mike  1.2              throw e;
 716                    }
 717                    if ( !_passwordSet )
 718                    {
 719                        //
 720                        // Password is not set, prompt for the password
 721                        //
 722            	    String pw = String::EMPTY;
 723                        do
 724                        {
 725                            pw = System::getPassword( PASSWORD_PROMPT );
 726            
 727            		if ( pw == String::EMPTY || pw == "" )
 728            		{
 729            		    errPrintWriter << PASSWORD_BLANK << endl;
 730            	            pw = String::EMPTY;
 731            		    continue;
 732                            }
 733                            if ( pw != System::getPassword( RE_ENTER_PROMPT ))
 734                            {
 735            	            errPrintWriter << PASSWORD_DOES_NOT_MATCH << endl;
 736 mike  1.2  	            pw = String::EMPTY;
 737                            }
 738                        }
 739                        while ( pw == String::EMPTY );
 740            
 741                        _password = pw ;
 742                    }
 743                }
 744            
 745                if (_operationType == OPERATION_TYPE_MODIFY)
 746                {
 747                    if ( !_userNameSet )
 748                    {
 749                        //
 750                        // An invalid option was encountered
 751                        //
 752 kumpf 1.3              MissingOptionException e (OPTION_USER_NAME);
 753 mike  1.2              throw e;
 754                    }
 755            	if ( _passwordSet && _newpasswordSet )
 756            	{
 757            	    if ( _newpassword == _password )
 758            	    {
 759            		cerr << PASSWORD_SAME_ERROR << endl;
 760            		exit (1);
 761                        }
 762                    }
 763                    if ( !_passwordSet )
 764                    {
 765                        //
 766                        // Password is not set, prompt for the old password once
 767                        //
 768            	    String pw = String::EMPTY;
 769            	    do
 770            	    {
 771                            pw = System::getPassword( OLD_PASSWORD_PROMPT );
 772            		if ( pw == String::EMPTY || pw == "" )
 773            		{
 774 mike  1.2  		    errPrintWriter << PASSWORD_BLANK << endl;
 775            	            pw = String::EMPTY;
 776            		    continue;
 777                            }
 778                        }
 779            	    while ( pw == String::EMPTY );
 780                        _password = pw ;
 781                    }
 782                    if ( !_newpasswordSet )
 783                    {
 784                        //
 785                        // Password is not set, prompt for the new password twice
 786                        //
 787            	    String newPw = String::EMPTY;
 788                        do
 789                        {
 790                            newPw = System::getPassword( NEW_PASSWORD_PROMPT );
 791            		if ( newPw == String::EMPTY || newPw == "" )
 792            		{
 793            		    errPrintWriter << PASSWORD_BLANK << endl;
 794            	            newPw = String::EMPTY;
 795 mike  1.2  		    continue;
 796                            }
 797            
 798                            if ( newPw != System::getPassword( RE_ENTER_PROMPT ))
 799                            {
 800            	            errPrintWriter << PASSWORD_DOES_NOT_MATCH << endl;
 801            	            newPw = String::EMPTY;
 802                            }
 803                        }
 804                        while ( newPw == String::EMPTY );
 805                        _newpassword = newPw ;
 806            	    if ( _newpassword == _password )
 807            	    {
 808            		cerr << PASSWORD_SAME_ERROR << endl;
 809                            exit (-1);
 810                        }
 811                    }
 812                }
 813            
 814                if (_operationType == OPERATION_TYPE_REMOVE)
 815                {
 816 mike  1.2          if ( !_userNameSet )
 817                    {
 818                        //
 819                        // An invalid option was encountered
 820                        //
 821 kumpf 1.3              MissingOptionException e (OPTION_USER_NAME);
 822 mike  1.2              throw e;
 823                    }
 824                    if ( _passwordSet )
 825                    {
 826                        //
 827                        // An invalid option was encountered
 828                        //
 829 kumpf 1.3              UnexpectedOptionException e ( OPTION_PASSWORD );
 830                        throw e;
 831                    }
 832                    if ( _newpasswordSet )
 833                    {
 834                        //
 835                        // An invalid option was encountered
 836                        //
 837                        UnexpectedOptionException e ( OPTION_NEW_PASSWORD );
 838 mike  1.2              throw e;
 839                    }
 840                }
 841            }
 842            
 843            /**
 844                Executes the command and writes the results to the PrintWriters.
 845            */
 846            Uint32 CIMUserCommand::execute (
 847                ostream& outPrintWriter, 
 848                ostream& errPrintWriter)
 849            {
 850                if ( _operationType == OPERATION_TYPE_UNINITIALIZED )
 851                {
 852                    //
 853                    // The command was not initialized
 854                    //
 855                    return 1;
 856                }
 857            
 858                // 
 859 mike  1.2      // Get local host name
 860                //
 861                _hostName.assign(System::getHostName());
 862            
 863                try
 864                {
 865                    //
 866                    // Open connection with CIMSever
 867                    //
 868 kumpf 1.7          _client = new CIMClient;
 869 mike  1.2  
 870 kumpf 1.6          _client->connectLocal();
 871 mike  1.2      }
 872 kumpf 1.19     catch(Exception& e)
 873 mike  1.2      {
 874                    outPrintWriter << CIMOM_NOT_RUNNING << endl;
 875 kumpf 1.3  	return 1;
 876 mike  1.2      }
 877            
 878                //
 879                // Perform the requested operation
 880                //
 881                switch ( _operationType )
 882                {
 883                    case OPERATION_TYPE_ADD:
 884                        try
 885                        {
 886 kumpf 1.4                  _AddUser( outPrintWriter, errPrintWriter );
 887 mike  1.2              }
 888 kumpf 1.19             catch (CIMException& e)
 889 mike  1.2              {
 890                            CIMStatusCode code = e.getCode();
 891            
 892                            if (code == CIM_ERR_FAILED)
 893                            {
 894            		    outPrintWriter << ADD_USER_FAILURE << endl;
 895                                errPrintWriter << e.getMessage() << endl;
 896                            }
 897            		else if (code == CIM_ERR_NOT_SUPPORTED)
 898            		{
 899            		    outPrintWriter << ADD_USER_FAILURE << endl;
 900            		    errPrintWriter << e.getMessage()  << endl;
 901                            }
 902            		else if (code == CIM_ERR_ALREADY_EXISTS)
 903            		{
 904            		    outPrintWriter << ADD_USER_FAILURE << endl;
 905            		    outPrintWriter << USER_ALREADY_EXISTS << endl;
 906            		    errPrintWriter << e.getMessage()  << endl;
 907            		}    
 908                            else if (code == CIM_ERR_INVALID_CLASS)
 909                            {
 910 mike  1.2  		    outPrintWriter << ADD_USER_FAILURE << endl;
 911            		    outPrintWriter << AUTH_SCHEMA_NOT_LOADED << endl;
 912                            }
 913                            else
 914                            {
 915            		    errPrintWriter << e.getMessage() << endl;
 916                            }
 917                            return ( RC_ERROR );
 918                        }
 919 kumpf 1.19             catch (Exception& e)
 920 kumpf 1.4              {
 921                            outPrintWriter << ADD_USER_FAILURE << endl <<
 922 kumpf 1.10                     e.getMessage() << endl;
 923 kumpf 1.4                  return ( RC_ERROR );
 924                        }
 925 mike  1.2              break; 
 926            
 927                        case OPERATION_TYPE_MODIFY:
 928                        try
 929                        {
 930 kumpf 1.4                  _ModifyUser( outPrintWriter, errPrintWriter );
 931 mike  1.2              }
 932 kumpf 1.19             catch (CIMException& e)
 933 mike  1.2              {
 934                            CIMStatusCode code = e.getCode();
 935                            if (code == CIM_ERR_FAILED)
 936                            {
 937                                outPrintWriter << CHANGE_PASSWORD_FAILURE << endl;
 938                                errPrintWriter << e.getMessage() << endl;
 939                            }
 940                            else if (code == CIM_ERR_NOT_SUPPORTED)
 941                            {
 942                                outPrintWriter << CHANGE_PASSWORD_FAILURE << endl;
 943                                errPrintWriter << e.getMessage()  << endl;
 944                            }
 945                            else if (code == CIM_ERR_NOT_FOUND)
 946                            {
 947                                outPrintWriter << CHANGE_PASSWORD_FAILURE << endl;
 948            		    outPrintWriter << USER_NOT_FOUND          << endl;
 949                                errPrintWriter << e.getMessage()  << endl;
 950                            }
 951                            else if (code == CIM_ERR_INVALID_CLASS)
 952                            {
 953                                outPrintWriter << CHANGE_PASSWORD_FAILURE << endl;
 954 mike  1.2                      outPrintWriter << AUTH_SCHEMA_NOT_LOADED << endl;
 955                            }
 956                            else
 957                            {
 958                                errPrintWriter << e.getMessage() << endl;
 959                            }
 960                            return ( RC_ERROR );
 961                        }
 962 kumpf 1.19             catch (Exception& e)
 963 kumpf 1.4              {
 964                            outPrintWriter << CHANGE_PASSWORD_FAILURE << endl <<
 965 kumpf 1.10                     e.getMessage() << endl;
 966 kumpf 1.4                  return ( RC_ERROR );
 967                        }
 968 mike  1.2              break;
 969            
 970                    case OPERATION_TYPE_REMOVE:
 971                        try
 972                        {
 973 kumpf 1.4                  _RemoveUser( outPrintWriter, errPrintWriter );
 974 mike  1.2              }
 975 kumpf 1.19             catch (CIMException& e)
 976 mike  1.2              {
 977                            CIMStatusCode code = e.getCode();
 978                            if (code == CIM_ERR_FAILED)
 979                            {
 980                                outPrintWriter << REMOVE_USER_FAILURE << endl;
 981                                errPrintWriter << e.getMessage() << endl;
 982                            }
 983                            else if (code == CIM_ERR_NOT_SUPPORTED)
 984                            {
 985                                outPrintWriter << REMOVE_USER_FAILURE << endl;
 986                                errPrintWriter << e.getMessage()  << endl;
 987                            }
 988                            else if (code == CIM_ERR_NOT_FOUND)
 989                            {
 990                                outPrintWriter << REMOVE_USER_FAILURE << endl;
 991            		    outPrintWriter << USER_NOT_FOUND          << endl;
 992                                errPrintWriter << e.getMessage()  << endl;
 993                            }
 994                            else if (code == CIM_ERR_INVALID_CLASS)
 995                            {
 996                                outPrintWriter << REMOVE_USER_FAILURE << endl;
 997 mike  1.2                      outPrintWriter << AUTH_SCHEMA_NOT_LOADED << endl;
 998                            }
 999                            else
1000                            {
1001                                errPrintWriter << e.getMessage() << endl;
1002                            }
1003                            return ( RC_ERROR );
1004                        }
1005 kumpf 1.19             catch (Exception& e)
1006 kumpf 1.4              {
1007                            outPrintWriter << REMOVE_USER_FAILURE << endl <<
1008 kumpf 1.10                     e.getMessage() << endl;
1009 kumpf 1.4                  return ( RC_ERROR );
1010                        }
1011 mike  1.2              break;
1012            
1013                    case OPERATION_TYPE_LIST:
1014                        try
1015                        {
1016 kumpf 1.4                  _ListUsers(outPrintWriter, errPrintWriter);
1017 mike  1.2              }
1018 kumpf 1.19             catch (CIMException& e)
1019 mike  1.2              {
1020                            CIMStatusCode code = e.getCode();
1021                            if (code == CIM_ERR_FAILED)
1022                            {
1023                                outPrintWriter << LIST_USERS_FAILURE << endl;
1024                                errPrintWriter << e.getMessage() << endl;
1025                            }
1026                            else if (code == CIM_ERR_NOT_SUPPORTED)
1027                            {
1028                                outPrintWriter << LIST_USERS_FAILURE << endl;
1029                                errPrintWriter << e.getMessage()  << endl;
1030                            }
1031                            else if (code == CIM_ERR_ALREADY_EXISTS)
1032                            {
1033                                outPrintWriter << LIST_USERS_FAILURE << endl;
1034                                errPrintWriter << e.getMessage()  << endl;
1035                            }
1036                            else if (code == CIM_ERR_INVALID_CLASS)
1037                            {
1038                                outPrintWriter << LIST_USERS_FAILURE << endl;
1039                                outPrintWriter << AUTH_SCHEMA_NOT_LOADED << endl;
1040 mike  1.2                  }
1041                            else
1042                            {
1043                                errPrintWriter << e.getMessage() << endl;
1044                            }
1045                            return ( RC_ERROR );
1046                        }
1047 kumpf 1.19             catch (Exception& e)
1048 mike  1.2              {
1049 kumpf 1.4                  outPrintWriter << LIST_USERS_FAILURE << endl <<
1050 kumpf 1.10                     e.getMessage() << endl;
1051 mike  1.2                  return ( RC_ERROR );
1052                        }
1053 kumpf 1.4              break;
1054 mike  1.2  
1055                    default:
1056                        //
1057                        // Should never get here
1058                        //
1059                        break;
1060                }
1061            
1062                return (RC_SUCCESS);
1063            }
1064            
1065            /**
1066                Add the user to the CIM Server.
1067            */
1068            void CIMUserCommand::_AddUser
1069                (
1070                ostream&    outPrintWriter, 
1071                ostream&    errPrintWriter
1072                ) 
1073            {
1074                CIMProperty prop;
1075 mike  1.2      
1076                try
1077                {
1078 kumpf 1.17         CIMInstance newInstance( PEGASUS_CLASSNAME_USER );
1079 mike  1.2  	newInstance.addProperty ( 
1080            			  CIMProperty( PROPERTY_NAME_USER_NAME, _userName ) );
1081            	newInstance.addProperty ( 
1082            			  CIMProperty( PROPERTY_NAME_PASSWORD , _password ) );
1083            
1084 kumpf 1.17 	_client->createInstance( PEGASUS_NAMESPACENAME_USER, newInstance );
1085 mike  1.2  	outPrintWriter << ADD_USER_SUCCESS << endl;
1086            
1087                }
1088 kumpf 1.19     catch (Exception& e)
1089 mike  1.2      {
1090                    throw e;
1091                }
1092            }
1093            
1094            //
1095            // Modify the password for a user
1096            //
1097            void CIMUserCommand::_ModifyUser
1098                (
1099                ostream&    outPrintWriter, 
1100                ostream&    errPrintWriter
1101                ) 
1102            {
1103                try
1104                {
1105 kumpf 1.20         Array<CIMKeyBinding>      kbArray;
1106                    CIMKeyBinding             kb;
1107 mike  1.2  	Array<CIMParamValue>   inParams;
1108            	Array<CIMParamValue>   outParams;
1109            
1110            	//
1111            	// Build the input params
1112            	//
1113 kumpf 1.5  	inParams.append ( CIMParamValue ( OLD_PASS_PARAM,
1114                                                      CIMValue ( _password )));
1115            	inParams.append ( CIMParamValue ( NEW_PASS_PARAM, 
1116                                                      CIMValue ( _newpassword )));
1117 mike  1.2  
1118 kumpf 1.21         kb.setName(CIMName ("Username"));
1119 mike  1.2          kb.setValue(_userName);
1120 kumpf 1.20         kb.setType(CIMKeyBinding::STRING);
1121 mike  1.2  
1122                    kbArray.append(kb);
1123            
1124 kumpf 1.13         CIMObjectPath reference(
1125 kumpf 1.17             _hostName, PEGASUS_NAMESPACENAME_USER,
1126                        PEGASUS_CLASSNAME_USER, kbArray);
1127 mike  1.2  
1128            	//
1129            	// Call the invokeMethod with the input parameters
1130            	// 
1131 kumpf 1.3  
1132            	//
1133            	// Not checking for return code as all error conditions will
1134            	// throw exceptions and will be handled by the catch block. If new 
1135            	// return codes are added in future, they need to be handled here.  
1136            	//
1137            	CIMValue retValue = _client->invokeMethod (
1138 kumpf 1.17 		                       PEGASUS_NAMESPACENAME_USER,
1139 mike  1.2  		                       reference,
1140            		                       MODIFY_METHOD,
1141            		                       inParams,
1142            		                       outParams );
1143 kumpf 1.3  
1144 mike  1.2          outPrintWriter << CHANGE_PASSWORD_SUCCESS << endl;
1145                }
1146 kumpf 1.19     catch (Exception& e)
1147 mike  1.2      {
1148                    throw e;
1149                }
1150            }
1151            
1152            //
1153            // Remove a user    
1154            //
1155            void CIMUserCommand::_RemoveUser
1156                (
1157                ostream&    outPrintWriter, 
1158                ostream&    errPrintWriter
1159                ) 
1160            {
1161                try
1162                {
1163 kumpf 1.20         Array<CIMKeyBinding> kbArray;
1164                    CIMKeyBinding        kb;
1165 mike  1.2  
1166                    kb.setName(PROPERTY_NAME_USER_NAME);
1167                    kb.setValue(_userName);
1168 kumpf 1.20         kb.setType(CIMKeyBinding::STRING);
1169 mike  1.2  
1170                    kbArray.append(kb);
1171            
1172 kumpf 1.13         CIMObjectPath reference(
1173 kumpf 1.17             _hostName, PEGASUS_NAMESPACENAME_USER,
1174                        PEGASUS_CLASSNAME_USER, kbArray);
1175 mike  1.2  
1176 kumpf 1.17         _client->deleteInstance(PEGASUS_NAMESPACENAME_USER, reference);
1177 mike  1.2  	outPrintWriter << REMOVE_USER_SUCCESS << endl;
1178            
1179                }
1180 kumpf 1.19     catch (Exception& e)
1181 mike  1.2      {
1182            	throw e;
1183                }
1184            }
1185            
1186            
1187            /**
1188                get a list of all user names from the CIM Server.
1189             */
1190            void CIMUserCommand::_ListUsers
1191                ( 
1192                ostream&    outPrintWriter,
1193                ostream&    errPrintWriter
1194                )
1195            {
1196                try
1197                {
1198                    //
1199                    // get all the instances of class PG_User
1200                    //
1201 kumpf 1.13         Array<CIMObjectPath> instanceNames =
1202 kumpf 1.3              _client->enumerateInstanceNames(
1203 kumpf 1.17 	        PEGASUS_NAMESPACENAME_USER, 
1204            	        PEGASUS_CLASSNAME_USER);
1205 mike  1.2  
1206                    if ( instanceNames.size() == 0 )
1207                    {
1208                         outPrintWriter << NO_USERS_FOUND << endl;
1209                    }
1210                    else
1211            	{
1212                        //
1213                        // List all the users.
1214                        //
1215                        for (Uint32 i = 0; i < instanceNames.size(); i++)
1216                        {
1217 kumpf 1.20                 Array<CIMKeyBinding> kbArray = instanceNames[i].getKeyBindings();
1218 mike  1.2  
1219                            if (kbArray.size() > 0)
1220                            {
1221            	            outPrintWriter << kbArray[0].getValue() << endl;
1222                            }
1223                        }
1224                    }
1225                }
1226 kumpf 1.19     catch (Exception& e)
1227 mike  1.2      {
1228                    throw e;
1229                }
1230            }
1231            
1232            PEGASUS_NAMESPACE_END
1233            
1234            //
1235            // exclude main from the Pegasus Namespace
1236            //
1237            PEGASUS_USING_PEGASUS;
1238            
1239            PEGASUS_USING_STD;
1240            
1241            ///////////////////////////////////////////////////////////////////////////////
1242            /**
1243                Parses the command line, and execute the command.
1244            
1245                @param   args  the string array containing the command line arguments
1246            */
1247            ///////////////////////////////////////////////////////////////////////////////
1248 mike  1.2  
1249            int main (int argc, char* argv []) 
1250            {
1251                CIMUserCommand*      command;
1252                Uint32               retCode;
1253            
1254                //
1255                // Check if root is issuing the command
1256                //
1257 kumpf 1.14     if ( !System::isPrivilegedUser(System::getEffectiveUserName()) )
1258 mike  1.2      {
1259            	cerr << NOT_PRIVILEGED_USER << endl;
1260            	return 1;
1261                }
1262            	 
1263                command  = new CIMUserCommand ();
1264            
1265                try 
1266                {
1267                    command->setCommand ( cout, cerr, argc, argv);
1268                } 
1269                catch (CommandFormatException& cfe) 
1270                {
1271                    if (!String::equal(cfe.getMessage (), ""))
1272                    {
1273                        cerr << COMMAND_NAME << ": " << cfe.getMessage () << endl;
1274                    }
1275                    cerr << command->getUsage () << endl;
1276                    return 1;
1277                }
1278            
1279 mike  1.2      retCode = command->execute (cout, cerr);
1280            
1281                return (retCode);
1282            }
1283            

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2