home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / dbsamp.exe / DBAPP / SERVER / DBNLM.C next >
C/C++ Source or Header  |  1995-01-12  |  10KB  |  425 lines

  1. /*------------------------------------------------------------------------
  2. **      Module: DBNLM v1.00  Copyright (c) 1994 NDA TEAM - Novell, Inc.
  3. **
  4. **      Source: NDA Team
  5. **
  6. **      Abstract:
  7. **              
  8. **              This NLM is designed to respond to NCP Extension requests and
  9. **              grant access to a Database object which is passed in the
  10. **              NCP packet. This NLM gets the distinquished name of the user 
  11. **                     requesting access to the database. The DN of the database is
  12. **              passed to NWDSRead for the purpose of verifying that the 
  13. **              user requesting access exists in a "User" attribute of the
  14. **                     database object.
  15. **
  16. **      Environment:
  17. **              4.x
  18. **
  19. **
  20. **------------------------------------------------------------------------
  21. */
  22.  
  23. #pragma warn -stu-
  24. #define  NWNLM
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <malloc.h>
  29. #include <advanced.h>
  30. #include <process.h>
  31. #include <conio.h>
  32. #include <errno.h>
  33. #include <nwenvrn.h>
  34.  
  35. #include <nwdsapi.h>
  36. #include <nwdsbuft.h>
  37. #include <nwalias.h>
  38. #include <nwdssch.h>
  39. #include <nwcntask.h>
  40. #include <nwconn.h>
  41. #include <nwmisc.h>
  42. #include <ncpext.h>
  43. #pragma warn -stu.
  44.  
  45. #include "dbnlm.h"
  46.  
  47. /*      ---------- Function Prototypes ----------  */
  48.  
  49. BYTE AuthServer(NCPExtensionClient *client, void *reqData,
  50.     LONG requestDataLen, void *repData, LONG *replyDataLen);
  51.  
  52. void AuthServerConnEventHandler(LONG connection, LONG eventType);
  53.  
  54. /*int AuthToDS(void);
  55. */
  56. /*      ---------- Global Variables ----------  */
  57.  
  58. int     myThreadGroupID;
  59. void   *queryData;
  60. NWDSContextHandle               context;
  61.  
  62. int main(void)
  63. {
  64.     int             cCode;
  65.     NWDSCCODE dscCode;
  66.     unsigned  conHandle;
  67.     unsigned  flags;
  68.     WORD            fileServerID;
  69.     char serverName[48+1];
  70.     char    loginName[MAX_DN_CHARS];
  71.     char    password[256];
  72.  
  73.     myThreadGroupID = GetThreadGroupID();
  74.     
  75.     context=NWDSCreateContext();
  76.     if(context == ERR_CONTEXT_CREATION)
  77.     {
  78.         printf("Context Creation Failed.  Exiting NLM...\n");
  79.         return 3;
  80.     }
  81.  
  82.   
  83.    cCode = NWDSGetContext(
  84.                 /* Contxt Handle    */ context,
  85.                 /* Key              */ DCK_FLAGS,
  86.                 /* Context Flags    */ &flags
  87.                 );
  88.  
  89.     if (cCode < 0)
  90.         {
  91.         NWDSFreeContext(context);
  92.         printf("NWDSGetContext returned: %04X\n", cCode);
  93.         return 1;
  94.         }
  95.  
  96.     /*-------------------------------------------------------------------
  97.    **   Turn typeless naming on.
  98.    **   Turn canonicalize names off.  This means we will get full names.
  99.    */
  100.  
  101.    flags |= DCV_TYPELESS_NAMES;
  102.    flags &= ~DCV_CANONICALIZE_NAMES;
  103.  
  104.     /*-------------------------------------------------------------------
  105.    ** Set the directory context flags so they take effect.
  106.     */
  107.  
  108.    cCode = NWDSSetContext(
  109.                 /* Context Handle */ context,
  110.                 /* Key            */ DCK_FLAGS,
  111.                 /* Set Flag Value */ &flags
  112.                 );
  113.  
  114.     if (cCode < 0)
  115.         {
  116.         NWDSFreeContext(context);
  117.         printf("NWDSSetContext returned: %04X\n", cCode);
  118.         return 1;
  119.         }
  120.     
  121.     printf("Login name: ");
  122.     gets(loginName);
  123.     printf("\nPassword: ");
  124.     gets(password);
  125.  
  126.     dscCode = NWDSLogin(context, 0, loginName, password, 0);
  127.  
  128.     if(dscCode < 0)
  129.     {
  130.         NWDSFreeContext(context);
  131.         printf("Error Authenticating to DS.  Exiting NLM...\n");
  132.         return 3;
  133.     }
  134.  
  135.     GetServerInformation(48,(FILE_SERV_INFO *)serverName);
  136.     AttachToFileServer(serverName, &fileServerID);
  137.     conHandle = GetCurrentConnection();
  138.     NWDSAuthenticate(conHandle,0, NULL);
  139.  
  140.     printf("Registering NCP extention %s\n", NCPEXTNAME);
  141.  
  142.     cCode = NWRegisterNCPExtension(NCPEXTNAME, AuthServer,
  143.         AuthServerConnEventHandler, NULL, 1, 0, 0, &queryData);
  144.  
  145.     if (cCode != ESUCCESS) {
  146.         printf("Fatal: Unable to register NCP Extension (%08X).\n", cCode);
  147.         return 3;
  148.     }
  149.  
  150.     printf("Press any key to unload this NLM.\n");
  151.     getch();
  152.  
  153.     cCode = NWDeRegisterNCPExtension(queryData);
  154.     if (cCode)
  155.     {
  156.         printf("Error %d Deregistering NCP Extension: %s\n", NCPEXTNAME);
  157.         return 3;
  158.     }
  159.  
  160.     printf("NCP Extension %s Deregistered\n", NCPEXTNAME);
  161.     NWDSLogout(context);
  162.     ReturnConnection(conHandle);
  163.     NWDSFreeContext(context);
  164.     return 0;
  165. }
  166.  
  167.  
  168. BYTE AuthServer(NCPExtensionClient *client, void *reqData,
  169.     LONG requestDataLen, void *repData, LONG *replyDataLen)
  170. {
  171.     AUTHREQUEST             *requestData = reqData;
  172.     AUTHREPLY               *replyData = repData;
  173.     char                                            userName[514];
  174.     unsigned long           conHandle;
  175.     int                                             oldThreadGroupID;
  176.     int                                             cCode = 0xFF;
  177.     int                            rval;
  178.     long                                    objectID;
  179.     WORD                                    ot;
  180.     BYTE                                    lt;
  181.  
  182.     oldThreadGroupID = SetThreadGroupID(myThreadGroupID);
  183.  
  184.     if ((requestDataLen != sizeof(AUTHREQUEST)) ||
  185.         (*replyDataLen != sizeof(AUTHREPLY)))
  186.         return 0xFF;
  187.  
  188.  
  189.     cCode = GetConnectionInformation(client->connection,
  190.                                                 userName,
  191.                                                 &ot,
  192.                                                 &objectID,
  193.                                                 <);
  194.  
  195.     if(cCode)
  196.     {
  197.         printf("Error with GetConnectionInformation: %d\n", cCode);
  198.         replyData->completionCode = 1;
  199.         printf("User is denied access\n");
  200.         return 0;
  201.     }
  202.  
  203.  
  204.  
  205.     conHandle = GetCurrentConnection();
  206.  
  207.     cCode = NWDSMapIDToName(
  208.                         context,
  209.                         conHandle,
  210.                         LongSwap(objectID),
  211.                         userName);
  212.  
  213.     if(cCode)
  214.     {
  215.         printf("Error with NWDSMapIDToName: %d\n", cCode);
  216.         replyData->completionCode = 1;
  217.         printf("User is denied access\n");
  218.     }
  219.     else
  220.     {
  221.         rval = VerifyACL(requestData->objectName, userName);
  222.  
  223.         if(!rval)
  224.             printf("User %s was granted access to %s!\n",
  225.                         userName, requestData->objectName);
  226.         else
  227.             printf("User %s was denied access to %s!\n",
  228.                         userName, requestData->objectName);
  229.  
  230.         replyData->completionCode = rval;
  231.  
  232.     }
  233.  
  234.     SetThreadGroupID(oldThreadGroupID);
  235.     return 0;
  236. }
  237.  
  238.  
  239. void AuthServerConnEventHandler(LONG connection, LONG eventType)
  240. {
  241.     int     oldThreadGroupID;
  242.  
  243.     oldThreadGroupID = SetThreadGroupID(myThreadGroupID);
  244.     SetThreadGroupID(oldThreadGroupID);
  245.     return;
  246. }
  247.  
  248.  
  249.  
  250. /****************************************************************************
  251. **    Function: int VerifyACL(char *objectName, char *userName)
  252. ** This function reads the ACL attributes and then calls the
  253. **    GetAttributeValues() function    to get the values for each attribute.
  254. */
  255. int VerifyACL(char *objectName, char *userName)
  256. {
  257.     NWDSCCODE            cCode;
  258.     NWDS_BUFFER            *outBuf, *inBuf;
  259.     NWDS_ITERATION        iterHandle = -1L;
  260.     int                    rval = 0;
  261.  
  262.  
  263.     cCode = NWDSAllocBuf(
  264.                 /* Buffer Size */ DEFAULT_MESSAGE_LEN,
  265.                 /* Buff. Point.*/ &inBuf
  266.                 );
  267.  
  268.     if (cCode < 0)
  269.             {
  270.             printf("NWDSAllocBuf returned: %04X\n", cCode);
  271.             return(1);
  272.             }
  273.  
  274.    cCode = NWDSInitBuf(
  275.                 /* Context  */ context,
  276.                 /* Operation*/ DSV_READ,
  277.                 /* buffer   */ inBuf
  278.                 );
  279.  
  280.  
  281.     if (cCode < 0)
  282.             {
  283.             printf("NWDSInitBuf returned: %04X\n", cCode);
  284.             NWDSFreeBuf(inBuf);
  285.             return(1);
  286.             }
  287.  
  288.     cCode = NWDSAllocBuf(
  289.                 /* Buffer Size */ DEFAULT_MESSAGE_LEN,
  290.                 /* Buff. Point.*/ &outBuf
  291.                 );
  292.  
  293.     if (cCode < 0)
  294.             {
  295.             printf("NWDSAllocBuf returned: %04X\n", cCode);
  296.             NWDSFreeBuf(inBuf);
  297.             return(1);
  298.             }
  299.  
  300.     cCode = NWDSPutAttrName(
  301.                 /* context   */ context,
  302.                 /* in buffer */ inBuf,
  303.                 /* attr. name*/ "User"
  304.                 );
  305.  
  306.     if (cCode < 0)
  307.             {
  308.             printf("NWDSPutAttrName returned: %04X\n", cCode);
  309.             NWDSFreeBuf(inBuf);
  310.             NWDSFreeBuf(outBuf);
  311.             return(1);
  312.             }
  313.  
  314.  
  315.     cCode = NWDSRead(
  316.                 /* Context     */ context,
  317.                 /* Object name    */ objectName,
  318.                 /* Info. Type  */ DS_ATTRIBUTE_VALUES,
  319.                 /* All Attrib ?*/ FALSE,
  320.                 /* Attri. names*/ inBuf,
  321.                 /* Iter. Handle*/ &iterHandle,
  322.                 /* Object info */ outBuf
  323.                 );
  324.  
  325.     if (cCode < 0)
  326.         {
  327.         printf("\nNWDSRead returned: %04X\n", cCode);
  328.         NWDSFreeBuf(inBuf);
  329.         NWDSFreeBuf(outBuf);
  330.         return(1);
  331.         }
  332.  
  333.  
  334.     rval = GetAttributeValues(outBuf, userName);
  335.  
  336.     NWDSFreeBuf(inBuf);
  337.     NWDSFreeBuf(outBuf);
  338.  
  339.     return(rval);
  340.  
  341. }
  342.  
  343.  
  344.  
  345.  
  346. /****************************************************************************
  347. **    Function: GetAttributeValues(NWDS_BUFFER    *buf)
  348. **                 This function verifies that a User exists on the database
  349. **                 object for the user attempting to make a connection.
  350. */
  351. int GetAttributeValues(NWDS_BUFFER *buf, char *userName)
  352. {
  353.     NWSYNTAX_ID        syntax;
  354.     NWDSCCODE        cCode;
  355.     NWCOUNT            attrCount;
  356.     NWCOUNT            valCount;
  357.     NWSIZE            attrValSize;
  358.     char                attrVal[MAX_DN_CHARS + 1];
  359.     char                attrName[MAX_DN_CHARS + 1];
  360.     int                i, j;
  361.  
  362.  
  363.  
  364.     cCode = NWDSGetAttrCount(
  365.                 /* Context    */ context,
  366.                 /* attr. buff */ buf,
  367.                 /* num of attr*/ &attrCount
  368.                 );
  369.  
  370.     if (cCode < 0)
  371.     {
  372.         printf("\nNWDSGetAttrCount returned: %04X\n", cCode);
  373.         return(1);
  374.     }
  375.  
  376.     for (i = 0; i < attrCount; i++)
  377.     {
  378.         cCode = NWDSGetAttrName(
  379.                     /* Context        */ context,
  380.                     /* attrib. buf    */ buf,
  381.                     /* attrib name    */ attrName,
  382.                     /* attr. val. cnt    */ &valCount,
  383.                     /* Syntax ID      */ &syntax
  384.                     );
  385.  
  386.         if (cCode < 0)
  387.         {
  388.             printf("\nNWDSGetAttrName returned: %04X\n", cCode);
  389.             return(1);
  390.         }
  391.  
  392.  
  393.         /*--------------------------------------------------------------
  394.         ** Verify Users against Passed In Name.
  395.         */
  396.         for (j = 0; j < valCount; j++)
  397.         {
  398.  
  399.             cCode = NWDSComputeAttrValSize(
  400.                             /* Context handle */ context,
  401.                             /* Result Buffer  */ buf,
  402.                             /* Syntax ID      */ syntax,
  403.                             /* Size of attrib.*/ &attrValSize
  404.                             );
  405.  
  406.             cCode = NWDSGetAttrVal(
  407.                         /* Context  */ context,
  408.                         /* read buf */ buf,
  409.                         /* syntax id*/ syntax,
  410.                         /* attr. val*/ attrVal
  411.                         );
  412.  
  413.             if (strcmp(attrVal, userName) == 0)
  414.                 return(0);
  415.  
  416.             if (cCode < 0)
  417.             {
  418.                 printf("\nNWDSGetAttrVal returned: %04X\n", cCode);
  419.                 return(1);
  420.             }
  421.         }
  422.     }
  423.     return(1);
  424. }
  425.