home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / dn511.exe / OBJECT.CPP < prev    next >
C/C++ Source or Header  |  1995-01-12  |  4KB  |  154 lines

  1. /*
  2. ** OBJECT <common name> <last name> [context]
  3. **
  4. **    Create and read NDS user objects. This program will create a
  5. **    new NDS user object if the user does not exist. Otherwise the
  6. **    program will modify the user's language list.
  7. **
  8. **    Author: John Buckle, Asia Pacific Support Centre, Novell Australia.
  9. */
  10.  
  11. # define DSCPP_IOSTREAM
  12.  
  13. # include "dslocale.h"
  14. # include "dsobject.h"
  15. # include "dsvchar.h"
  16. # include "dsvint.h"
  17. # include "dsvmulti.h"
  18.  
  19. # include <string.h>
  20. # include <stdio.h>
  21.  
  22. /*
  23. ** dsUserAttrNames[]        Null terminated list of attribute names present
  24. **                in the DSUserObject. The order of these names
  25. **                is important and must be the same as used by
  26. **                the function FindAttribute().
  27. */
  28.  
  29. NWPSTR    dsUserAttrNames[] = { "Surname",
  30.                   "Full Name",
  31.                   "Security Equals",
  32.                   "Language", 0 } ;
  33.  
  34. /*
  35. ** dsUserAttrCount[]        Number of attribute fields in the DSUserObject
  36. **                for each attribute listed in dsUserAttrNames[].
  37. */
  38.  
  39. WORD    dsUserAttrCount[] = { 1, 1, 1, 1, 0 } ;
  40.  
  41. /*
  42. ** Class DSUserObject
  43. **
  44. **    Used to create and read USER objects from the NDS.
  45. */
  46.  
  47. class DSUserObject : public DSObject
  48. {
  49. protected:
  50.     DSValue *        FindAttribute(WORD attrIndex, WORD valueIndex) ;
  51.  
  52.     NWPSTR *        AttributeNames()
  53.                 { return dsUserAttrNames ; }
  54.     WORD *        AttributeCount()
  55.                 { return dsUserAttrCount ; }
  56.     NWPSTR        ClassName()
  57.                 { return "USER" ; }
  58. public:
  59.     DSVString        SurName ;
  60.     DSVString        FullName ;
  61.     DSVMultiValue    Security ;
  62.     DSVStringList    Language ;
  63.  
  64.             DSUserObject() { ; }
  65. } ;
  66.  
  67. /*
  68. ** DSValue * DSUserObject::FindAttribute(WORD attrIndex, WORD)
  69. **
  70. **    Given an attribute index and a value index, return a pointer to the
  71. **    DSValue object responsible for that attribute/value pair.
  72. */
  73.  
  74. DSValue * DSUserObject::FindAttribute(WORD attrIndex, WORD)
  75. {
  76.     switch (attrIndex) {
  77.         case 0:
  78.         return & SurName ;
  79.         case 1:
  80.         return & FullName ;
  81.         case 2:
  82.         return & Security ;
  83.         case 3:
  84.         return & Language ;
  85.         }
  86.     return 0 ;
  87. }
  88.  
  89. /*
  90. ** int main(int argc, char * argv[])
  91. */
  92.  
  93. int main(int argc, char * argv[])
  94. {
  95.     if (argc < 3 || argc > 4){
  96.         cerr << "Usage: OBJECT common_name last_name [context]\n" ;
  97.         return 0 ;
  98.         }
  99.     DSLocale    dsLocale ;
  100.     DSBuffer    dsBuffer ;
  101.     DSUserObject    newUser, oldUser ;
  102.     char        fullname[MAX_DN_BYTES],
  103.             distname[MAX_DN_BYTES],
  104.             context [MAX_DN_BYTES],
  105.             everyone[MAX_DN_BYTES] ;
  106.  
  107.     if (dsLocale.status() || dsBuffer.status()){
  108.         cerr << "OBJECT: Cannot initialise Unicode\n" ;
  109.         return 0 ;
  110.         }
  111.  
  112.     if (argc == 3)
  113.         dsBuffer.GetContext(context) ;
  114.     else
  115.         strcpy(context,argv[3]) ;
  116.  
  117.     sprintf(fullname,"%s %s",argv[1],argv[2]) ;
  118.     sprintf(distname,".%s.%s",argv[1],context) ;
  119.     sprintf(everyone,"Everyone.%s",context) ;
  120.  
  121.     if (dsBuffer.DoesObjectExist(distname) == 0){
  122.         if (newUser.ReadObject(distname,& dsBuffer)){
  123.             cerr << "Cannot read user \"" << distname << "\"\n" ;
  124.             return 0 ;
  125.             }
  126.         oldUser = newUser ;
  127.         cout << "\nOld user value\n" << oldUser ;
  128.         newUser.Language.release() ;
  129.         newUser.Language << String("English") << String("C++")
  130.                  << String("Pascal")  << String("Fortran") ;
  131.         if (newUser.WriteObject(distname,& dsBuffer,& oldUser)){
  132.             cerr << "Cannot modify user \"" << distname << "\"\n" ;
  133.             return 0 ;
  134.             }
  135.         cout << "\nNew user value\n" << newUser ;
  136.         }
  137.     else {
  138.         newUser.SurName  << String(argv[2])  << Syntax(SYN_CI_STRING) ;
  139.         newUser.FullName << String(fullname) << Syntax(SYN_CI_STRING) ;
  140.         newUser.Security << String(everyone) << Syntax(SYN_DIST_NAME) ;
  141.         newUser.Language << String("English") ;
  142.         if (newUser.WriteObject(distname,& dsBuffer)){
  143.             cerr << "Cannot create user \"" << distname << "\"\n" ;
  144.             return 0 ;
  145.             }
  146.         cout << "\nNew user value\n" << newUser ;
  147.         if (dsBuffer.GenerateObjectKeyPair(distname,"")){
  148.             cerr << "Cannot generate user's RSA keys \n" ;
  149.             return 0 ;
  150.             }
  151.         }
  152.  
  153.     return 0 ;
  154. }