home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Network Support Encyclopedia 96-1
/
novell-nsepro-1996-1-cd2.iso
/
download
/
netware
/
dbsamp.exe
/
DBAPP
/
CLIENT
/
DBAPP.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-01-12
|
29KB
|
1,051 lines
/****************************************************************************
** File: DBAPP.C
**
** Desc: This is the CLIENT side of an NCP Extention server which authenticates
** to services that are loaded as NetWare NLMs. The user is looked up in
** the "User" attribute of the Database to verify that the user has rights.
** If the user has rights, access is granted.
**
** DISCLAIMER
**
** Novell, Inc. makes no representations or warranties with respect to
** any NetWare software, and specifically disclaims any express or
** implied warranties of merchantability, title, or fitness for a
** particular purpose.
**
** Distribution of any NetWare software is forbidden without the
** express written consent of Novell, Inc. Further, Novell reserves
** the right to discontinue distribution of any NetWare software.
**
** Novell is not responsible for lost profits or revenue, loss of use
** of the software, loss of data, costs of re-creating lost data, the
** cost of any substitute equipment or program, or claims by any party
** other than you. Novell strongly recommends a backup be made before
** any software is installed. Technical support for this software
** may be provided at the discretion of Novell.
**
** Programmers:
**
** Ini Who Firm
** --- ----------------- ------------------------------------------------
** CRG Calvin Gaisford Novell Developer Support
** KLB Karl L. Bunnell Novell Developer Support
**
** History:
**
** When Who What
** -------- --- ---------------------------------------------------------
** 1-04-95 CRG Adapted rough code into generic example to be distributed.
*/
/***************************************************************************
** Libraries Linked in for .exe
**
** NWCALLS.LIB
** NWLOCALE.LIB
** NWNET.LIB
**
*/
/***************************************************************************
** #defines
*/
#define NWWIN
#define NCPEXTNAME "DBSAuth"
#define DO_AUTH 1
/****************************************************************************
** Include headers, macros, etc.
*/
/*-----------------------------------------------------------------------
** ANSI -- BORLAND/WINDOWS
*/
#include <windows.h>
#include <bwcc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
/*-----------------------------------------------------------------------
** NetWare
*/
#include <nwlocale.h>
#include <nwnet.h>
#include <nwcalls.h>
#include <nwncpext.h>
/*-----------------------------------------------------------------------
** Program
*/
#include "dbapp.h"
/***************************************************************************
** Type Definitions
*/
typedef struct AUTHREQUEST {
unsigned short Command;
char objectName[516];
} AUTHREQUEST;
typedef struct AUTHREPLY {
unsigned char completionCode; /* Zero means successful */
} AUTHREPLY;
/***************************************************************************
** Global Storage
*/
HANDLE hInst;
int err;
NWDSContextHandle gContext;
NWCONN_HANDLE gConnHandle;
char nContext[MAX_DN_CHARS];
/***************************************************************************
** Function Prototypes
*/
BOOL CALLBACK NCPAuthDlgProc (HWND hDlg, WORD message, WORD wParam, LONG lParam);
BOOL CALLBACK DBFindDlgProc (HWND hDlg, WORD message, WORD wParam, LONG lParam);
WORD InitDialogStuff(HWND hDlg);
WORD DoAuthentication(HWND hDlg);
WORD DoUnAuthentication(HWND hDlg);
WORD GetHostServer(char *objectName, char *hostServer);
WORD GetConnectionHandle(char *nwServerName, NWCONN_HANDLE NWFAR *connHandle);
WORD DoNCPCall(char *dbName, NWCONN_HANDLE NWFAR *connHandle);
WORD ShowDBFindDialog(HWND hDlg);
WORD UpdateDBFindDialog(HWND hDlg);
WORD UpdateSelectedDatabase(HWND hDlg);
WORD UpdateChangedContext(HWND hDlg);
WORD HandleErrorMsg(char *funcName, char *message, NWDSCCODE ccode);
WORD ReadStoredDatabaseName(HWND hDlg);
WORD SetStoredDatabaseName(HWND hDlg);
#pragma argsused
/***************************************************************************
** Main Windows proc. Common to all Windows Apps.
*/
int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
HWND hwnd;
MSG msg;
DLGPROC dlgproc;
(void) lpszCmdLine;
hInst = hInstance;
if (err != 0)
return 1;
dlgproc = (DLGPROC) MakeProcInstance (NCPAuthDlgProc, hInstance);
hwnd = CreateDialog (hInstance, "NCPAUTH_DLG", GetDesktopWindow (), dlgproc);
ShowWindow (hwnd, nCmdShow);
UpdateWindow (hwnd);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
/***************************************************************************
** Callback procedure to handle the Main Dialog for the Application.
*/
BOOL CALLBACK NCPAuthDlgProc (HWND hDlg, WORD message, WORD wParam, LONG lParam)
{
char nameBuf[MAX_DN_CHARS];
switch (message)
{
case WM_CLOSE:
NWDSFreeContext(gContext);
EndDialog (hDlg, 0);
PostQuitMessage (0);
return TRUE;
case WM_INITDIALOG:
InitDialogStuff(hDlg);
return TRUE;
case WM_COMMAND:
switch (wParam)
{
case DATABASE_NAME:
if(HIWORD(lParam) == EN_CHANGE)
{
GetDlgItemText(hDlg, DATABASE_NAME, nameBuf, MAX_DN_CHARS);
if(strlen(nameBuf))
EnableWindow(GetDlgItem(hDlg, IDC_AUTH_BUT), TRUE);
else
EnableWindow(GetDlgItem(hDlg, IDC_AUTH_BUT), FALSE);
}
break;
case IDC_AUTH_BUT:
SetStoredDatabaseName(hDlg);
DoAuthentication(hDlg);
break;
case IDC_UNAUTH_BUT:
DoUnAuthentication(hDlg);
break;
case IDC_QUIT_BUT:
NWDSFreeContext(gContext);
EndDialog (hDlg, 0);
PostQuitMessage (0);
return TRUE;
case IDC_LOCATE_BUT:
ShowDBFindDialog(hDlg);
return TRUE;
}
break;
}
return FALSE;
}
/***************************************************************************
** Callback Procedure for Browse dialog that pops up when the "Locate Dabase"
** button is pressed from the main dialog.
*/
BOOL CALLBACK DBFindDlgProc (HWND hDlg, WORD message, WORD wParam, LONG lParam)
{
char dbName[MAX_DN_CHARS];
switch (message)
{
case WM_CLOSE:
EndDialog (hDlg, 0);
return TRUE;
case WM_INITDIALOG:
UpdateDBFindDialog(hDlg);
return TRUE;
case WM_COMMAND:
switch (wParam)
{
case CANCEL_BUT:
EndDialog (hDlg, 0);
return TRUE;
case CONTAINER_LIST:
if (HIWORD (lParam) == LBN_DBLCLK) /* Double Click*/
{
UpdateChangedContext(hDlg);
UpdateDBFindDialog(hDlg);
}
break;
case DATABASE_LIST:
if (HIWORD (lParam) == LBN_SELCHANGE)
{
UpdateSelectedDatabase(hDlg);
}
if (HIWORD (lParam) == LBN_DBLCLK)
{
UpdateSelectedDatabase(hDlg);
GetDlgItemText(hDlg, SELECTED_DATABASE, dbName, MAX_DN_CHARS);
SetDlgItemText(GetParent(hDlg), DATABASE_NAME, dbName);
EndDialog (hDlg, 0);
}
break;
case SELECT_BUT:
GetDlgItemText(hDlg, SELECTED_DATABASE, dbName, MAX_DN_CHARS);
SetDlgItemText(GetParent(hDlg), DATABASE_NAME, dbName);
EndDialog (hDlg, 0);
return TRUE;
}
break;
}
return FALSE;
}
/***************************************************************************
** Initializes all controls and variables for the Main Dialog.
*/
WORD InitDialogStuff(HWND hDlg)
{
NWDSCCODE ccode;
DWORD flags;
char tmpContext[MAX_DN_CHARS];
BWCCRegister(hInst);
ccode = NWCallsInit(NULL, NULL);
if (ccode <0 )
{
HandleErrorMsg("InitDialogStuff", "NWCallsInit", ccode);
return(ccode);
}
gContext = NWDSCreateContext();
if(gContext == ERR_CONTEXT_CREATION)
{
return((WORD)gContext);
}
ccode = NWDSGetContext(gContext, DCK_NAME_CONTEXT, (void *)tmpContext);
if(ccode < 0)
{
HandleErrorMsg("InitDialogStuff", "NWDSGetContext", ccode);
return(ccode);
}
ccode = NWDSRemoveAllTypes(gContext, tmpContext, nContext);
if(ccode < 0)
{
HandleErrorMsg("InitDialogStuff", "NWDSRemoveAllTypes", ccode);
return(ccode);
}
ccode = NWDSGetContext(gContext, DCK_FLAGS, &flags);
if(ccode < 0)
{
HandleErrorMsg("InitDialogStuff", "NWDSGetContext", ccode);
return(ccode);
}
flags |= DCV_TYPELESS_NAMES;
flags &= ~DCV_CANONICALIZE_NAMES;
ccode = NWDSSetContext(gContext, DCK_FLAGS, &flags);
if(ccode < 0)
{
HandleErrorMsg("InitDialogStuff", "NWDSSetContext", ccode);
return(ccode);
}
ReadStoredDatabaseName(hDlg);
SetDlgItemText(hDlg, DBSERVER_NAME, "");
SetDlgItemText(hDlg, NWSERVER_NAME, "");
SetDlgItemText(hDlg, STATUS_MSG, "Not Authenticated");
return(FALSE);
}
/***************************************************************************
** Attempts to read the stored Database name from the User object in the
** attribute "DBName". If it is successful in reading the database name
** the "DATABASE_NAME" text box will be updated with the read name.
*/
WORD ReadStoredDatabaseName(HWND hDlg)
{
NWDS_ITERATION iterHandle=-1L;
NWDS_BUFFER *inBuf, *outBuf;
NWCOUNT totalAttrs, totalValues;
NWDSCCODE ccode;
NWSYNTAX_ID syntaxID;
char name[MAX_DN_CHARS]; /* never anything but "DBName" */
char databaseName[MAX_DN_CHARS];
char objectName[MAX_DN_CHARS];
ccode = NWDSWhoAmI(gContext, objectName);
if(ccode < 0)
{
HandleErrorMsg("ReadStoredDatabaseName", "NWWhoAmI", ccode);
return(TRUE);
}
ccode = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &inBuf);
if(ccode <0)
{
HandleErrorMsg("ReadStoredDatabaseName", "NWDSAllocBuf", ccode);
return(TRUE);
}
ccode = NWDSAllocBuf(MAX_DN_CHARS, &outBuf);
if(ccode <0)
{
HandleErrorMsg("ReadStoredDatabaseName", "NWDSAllocBuf", ccode);
NWDSFreeBuf(outBuf);
return(TRUE);
}
ccode = NWDSInitBuf(gContext,DSV_READ,inBuf);
if(ccode <0)
{
HandleErrorMsg("ReadStoredDatabaseName", "NWDSInitBuf", ccode);
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(TRUE);
}
ccode = NWDSPutAttrName(gContext,inBuf,"DBName"); /* We only want the */
if(ccode <0) /* attribute "Host Server" */
{
HandleErrorMsg("ReadStoredDatabaseName", "NWDSPutAttrName", ccode);
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(TRUE);
}
ccode = NWDSRead(gContext,
objectName, /* name of Object to read (IN) */
DS_ATTRIBUTE_VALUES, /* read name and value (IN) */
FALSE, /* only attributes specified (IN) */
inBuf, /* buffer holding "Host Server" (IN) */
&iterHandle, /* read until all values (OUT) */
outBuf); /* where the values go to(OUT) */
if(ccode < 0)
{
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(TRUE);
}
ccode = NWDSGetAttrCount(gContext, outBuf, &totalAttrs);
if(ccode < 0)
{
HandleErrorMsg("ReadStoredDatabaseName", "NWDSGetAttrCount", ccode);
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(TRUE);
}
if((NWCOUNT)0 == totalAttrs)
{
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(TRUE);
}
else
{
ccode = NWDSGetAttrName(gContext,
outBuf, /* values are in here (IN) */
name, /* better be DBName (OUT) */
&totalValues, /* number of values (OUT) */
&syntaxID); /* how to read the value (OUT) */
if(ccode <0)
{
HandleErrorMsg("ReadStoredDatabaseName", "NWDSGetAttrName", ccode);
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(TRUE);
}
if(syntaxID != SYN_DIST_NAME)
{
MessageBox(NULL, "Syntax was wrong", "ReadStoredDatabaseName", MB_OK);
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(TRUE);
}
ccode = NWDSGetAttrVal(gContext, /* context handle (IN) */
outBuf, /* buffer with info (IN) */
syntaxID, /* syntax ID (IN) */
databaseName); /* value of attrib (OUT) */
if(ccode <0)
{
HandleErrorMsg("ReadStoredDatabaseName", "NWDSGetAttrVal", ccode);
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(TRUE);
}
}
if(databaseName)
{
SetDlgItemText(hDlg, DATABASE_NAME, databaseName);
return(FALSE);
}
else
{
SetDlgItemText(hDlg, DATABASE_NAME, "");
return(TRUE);
}
}
WORD SetStoredDatabaseName(HWND hDlg)
{
NWDS_BUFFER *inBuf;
NWDSCCODE ccode;
char databaseName[MAX_DN_CHARS];
char objectName[MAX_DN_CHARS];
GetDlgItemText(hDlg, DATABASE_NAME, databaseName, MAX_DN_CHARS);
ccode = NWDSWhoAmI(gContext, objectName);
if(ccode < 0)
{
HandleErrorMsg("SetStoredDatabaseName", "NWWhoAmI", ccode);
return(TRUE);
}
ccode = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &inBuf);
if(ccode <0)
{
HandleErrorMsg("SetStoredDatabaseName", "NWDSAllocBuf", ccode);
return(TRUE);
}
ccode = NWDSInitBuf(gContext,DSV_MODIFY_ENTRY,inBuf);
if(ccode <0)
{
HandleErrorMsg("SetStoredDatabaseName", "NWDSInitBuf", ccode);
NWDSFreeBuf(inBuf);
return(TRUE);
}
ccode = NWDSPutChange(gContext, inBuf, DS_OVERWRITE_VALUE, "DBName");
if(ccode <0)
{
HandleErrorMsg("SetStoredDatabaseName", "NWDSPutChange", ccode);
NWDSFreeBuf(inBuf);
return(TRUE);
}
ccode = NWDSPutAttrVal(gContext, inBuf, SYN_DIST_NAME, databaseName);
if(ccode <0)
{
HandleErrorMsg("SetStoredDatabaseName", "NWDSPutAttrVal", ccode);
NWDSFreeBuf(inBuf);
return(TRUE);
}
ccode = NWDSModifyObject(gContext,
objectName,
NULL,
(NWFLAGS)0,
inBuf);
if(ccode < 0)
{
HandleErrorMsg("SetStoredDatabaseName", "NWDSModifyObject", ccode);
NWDSFreeBuf(inBuf);
return(TRUE);
}
return(FALSE);
}
/***************************************************************************
** Pops up the dialog and relates the process for the find database dialog.
*/
WORD ShowDBFindDialog(HWND hDlg)
{
static FARPROC lpfnDBFindDlgProc;
lpfnDBFindDlgProc = MakeProcInstance((FARPROC)DBFindDlgProc, hInst);
DialogBox(hInst, "DBFIND_DLG", GetActiveWindow(), lpfnDBFindDlgProc);
FreeProcInstance(lpfnDBFindDlgProc);
return FALSE;
}
/***************************************************************************
** Updates the find database dialog so the list of containers reflects the
** containers that are under the current context. This is called any time
** a user changes the context in the browser. (it is also called when first
** initialized.
*/
WORD UpdateDBFindDialog(HWND hDlg)
{
NWDS_BUFFER *outBuf;
NWDS_ITERATION iterationHandle = -1L;
NWDSCCODE ccode;
NWCOUNT objectCount, attrCount,i;
NWOBJECT_INFO objectInfo;
char objectName[MAX_DN_CHARS];
SendDlgItemMessage(hDlg, CONTAINER_LIST, LB_RESETCONTENT, 0, 0);
SendDlgItemMessage(hDlg, DATABASE_LIST, LB_RESETCONTENT, 0, 0);
SetDlgItemText(hDlg, CURRENT_CONTEXT, nContext);
SetDlgItemText(hDlg, SELECTED_DATABASE , "");
ccode = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &outBuf);
if(ccode < 0)
{
HandleErrorMsg("UpdateDBFindDialog", "NWDSAllocBuf", ccode);
return(TRUE);
}
do
{
ccode = NWDSList(gContext,
nContext,
&iterationHandle,
outBuf);
if(ccode < 0)
{
HandleErrorMsg("UpdateDBFindDialog", "NWDSList", ccode);
NWDSFreeBuf(outBuf);
return(TRUE);
}
ccode = NWDSGetObjectCount(gContext,
outBuf,
&objectCount);
if(ccode < 0)
{
HandleErrorMsg("UpdateDBFindDialog", "NWDSGetObjectCount", ccode);
NWDSFreeBuf(outBuf);
return(TRUE);
}
for(i=0; i<objectCount; i++)
{
ccode = NWDSGetObjectName(gContext,
outBuf,
objectName,
&attrCount,
&objectInfo);
if(ccode < 0)
{
HandleErrorMsg("UpdateDBFindDialog", "NWDSGetObjectName", ccode);
NWDSFreeBuf(outBuf);
return(TRUE);
}
if(objectInfo.objectFlags & DS_CONTAINER_ENTRY)
SendDlgItemMessage(hDlg, CONTAINER_LIST, LB_ADDSTRING, 0,
(LONG) (LPSTR) objectName);
if(!strcmp(objectInfo.baseClass,"Database"))
SendDlgItemMessage(hDlg, DATABASE_LIST, LB_ADDSTRING, 0,
(LONG) (LPSTR) objectName);
}
}
while(iterationHandle != -1);
if(strcmp("[Root]", nContext))
SendDlgItemMessage(hDlg, CONTAINER_LIST, LB_ADDSTRING, 0,
(LONG) (LPSTR) "<- ..");
NWDSFreeBuf(outBuf);
return(FALSE);
}
/***************************************************************************
** Changes the global context for the app when a new container is selected
*/
WORD UpdateChangedContext(HWND hDlg)
{
NWDSCCODE ccode;
char contextName[MAX_DN_CHARS];
char contextCName[MAX_DN_CHARS];
WORD n;
n = (WORD) SendDlgItemMessage(hDlg, CONTAINER_LIST, LB_GETCURSEL, 0, 0L);
SendDlgItemMessage(hDlg, CONTAINER_LIST, LB_GETTEXT, n,
(LONG) (LPSTR) contextName);
if(!strcmp("<- ..", contextName))
strcpy(contextName, ".");
ccode = NWDSCanonicalizeName(gContext, contextName, contextCName);
if(ccode < 0)
{
HandleErrorMsg("UpdateChangedContext", "NWDSCanonicalizeName", ccode);
return(ccode);
}
ccode = NWDSSetContext(gContext, DCK_NAME_CONTEXT, (void *)contextCName);
if(ccode < 0)
{
HandleErrorMsg("UpdateChangedContext", "NWDSSetContext", ccode);
return(ccode);
}
ccode = NWDSGetContext(gContext, DCK_NAME_CONTEXT, (void *)contextName);
if(ccode < 0)
{
HandleErrorMsg("UpdateChangedContext", "NWDSGetContext", ccode);
return(ccode);
}
ccode = NWDSRemoveAllTypes(gContext, contextName, nContext);
if(ccode < 0)
{
HandleErrorMsg("UpdateChangedContext", "NWDSRemoveAllTypes", ccode);
return(ccode);
}
return(FALSE);
}
/***************************************************************************
** Updates the database field when a database is selected from the list.
*/
WORD UpdateSelectedDatabase(HWND hDlg)
{
char dbName[MAX_DN_CHARS];
char dbDName[MAX_DN_CHARS];
WORD n;
n = (WORD) SendDlgItemMessage(hDlg, DATABASE_LIST, LB_GETCURSEL, 0, 0L);
SendDlgItemMessage(hDlg, DATABASE_LIST, LB_GETTEXT, n,
(LONG) (LPSTR) dbName);
strcpy(dbDName, dbName);
strcat(dbDName, ".");
strcat(dbDName, nContext);
SetDlgItemText(hDlg, SELECTED_DATABASE, dbDName);
return(FALSE);
}
/***************************************************************************
** Main control for finding the Database Server (by reading the location
** from the database) and then the NetWare server than has the NLM loaded
** (by reading the location fromt the Database server. Finally, the
** Network address of the NetWare server is found and the NCP Extention
** call is made.
*/
WORD DoAuthentication(HWND hDlg)
{
char dbName[MAX_DN_CHARS];
char dbServerName[MAX_DN_CHARS];
char nwServerName[MAX_DN_CHARS];
GetDlgItemText(hDlg, DATABASE_NAME, dbName, MAX_DN_CHARS);
SetDlgItemText(hDlg, STATUS_MSG, "Looking up Database...");
if(!GetHostServer(dbName, dbServerName))
{
if(!GetHostServer(dbServerName, nwServerName))
{
if(!GetConnectionHandle(nwServerName, &gConnHandle))
{
if(!NWDSAuthenticate(gConnHandle, 0, NULL))
{
SetDlgItemText(hDlg, STATUS_MSG, "Making NCP Call...");
if(!DoNCPCall(dbName, &gConnHandle))
{
SetDlgItemText(hDlg, STATUS_MSG, "Authenticated");
SetDlgItemText(hDlg, DBSERVER_NAME, dbServerName);
SetDlgItemText(hDlg, NWSERVER_NAME, nwServerName);
EnableWindow(GetDlgItem(hDlg, IDC_UNAUTH_BUT), TRUE);
EnableWindow(GetDlgItem(hDlg, IDC_AUTH_BUT), FALSE);
EnableWindow(GetDlgItem(hDlg, IDC_LOCATE_BUT), FALSE);
EnableWindow(GetDlgItem(hDlg, DATABASE_NAME), FALSE);
return(FALSE);
}
else
SetDlgItemText(hDlg, STATUS_MSG, "Not Authenticated");
}
}
}
}
else
{
return(TRUE);
}
return(TRUE);
}
/***************************************************************************
** Resets the Dialog to reflect no authentication but doesn't have to do
** anything to the actuall server because we really didn't have a service
** loaded in this example.
*/
WORD DoUnAuthentication(HWND hDlg)
{
SetDlgItemText(hDlg, STATUS_MSG, "Not Authenticated");
SetDlgItemText(hDlg, DBSERVER_NAME, "");
SetDlgItemText(hDlg, NWSERVER_NAME, "");
EnableWindow(GetDlgItem(hDlg, IDC_UNAUTH_BUT), FALSE);
EnableWindow(GetDlgItem(hDlg, IDC_AUTH_BUT), TRUE);
EnableWindow(GetDlgItem(hDlg, IDC_LOCATE_BUT), TRUE);
EnableWindow(GetDlgItem(hDlg, DATABASE_NAME), TRUE);
return(FALSE);
}
/***************************************************************************
** Reads the host server object name from the object name passed in.
** Both the Database and Database Servers have host servers. The host
** server for a Database is a Database Server and the host server for
** a Database Server is a NetWare Server.
*/
WORD GetHostServer(char *objectName, char *hostServer)
{
NWDS_ITERATION iterHandle=-1L;
NWDS_BUFFER *inBuf, *outBuf;
NWCOUNT totalAttrs, totalValues;
NWDSCCODE ccode;
NWSYNTAX_ID syntaxID;
char name[MAX_DN_CHARS]; /* never anything but "Host Server" */
char tmpHostServer[MAX_DN_CHARS];
ccode = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &inBuf);
if(ccode <0)
{
HandleErrorMsg("GetHostServer", "NWDSAllocBuf", ccode);
return(TRUE);
}
ccode = NWDSAllocBuf(MAX_DN_CHARS, &outBuf);
if(ccode <0)
{
HandleErrorMsg("GetHostServer", "NWDSAllocBuf", ccode);
NWDSFreeBuf(outBuf);
return(TRUE);
}
ccode = NWDSInitBuf(gContext,DSV_READ,inBuf);
if(ccode <0)
{
HandleErrorMsg("GetHostServer", "NWDSInitBuf", ccode);
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(TRUE);
}
ccode = NWDSPutAttrName(gContext,inBuf,"Host Server"); /* We only want the */
if(ccode <0) /* attribute "Host Server" */
{
HandleErrorMsg("GetHostServer", "NWDSPutAttrName", ccode);
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(TRUE);
}
ccode = NWDSRead(gContext,
objectName, /* name of Object to read (IN) */
DS_ATTRIBUTE_VALUES, /* read name and value (IN) */
FALSE, /* only attributes specified (IN) */
inBuf, /* buffer holding "Host Server" (IN) */
&iterHandle, /* read until all values (OUT) */
outBuf); /* where the values go to(OUT) */
if(ccode < 0)
{
HandleErrorMsg("GetHostServer", "NWDSRead", ccode);
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(TRUE);
}
ccode = NWDSGetAttrCount(gContext, outBuf, &totalAttrs);
if(ccode < 0)
{
HandleErrorMsg("GetHostServer", "NWDSGetAttrCount", ccode);
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(TRUE);
}
if((NWCOUNT)0 == totalAttrs)
{
MessageBox(NULL, "No Host Server was Found", "GetHostServer", MB_OK);
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(TRUE);
}
else
{
ccode = NWDSGetAttrName(gContext,
outBuf, /* values are in here (IN) */
name, /* better be Host Server (OUT) */
&totalValues, /* number of values (OUT) */
&syntaxID); /* how to read the value (OUT) */
if(ccode <0)
{
HandleErrorMsg("GetHostServer", "NWDSGetAttrName", ccode);
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(TRUE);
}
if(syntaxID != SYN_DIST_NAME)
{
MessageBox(NULL, "Syntax was wrong", "GetHostServer", MB_OK);
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(TRUE);
}
ccode = NWDSGetAttrVal(gContext, /* context handle (IN) */
outBuf, /* buffer with info (IN) */
syntaxID, /* syntax ID (IN) */
tmpHostServer); /* value of attrib (OUT) */
if(ccode <0)
{
HandleErrorMsg("GetHostServer", "NWDSGetAttrVal", ccode);
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(TRUE);
}
}
if(tmpHostServer)
{
strcpy(hostServer,tmpHostServer);
return(FALSE);
}
else
{
strcpy(hostServer, NULL);
return(TRUE);
}
}
/***************************************************************************
** Creates (if not already connected) and reads the connection handle from
** the passed in NetWare server.
*/
WORD GetConnectionHandle(char *nwServerName, NWCONN_HANDLE NWFAR *connHandle)
{
NWDS_BUFFER *outBuf,*inBuf;
NWDS_ITERATION iterationHandle = -1L;
NWDSCCODE ccode;
NWSIZE attrSize;
void *attrValue;
NWSYNTAX_ID syntax;
NWCOUNT valCount;
NWCOUNT totalAttrs;
char currAttr[MAX_DN_CHARS];
ccode = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &inBuf);
if(ccode < 0)
return(ccode);
ccode = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &outBuf);
if(ccode < 0)
{
NWDSFreeBuf(inBuf);
return(ccode);
}
ccode = NWDSInitBuf(gContext,DSV_READ,inBuf);
if(ccode <0)
{
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(ccode);
}
ccode = NWDSPutAttrName(gContext,inBuf,"Network Address");
if(ccode <0)
{
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(ccode);
}
ccode = NWDSRead(gContext, /* Context Handle (IN) */
nwServerName, /* name of object (IN) */
DS_ATTRIBUTE_VALUES, /* a #define (IN) */
FALSE, /* #define as 1 (IN) */
inBuf, /* Attribute names (IN) */
&iterationHandle, /* iteration Handle (OUT) */
outBuf); /* Output Buffer (OUT) */
if(ccode < 0)
{
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(ccode);
}
ccode = NWDSGetAttrCount(gContext, outBuf, &totalAttrs);
if(ccode < 0)
{
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(ccode);
}
if((NWCOUNT)0 == totalAttrs)
{
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(TRUE);
}
else
{
ccode = NWDSGetAttrName(gContext, outBuf, currAttr, &valCount, &syntax);
if(ccode < 0)
{
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(ccode);
}
ccode = NWDSComputeAttrValSize(gContext, outBuf, syntax, &attrSize);
if(ccode < 0)
{
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(ccode);
}
attrValue=malloc((size_t)attrSize);
if(attrValue == NULL)
{
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(ccode);
}
memset(attrValue, '\x0', (size_t)attrSize);
ccode = NWDSGetAttrVal(gContext, outBuf, syntax, attrValue);
if(ccode < 0)
{
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(ccode);
}
ccode = NWDSGetConnectionSlot(1,
((Net_Address_T *)attrValue)->addressType,
((Net_Address_T *)attrValue)->addressLength,
((Net_Address_T *)attrValue)->address,
connHandle);
if(ccode < 0)
{
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(ccode);
}
free(attrValue);
}
NWDSFreeBuf(inBuf);
NWDSFreeBuf(outBuf);
return(ccode);
}
/***************************************************************************
** Actually makes the NCP Extention call.
*/
WORD DoNCPCall(char *dbName, NWCONN_HANDLE NWFAR *connHandle)
{
NWDSCCODE ccode;
DWORD NCPExtID;
BYTE majorVersion;
BYTE minorVersion;
BYTE revision;
BYTE queryData[32];
AUTHREQUEST Request;
AUTHREPLY Reply;
WORD replyLen;
/* Make sure NCP Extension server is loaded */
ccode = NWGetNCPExtensionInfoByName(*connHandle, NCPEXTNAME, &NCPExtID,
&majorVersion, &minorVersion, &revision, queryData);
if (ccode < 0)
{
HandleErrorMsg("DoNCPCall", "NWGetNCPExtensionInfoByName", ccode);
return(TRUE);
}
/* Setup AUTHREQUEST block for NCPExtension request */
Request.Command = DO_AUTH;
/* Check command line arguments */
strcpy(Request.objectName, dbName);
replyLen = sizeof(AUTHREPLY);
ccode = NWNCPExtensionRequest(*connHandle, NCPExtID,
&Request, sizeof(AUTHREQUEST), &Reply, &replyLen);
if (ccode < 0)
{
HandleErrorMsg("DoNCPCall", "NWNCPExtensionRequest", ccode);
return(TRUE);
}
if (Reply.completionCode != SUCCESSFUL)
{
MessageBox(NULL, "Authentication Failed from NCP", "DoNCPCall", MB_OK);
return(TRUE);
}
return(FALSE);
}
/***************************************************************************
** A procedure for handling all error messages.
*/
WORD HandleErrorMsg(char *funcName, char *message, NWDSCCODE ccode)
{
char newMessage[DEFAULT_MESSAGE_LEN];
wsprintf(newMessage,"The Function: %s\n making the call: %s\n returned code: %d",funcName, message, ccode);
MessageBox(NULL, newMessage, "NDSError", MB_OK);
return(FALSE);
}