home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / xtru1.exe / TRUST.C next >
Text File  |  1994-09-08  |  11KB  |  369 lines

  1. /****************************************************************************
  2. **    File:    XTRU1.C
  3. **
  4. **    Desc: Sample code to add a trustee to a directory in the NetWare 4.x env.
  5. **
  6. **    Disclaimer:
  7. **
  8. **        Novell, Inc. makes no representations or warranties with respect to
  9. **        any NetWare software, and specifically disclaims any express or
  10. **        implied warranties of merchantability, title, or fitness for a
  11. **        particular purpose.  
  12. **
  13. **        Distribution of any NetWare software is forbidden without the
  14. **        express written consent of Novell, Inc.  Further, Novell reserves
  15. **        the right to discontinue distribution of any NetWare software.
  16. **
  17. **        Novell is not responsible for lost profits or revenue, loss of use
  18. **        of the software, loss of data, costs of re-creating lost data, the
  19. **        cost of any substitute equipment or program, or claims by any party
  20. **        other than you.  Novell strongly recommends a backup be made before
  21. **        any software is installed.   Technical support for this software
  22. **        may be provided at the discretion of Novell.
  23. **
  24. **
  25. **    QMK386 options used:
  26. **
  27. **        /id                    NDS (Directory Services) symbols.
  28. **
  29. **    Programmers:
  30. **
  31. **        Ini    Who                    Firm
  32. **        -----------------------------------------------------------------------
  33. **        ABJ    Adam B. Jerome        Novell Developer Support.
  34. **
  35. **    History:
  36. **
  37. **        When        Who    What
  38. **        -----------------------------------------------------------------------
  39. **        09-06-94    ABJ    First code.
  40. */
  41.  
  42. /****************************************************************************
  43. **    Include headers, function prototypes, macros, etc.
  44. */
  45.     /*------------------------------------------------------------------------
  46.     **    ANSI
  47.     */
  48.     #include <stdio.h>
  49.     #include <stdlib.h>
  50.     #include <process.h>
  51.     #include <string.h>
  52.     #include <conio.h>    /* getch() */
  53.     #include <errno.h>    /* ESUCCESS */
  54.     #include <ctype.h>    /* islower() */
  55.  
  56.     /*------------------------------------------------------------------------
  57.     **    NetWare NLM
  58.     */
  59.     #include <nwdsapi.h>    /* NWDSCreateContext() NWDSAuthenticate()    */    
  60.     #include <nwdsdc.h>    /*    NWDSCreateContext()     */
  61.     #include    <nwdsdsa.h>    /* NWDSAuthenticate()    */
  62.     #include <nwconn.h>    /* GetConnectionNumber()*/
  63.     #include <nwdir.h>    /* AddTrustee()            */
  64.     #include <niterror.h>/*    ERR_NO_MODIFY_PRIVILEGES, ERR_NO_SUCH_BINDERY_OBJECT    */
  65.     #include <nwcntask.h>/* ReturnConnection()    */
  66.     #include <nwmisc.h>    /*    LongSwap()                */
  67.  
  68.     /*------------------------------------------------------------------------
  69.     **    This module.
  70.     */
  71.     #define STREDIT_NOFLAGS    0x00000000    /* Just to be thourough                        */
  72.     #define STREDIT_NEW        0x00000001  /* Zero's out the strBuf before editing.*/
  73.     #define STREDIT_NOECHO    0x00000002    /* Does not echo to the screen.             */
  74.     #define STREDIT_ECHOAS    0x00000004    /* Echos astricts.                             */
  75.     #define STREDIT_UPPER    0x00000008    /* Upper-case alpha.                             */
  76.  
  77.     #define STREDIT_ERR_TOOLONG    (-1)
  78.     #define STREDIT_KEY_ESC            27
  79.     #define STREDIT_KEY_ENTER        13
  80.     #define STREDIT_KEY_BACKSPACE 8
  81.  
  82. /****************************************************************************
  83. **    Edit String function.
  84. **
  85. **    Parameters:
  86. **
  87. **        strBuf            [INPUT/OUTPUT] String to edit.
  88. **
  89. **        maxStrLen        [INPUT] Maximum length of strBuf including ZERO
  90. **                                terminator.
  91. **
  92. **        flags                [INPUT] Editing flags; including:
  93. **
  94. **                                  STREDIT_NEW        Zero's out the strBuf before editing.
  95. **                                STREDIT_NOECHO    Does not echo to the screen.
  96. **                                STREDIT_ECHOAS    Echos astricts.
  97. **                                STREDIT_UPPER    Upper-case only.
  98. **
  99. **        return Value:    STREDIT_ERR_TOOLONG    Initial string is longer than the
  100. **                                                        specified maxStrLen.
  101. **
  102. **                            STREDIT_KEY_ESC        User pressed the ESCAPE key to
  103. **                                                        terminate the edit process.
  104. **
  105. **                            STREDIT_KEY_ENTER        User pressed the ENTER key to
  106. **                                                        terminate the edit process.
  107. */
  108. int StringEdit(char *strBuf, int maxStrLen, int flags, char *prompt)
  109.     {
  110.     int     ch;
  111.  
  112.     /*------------------------------------------------------------------------
  113.     **    New buffer?
  114.     */
  115.     if(flags & STREDIT_NEW) memset(strBuf, 0x00, maxStrLen);
  116.  
  117.     /*------------------------------------------------------------------------
  118.     **    Check args.
  119.     */
  120.     if(strlen(strBuf) > maxStrLen - 1) return(STREDIT_ERR_TOOLONG);
  121.  
  122.     /*------------------------------------------------------------------------
  123.     **    Print the prompt & current strBuf value..
  124.     */
  125.     if(prompt != NULL) printf("%s%s", prompt, strBuf);
  126.  
  127.     /*------------------------------------------------------------------------
  128.     **    Handle keyboard.
  129.     */
  130.     while(1)
  131.         {
  132.         ch=getch(); if(!ch) ch=getch() + 256;
  133.         if((ch < 32) || (ch == 127) || (ch > 255)) /* Control chars & Function Keys    */
  134.             {
  135.             switch(ch)
  136.                 {
  137.                 case STREDIT_KEY_ESC:
  138.                 case STREDIT_KEY_ENTER:
  139.                     printf("\n");
  140.                     return(ch);
  141.  
  142.                 case STREDIT_KEY_BACKSPACE:
  143.                     if(strlen(strBuf) == 0) continue;
  144.                     strBuf[strlen(strBuf)-1] = '\0';
  145.                     printf("\b \b");
  146.                     continue;
  147.                 }
  148.  
  149.             continue;
  150.             }
  151.         else if(ch <= 126)                                /* -Normal- characters */
  152.             {
  153.             if(strlen(strBuf) == (maxStrLen -1)) continue;
  154.             if(flags & STREDIT_UPPER) if(islower(ch)) ch -= 32;
  155.             strBuf[strlen(strBuf)+1] = '\0';
  156.             strBuf[strlen(strBuf)] = (char)ch;
  157.             if(!(flags & STREDIT_NOECHO))
  158.                 {
  159.                 if(flags & STREDIT_ECHOAS) printf("*");
  160.                 else printf("%c", (char)ch);
  161.                 }
  162.  
  163.             continue;
  164.             }
  165.          else continue;                        /* Extended chars from 128 - 255 */
  166.         }
  167.     }
  168.  
  169. /****************************************************************************
  170. **    Program start.
  171. */
  172. void main(void)
  173.     {
  174.     NWDSContextHandle    contextHandle;
  175.     NWDSCCODE            loggedIn;
  176.     NWDSCCODE            attached;
  177.     NWDSCCODE            cCode;
  178.     char                    operatorUserName[47+1];
  179.     char                    operatorPassWord[47+1];
  180.     char                    contextName[255+1];
  181.     char                    targetObjectName[47+1];
  182.     uint32                targetObjectID;
  183.     char                    trusteeServerName[47+1];
  184.     WORD                    trusteeServerID;
  185.     char                    trusteePathName[255+1];
  186.     NWCONN_HANDLE        trusteeConnNumber;
  187.  
  188.       /*------------------------------------------------------------------------
  189.    ** Get a context handle.
  190.    */
  191.     contextHandle = NWDSCreateContext();
  192.     if((signed)contextHandle < 0) switch(contextHandle)
  193.         {
  194.         case ERR_CONTEXT_CREATION:
  195.             printf("ERROR:  NWDSCreateContext() indicated a Context Creation Error.\n");
  196.             goto END;
  197.  
  198.         default:
  199.             printf("ERROR:  NWDSCreateContext() indicated an unknown error: %ld\n", contextHandle);
  200.             goto END;
  201.         }
  202.  
  203.       /*------------------------------------------------------------------------
  204.    ** Get operator's name and password.
  205.    */
  206.     printf("Please authenticate yourself:\n");
  207.     StringEdit(operatorUserName, sizeof(operatorUserName), STREDIT_NEW, "  Enter login name: ");
  208.     StringEdit(operatorPassWord, sizeof(operatorPassWord), STREDIT_NEW|STREDIT_NOECHO, "  Password: ");
  209.     printf("\n");
  210.  
  211.       /*------------------------------------------------------------------------
  212.    ** Authenticate.
  213.    */
  214.    loggedIn = NWDSLogin(
  215.         /* I- context            */    contextHandle,
  216.         /*    I- optionsFlag        */    NULL,
  217.         /*    I- objectName        */    operatorUserName,
  218.         /*    I- password            */    operatorPassWord,
  219.         /*    I- validityPeriod    */ 0
  220.         );
  221.     switch(loggedIn)
  222.         {
  223.         case 0:
  224.             break;
  225.  
  226.         default:
  227.             printf("ERROR:  NWDSLogin() indicated an unknown error: %ld\n", loggedIn);
  228.             goto END;
  229.         }
  230.  
  231.       /*------------------------------------------------------------------------
  232.    **    Get tartet user's name. 
  233.    */
  234.     printf("Target object info:\n");
  235.     StringEdit(contextName, sizeof(contextName), STREDIT_NEW, "  Enter target context name: ");
  236.     StringEdit(targetObjectName, sizeof(targetObjectName), STREDIT_NEW, "  Enter target user's name: ");
  237.     printf("\n");
  238.  
  239.    /*------------------------------------------------------------------------
  240.    ** The server that I am running this on is in the LNC context.
  241.    ** The server also has a replica on it.
  242.    */
  243.    cCode = NWDSSetContext(
  244.         /* I-    context    */    contextHandle,
  245.         /*    I-    key        */    DCK_NAME_CONTEXT,
  246.         /*    I-    value        */    contextName
  247.         );
  248.       if(cCode < 0) switch(cCode)
  249.         {
  250.         default:
  251.             printf("ERROR:  NWDSSetContext() returned unknown error: %d", cCode);
  252.             goto END;
  253.         }
  254.  
  255.       /*------------------------------------------------------------------------
  256.    ** Attach us to the file server on which we will manipulate trustee info.    
  257.    */
  258.     printf("Target path info:\n");
  259.     StringEdit(trusteeServerName, sizeof(trusteeServerName), STREDIT_NEW|STREDIT_UPPER, "  File server name: ");
  260.     attached=AttachToFileServer(
  261.         /* I- fileServerName    */ trusteeServerName,
  262.         /*    I- filsServerID    */    &trusteeServerID
  263.         );
  264.     if(attached) switch(NetWareErrno)
  265.         {
  266.         
  267.         default:
  268.             printf("ERROR:  AttachToFileServer() returned unknown error: %d\n", NetWareErrno);
  269.             goto END;
  270.         }
  271.  
  272.     /*------------------------------------------------------------------------
  273.    **    What's our connection number? 
  274.    */
  275.     trusteeConnNumber = GetConnectionNumber();
  276.  
  277.     /*------------------------------------------------------------------------
  278.    **    Have DS authenticate our connection to the server.
  279.    */
  280.     cCode=NWDSAuthenticate(
  281.         /*    I-    connID            */    trusteeConnNumber,
  282.         /*    I- optionsFlags    */    0,
  283.         /*    I-    sessionKey        */    NULL
  284.         );
  285.     switch(cCode)
  286.         {
  287.         case 0:
  288.             break;
  289.  
  290.         default:
  291.             printf("ERROR:  NWDSAuthenticate() returned error: %d\n");
  292.             goto END;
  293.         }
  294.  
  295.       /*------------------------------------------------------------------------
  296.    ** Get the target object's ID.
  297.    */
  298.     cCode=NWDSMapNameToID(
  299.         /* I-    context    */    contextHandle,
  300.         /*    I- connID    */    trusteeConnNumber,
  301.         /*    I-    object    */    targetObjectName,
  302.         /*    -O    objectID    */    &targetObjectID
  303.         );
  304.       if(cCode < 0) switch(cCode)
  305.         {
  306.         case (-333):
  307.             printf("ERROR:  NWDSMapNameToID() reports \"No Connection.\"\n");
  308.             goto END;
  309.  
  310.         default:
  311.             printf("ERROR:  NWDSMapNameToID() returned unknown error: %d\n", cCode);
  312.             goto END;
  313.         }
  314.  
  315.       /*------------------------------------------------------------------------
  316.    **    Get target directory and/or file path.
  317.    */
  318.     StringEdit(trusteePathName, sizeof(trusteePathName), STREDIT_NEW|STREDIT_UPPER, "  Grant rights to (vol:dir\\...): ");
  319.     printf("\n");
  320.  
  321.       /*------------------------------------------------------------------------
  322.    ** Add trustee to sys:root.
  323.    **
  324.     ** Add trustee, being an older function, requires that the trusteeObjectID
  325.     **    be long-swapped from the newer DS functions.
  326.    */
  327.     cCode=AddTrustee(
  328.         /* I-    pathName                */    trusteePathName,
  329.         /*    I-    trusteeObjectID    */    LongSwap(targetObjectID),
  330.         /*    I-    trusteeRightsMask    */    TA_READ|TA_SEEFILES
  331.         );
  332.     switch(cCode)
  333.         {
  334.         case ESUCCESS:
  335.             break;
  336.  
  337.         case BADSERVERNAME:
  338.             printf("ERROR:  AddTrustee() reports \"Bad server name.\"\n");
  339.             goto END;
  340.  
  341.         case ERR_INVALID_PATH:
  342.             printf("ERROR:  AddTrustee() reports \"Invalid Path.\": [%s]\n", trusteePathName);
  343.             goto END;
  344.  
  345.         case ERR_NO_MODIFY_PRIVILEGE:
  346.             printf("ERROR:  AddTrustee() reports \"No modify privileges.\"\n");
  347.             goto END;
  348.  
  349.         case ERR_NO_SUCH_BINDERY_OBJECT:
  350.             printf("ERROR:  AddTrustee() reports \"No modify privileges.\"\n");
  351.             goto END;
  352.  
  353.         default:
  354.             printf("ERROR:  AddTrustee() returned unknown error: %d\n", cCode);
  355.             goto END;
  356.  
  357.         }
  358.  
  359.     printf("Success\n");
  360.  
  361. END:
  362.     if(attached == 0)    ReturnConnection(trusteeConnNumber);
  363.    if(loggedIn >= 0) NWDSLogout(contextHandle);
  364.     if((signed)contextHandle >= 0)    NWDSFreeContext(contextHandle);
  365.    return;
  366.     }
  367.  
  368.  
  369.