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 >
Wrap
C/C++ Source or Header
|
1995-01-12
|
10KB
|
425 lines
/*------------------------------------------------------------------------
** Module: DBNLM v1.00 Copyright (c) 1994 NDA TEAM - Novell, Inc.
**
** Source: NDA Team
**
** Abstract:
**
** This NLM is designed to respond to NCP Extension requests and
** grant access to a Database object which is passed in the
** NCP packet. This NLM gets the distinquished name of the user
** requesting access to the database. The DN of the database is
** passed to NWDSRead for the purpose of verifying that the
** user requesting access exists in a "User" attribute of the
** database object.
**
** Environment:
** 4.x
**
**
**------------------------------------------------------------------------
*/
#pragma warn -stu-
#define NWNLM
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <advanced.h>
#include <process.h>
#include <conio.h>
#include <errno.h>
#include <nwenvrn.h>
#include <nwdsapi.h>
#include <nwdsbuft.h>
#include <nwalias.h>
#include <nwdssch.h>
#include <nwcntask.h>
#include <nwconn.h>
#include <nwmisc.h>
#include <ncpext.h>
#pragma warn -stu.
#include "dbnlm.h"
/* ---------- Function Prototypes ---------- */
BYTE AuthServer(NCPExtensionClient *client, void *reqData,
LONG requestDataLen, void *repData, LONG *replyDataLen);
void AuthServerConnEventHandler(LONG connection, LONG eventType);
/*int AuthToDS(void);
*/
/* ---------- Global Variables ---------- */
int myThreadGroupID;
void *queryData;
NWDSContextHandle context;
int main(void)
{
int cCode;
NWDSCCODE dscCode;
unsigned conHandle;
unsigned flags;
WORD fileServerID;
char serverName[48+1];
char loginName[MAX_DN_CHARS];
char password[256];
myThreadGroupID = GetThreadGroupID();
context=NWDSCreateContext();
if(context == ERR_CONTEXT_CREATION)
{
printf("Context Creation Failed. Exiting NLM...\n");
return 3;
}
cCode = NWDSGetContext(
/* Contxt Handle */ context,
/* Key */ DCK_FLAGS,
/* Context Flags */ &flags
);
if (cCode < 0)
{
NWDSFreeContext(context);
printf("NWDSGetContext returned: %04X\n", cCode);
return 1;
}
/*-------------------------------------------------------------------
** Turn typeless naming on.
** Turn canonicalize names off. This means we will get full names.
*/
flags |= DCV_TYPELESS_NAMES;
flags &= ~DCV_CANONICALIZE_NAMES;
/*-------------------------------------------------------------------
** Set the directory context flags so they take effect.
*/
cCode = NWDSSetContext(
/* Context Handle */ context,
/* Key */ DCK_FLAGS,
/* Set Flag Value */ &flags
);
if (cCode < 0)
{
NWDSFreeContext(context);
printf("NWDSSetContext returned: %04X\n", cCode);
return 1;
}
printf("Login name: ");
gets(loginName);
printf("\nPassword: ");
gets(password);
dscCode = NWDSLogin(context, 0, loginName, password, 0);
if(dscCode < 0)
{
NWDSFreeContext(context);
printf("Error Authenticating to DS. Exiting NLM...\n");
return 3;
}
GetServerInformation(48,(FILE_SERV_INFO *)serverName);
AttachToFileServer(serverName, &fileServerID);
conHandle = GetCurrentConnection();
NWDSAuthenticate(conHandle,0, NULL);
printf("Registering NCP extention %s\n", NCPEXTNAME);
cCode = NWRegisterNCPExtension(NCPEXTNAME, AuthServer,
AuthServerConnEventHandler, NULL, 1, 0, 0, &queryData);
if (cCode != ESUCCESS) {
printf("Fatal: Unable to register NCP Extension (%08X).\n", cCode);
return 3;
}
printf("Press any key to unload this NLM.\n");
getch();
cCode = NWDeRegisterNCPExtension(queryData);
if (cCode)
{
printf("Error %d Deregistering NCP Extension: %s\n", NCPEXTNAME);
return 3;
}
printf("NCP Extension %s Deregistered\n", NCPEXTNAME);
NWDSLogout(context);
ReturnConnection(conHandle);
NWDSFreeContext(context);
return 0;
}
BYTE AuthServer(NCPExtensionClient *client, void *reqData,
LONG requestDataLen, void *repData, LONG *replyDataLen)
{
AUTHREQUEST *requestData = reqData;
AUTHREPLY *replyData = repData;
char userName[514];
unsigned long conHandle;
int oldThreadGroupID;
int cCode = 0xFF;
int rval;
long objectID;
WORD ot;
BYTE lt;
oldThreadGroupID = SetThreadGroupID(myThreadGroupID);
if ((requestDataLen != sizeof(AUTHREQUEST)) ||
(*replyDataLen != sizeof(AUTHREPLY)))
return 0xFF;
cCode = GetConnectionInformation(client->connection,
userName,
&ot,
&objectID,
<);
if(cCode)
{
printf("Error with GetConnectionInformation: %d\n", cCode);
replyData->completionCode = 1;
printf("User is denied access\n");
return 0;
}
conHandle = GetCurrentConnection();
cCode = NWDSMapIDToName(
context,
conHandle,
LongSwap(objectID),
userName);
if(cCode)
{
printf("Error with NWDSMapIDToName: %d\n", cCode);
replyData->completionCode = 1;
printf("User is denied access\n");
}
else
{
rval = VerifyACL(requestData->objectName, userName);
if(!rval)
printf("User %s was granted access to %s!\n",
userName, requestData->objectName);
else
printf("User %s was denied access to %s!\n",
userName, requestData->objectName);
replyData->completionCode = rval;
}
SetThreadGroupID(oldThreadGroupID);
return 0;
}
void AuthServerConnEventHandler(LONG connection, LONG eventType)
{
int oldThreadGroupID;
oldThreadGroupID = SetThreadGroupID(myThreadGroupID);
SetThreadGroupID(oldThreadGroupID);
return;
}
/****************************************************************************
** Function: int VerifyACL(char *objectName, char *userName)
** This function reads the ACL attributes and then calls the
** GetAttributeValues() function to get the values for each attribute.
*/
int VerifyACL(char *objectName, char *userName)
{
NWDSCCODE cCode;
NWDS_BUFFER *outBuf, *inBuf;
NWDS_ITERATION iterHandle = -1L;
int rval = 0;
cCode = NWDSAllocBuf(
/* Buffer Size */ DEFAULT_MESSAGE_LEN,
/* Buff. Point.*/ &inBuf
);
if (cCode < 0)
{
printf("NWDSAllocBuf returned: %04X\n", cCode);
return(1);
}
cCode = NWDSInitBuf(
/* Context */ context,
/* Operation*/ DSV_READ,
/* buffer */ inBuf
);
if (cCode < 0)
{
printf("NWDSInitBuf returned: %04X\n", cCode);
NWDSFreeBuf(inBuf);
return(1);
}
cCode = NWDSAllocBuf(
/* Buffer Size */ DEFAULT_MESSAGE_LEN,
/* Buff. Point.*/ &outBuf
);
if (cCode < 0)
{
printf("NWDSAllocBuf returned: %04X\n", cCode);
NWDSFreeBuf(inBuf);
return(1);
}
cCode = NWDSPutAttrName(
/* context */ context,
/* in buffer */ inBuf,
/* attr. name*/ "User"
);
if (cCode < 0)
{
printf("NWDSPutAttrName returned: %04X\n", cCode);
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(1);
}
cCode = NWDSRead(
/* Context */ context,
/* Object name */ objectName,
/* Info. Type */ DS_ATTRIBUTE_VALUES,
/* All Attrib ?*/ FALSE,
/* Attri. names*/ inBuf,
/* Iter. Handle*/ &iterHandle,
/* Object info */ outBuf
);
if (cCode < 0)
{
printf("\nNWDSRead returned: %04X\n", cCode);
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(1);
}
rval = GetAttributeValues(outBuf, userName);
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(rval);
}
/****************************************************************************
** Function: GetAttributeValues(NWDS_BUFFER *buf)
** This function verifies that a User exists on the database
** object for the user attempting to make a connection.
*/
int GetAttributeValues(NWDS_BUFFER *buf, char *userName)
{
NWSYNTAX_ID syntax;
NWDSCCODE cCode;
NWCOUNT attrCount;
NWCOUNT valCount;
NWSIZE attrValSize;
char attrVal[MAX_DN_CHARS + 1];
char attrName[MAX_DN_CHARS + 1];
int i, j;
cCode = NWDSGetAttrCount(
/* Context */ context,
/* attr. buff */ buf,
/* num of attr*/ &attrCount
);
if (cCode < 0)
{
printf("\nNWDSGetAttrCount returned: %04X\n", cCode);
return(1);
}
for (i = 0; i < attrCount; i++)
{
cCode = NWDSGetAttrName(
/* Context */ context,
/* attrib. buf */ buf,
/* attrib name */ attrName,
/* attr. val. cnt */ &valCount,
/* Syntax ID */ &syntax
);
if (cCode < 0)
{
printf("\nNWDSGetAttrName returned: %04X\n", cCode);
return(1);
}
/*--------------------------------------------------------------
** Verify Users against Passed In Name.
*/
for (j = 0; j < valCount; j++)
{
cCode = NWDSComputeAttrValSize(
/* Context handle */ context,
/* Result Buffer */ buf,
/* Syntax ID */ syntax,
/* Size of attrib.*/ &attrValSize
);
cCode = NWDSGetAttrVal(
/* Context */ context,
/* read buf */ buf,
/* syntax id*/ syntax,
/* attr. val*/ attrVal
);
if (strcmp(attrVal, userName) == 0)
return(0);
if (cCode < 0)
{
printf("\nNWDSGetAttrVal returned: %04X\n", cCode);
return(1);
}
}
}
return(1);
}