home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Network Support Encyclopedia 96-1
/
novell-nsepro-1996-1-cd2.iso
/
download
/
netware
/
schrdn.exe
/
SCHRDN.C
next >
Wrap
C/C++ Source or Header
|
1995-07-13
|
12KB
|
465 lines
/****************************************************************************
** File: SCHRDN.C
**
** Desc: This sample code demonstrates how to set up an NWDSSearch() to
** search for objects in the NDS tree. More specifically this code
** demonstrates how to use the FTOK_RDN token to allow searching
** for a "common name" or a "relative distinquished name" without
** having an authenticated NDS connections. An attachment to an NDS
** server is all that is required.
**
**
** Copyright (c) 1994 Novell, Inc. All Rights Reserved.
**
** THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
** TREATIES. USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE
** LICENSE AGREEMENT ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK)
** THAT CONTAINS THIS WORK.
**
** Pursuant to the SDK License Agreement, Novell hereby grants to
** Developer a royalty-free, non-exclusive license to include the
** sample code SNAPEX01.C and derivative binaries in its product.
** Novell grants to Developer worldwide distribution rights to market,
** distribute or sell the sample code SNAPEX01.C and derivative
** binaries as a component of Developer's product(s). Novell shall
** have no obligations to Developer or Developer's customers with
** respect to this code.
**
**
** DISCLAIMER
**
** Novell, Inc. makes no representations or warranties with respect
** to the contents or use of this code, and specifically disclaims any
** express or implied warranties of merchantability or fitness for any
** particular purpose. Further, Novell, Inc. reserves the right to revise
** this publication and to make changes to its content, at any time,
** without obligation to notify any person or entity of such revisions or
** changes.
**
** Further, Novell, Inc. makes no representations or warranties with
** respect to any software, and specifically disclaims any express or
** implied warranties of merchantability or fitness for any particular
** purpose. Further, Novell, Inc. reserves the right to make changes to
** any and all parts of the software, at any time, without obligation to
** notify any person or entity of such changes.
**
**
** Programmers:
**
** Ini Who Firm
** -----------------------------------------------------------------------
** KLB Karl Bunnell Novell Developer Support.
**
** History:
**
** When Who What
** -----------------------------------------------------------------------
** 07-13-95 klb First code.
*/
/****************************************************************************
** Include headers, macros, function prototypes, etc.
*/
/*------------------------------------------------------------------------
** MACROS
*/
#define NWDOS
/*------------------------------------------------------------------------
** ANSI
*/
#include <stdlib.h>
#include <stdio.h>
/*------------------------------------------------------------------------
** NetWare
*/
#include <nwnet.h>
#include <nwcalls.h>
#include <nwlocale.h>
/*------------------------------------------------------------------------
** Prototypes
*/
int SearchForObjects(char *commonName);
int ActOnData(NWDS_BUFFER NWFAR *buffer);
/*------------------------------------------------------------------------
** Globals
*/
NWDSContextHandle dContext;
void main(int argC, char *argV[])
{
NWDSCCODE cCode;
LCONV lconvInfo;
DWORD flags;
if(argC < 2)
{
printf("\nUsage:SCHRDN <Object Name>");
exit(1);
}
cCode = NWCallsInit(NULL, NULL);
if (cCode)
{
printf("\nCall to NWCallsInit returned: %04X", cCode);
exit(1);
}
NWLsetlocale(LC_ALL, "");
NWLlocaleconv(&lconvInfo);
cCode = NWInitUnicodeTables(lconvInfo.country_id, lconvInfo.code_page);
if(cCode)
{
printf("NWInitUnicodeTables() returned: %04X\n", cCode);
goto _FreeUnicodeTables;
}
dContext = NWDSCreateContext();
if (dContext == ERR_CONTEXT_CREATION)
{
printf("NWDSCreateContext returned: %04X\n", cCode);
goto _FreeContext;
}
/*-------------------------------------------------------------------
** Get the current directory context flags so we can modify them.
*/
cCode = NWDSGetContext(
/* Contxt Handle */ dContext,
/* Key */ DCK_FLAGS,
/* Context Flags */ &flags
);
if (cCode < 0)
{
printf("NWDSGetContext returned: %04X\n", cCode);
goto _FreeContext;
}
/*-------------------------------------------------------------------
** 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 */ dContext,
/* Key */ DCK_FLAGS,
/* Set Flag Value */ &flags
);
if (cCode < 0)
{
printf("NWDSSetContext returned: %04X\n", cCode);
goto _FreeContext;
}
SearchForObjects(argV[1]);
_FreeContext:
NWDSFreeContext(dContext);
_FreeUnicodeTables:
NWFreeUnicodeTables();
}
int SearchForObjects(char *commonName)
{
NWFLAGS searchAliases = FALSE;
NWDS_ITERATION iterHandle = (NWDS_ITERATION)-1;
NWDS_BUFFER NWFAR *searchFilter = NULL,
NWFAR *attrNames = NULL,
NWFAR *retBuf = NULL;
NWDSCCODE cCode;
NWDS_NUM_OBJ cntObjectsToSearch,
cntObjectsSearched;
int i;
void NWFAR *val;
NWSYNTAX_ID syntaxID;
NWDS_FILTER_CURSOR NWFAR *cur = NULL;
/*-------------------------------------------------------------------
** Allocate a buffer to restrict our search to only certain attributes.
*/
cCode = NWDSAllocBuf(
/* Buffer Size */ DEFAULT_MESSAGE_LEN,
/* Buff. Point.*/ &attrNames
);
if (cCode < 0)
{
printf("NWDSAllocBuf returned: %04X\n", cCode);
goto Out;
}
/*------------------------------------------------------------
** Allocate a buffer to contain the search expression
*/
cCode = NWDSAllocBuf(
/* Buffer Size */ DEFAULT_MESSAGE_LEN,
/* Buff. Point.*/ &searchFilter
);
if (cCode < 0)
{
printf("NWDSAllocBuf returned: %04X\n", cCode);
goto Out;
}
/*------------------------------------------------------------
** Initialize the searchFilter buffer
*/
cCode = NWDSInitBuf(
/* Context */ dContext,
/* Operation*/ DSV_SEARCH_FILTER,
/* buffer */ searchFilter
);
if (cCode < 0)
{
printf("NWDSInitBuf returned: %04X\n", cCode);
goto Out;
}
/*------------------------------------------------------------
** Allocate a filter cursor to put the search expression
*/
cCode = NWDSAllocFilter(
/* Cursor */ &cur
);
if (cCode < 0)
{
printf("NWDSAllocFilter returned: %04X\n", cCode);
goto Out;
}
/*------------------------------------------------------------
** Now we can build the expression tree.
** Filter on Object Class of "User".
*/
cCode = NWDSAddFilterToken(
/* Cursor (exp tree) */ cur,
/* TOKEN */ FTOK_RDN,
/* Name or Value */ NULL,
/* Syntax ID */ 0
);
if (cCode < 0)
{
printf("NWDSAddFilterToken1 returned: %04X\n", cCode);
goto Out;
}
cCode = NWDSAddFilterToken(
/* Cursor (exp tree) */ cur,
/* TOKEN */ FTOK_ANAME,
/* Name or Value */ commonName,
/* Syntax ID */ SYN_CI_STRING
);
if (cCode < 0)
{
printf("NWDSAddFilterToken returned: %04X\n", cCode);
goto Out;
}
cCode = NWDSAddFilterToken(
/* Cursor (exp tree) */ cur,
/* TOKEN */ FTOK_END,
/* Name or Value */ NULL,
/* Syntax ID */ 0
);
if (cCode < 0)
{
printf("NWDSAddFilterToken returned: %04X\n", cCode);
goto Out;
}
/*-------------------------------------------------------------
** The expression is complete! Put the filter expression
** into the filter buffer. The freeVal function pointer
** would free the attribute values, but we allocated the
** values on the stack, so pass NULL.
*/
cCode = NWDSPutFilter(
/* Context Handle */ dContext,
/* Input Buffer */ searchFilter,
/* Cursor Pointer */ cur,
/* Free val. func */ NULL
);
if (cCode < 0)
{
printf("NWDSAddFilterToken returned: %04X\n", cCode);
goto Out;
}
else
cur = NULL; /* so we know at the bottom of this routine to free or not */
/*-------------------------------------------------------------
** Allocate a buffer to receive the results.
*/
cCode = NWDSAllocBuf(
/* Buffer Size */ DEFAULT_MESSAGE_LEN,
/* Buff. Point.*/ &retBuf
);
if (cCode < 0)
{
printf("NWDSAllocBuf returned: %04X\n", cCode);
goto Out;
}
do
{
/*---------------------------------------------------------
** Finally! we have everything we need to search. Note that
** the base object specified here is "[Root]". The base
** object could be an OU. This will initiate the search from
** that OU on down the tree.
*/
cCode = NWDSSearch(
/* Context handle */ dContext,
/* Base Obj Name */ "[Root]",
/* Scope */ DS_SEARCH_SUBTREE,
/* Search Alias? */ searchAliases,
/* Search Filter */ searchFilter,
/* Info Type */ 0,
/* All Attrib's ? */ FALSE,
/* Attrib. names */ NULL,
/* Iter. Handle */ &iterHandle,
/* cnt Obj's 2 sch*/ cntObjectsToSearch,
/* cnt obj's schd */ &cntObjectsSearched,
/* Object returned*/ retBuf
);
if (cCode < 0)
{
printf("NWDSSearch returned: %04X\n", cCode);
goto Out;
}
ActOnData(retBuf);
}
while (iterHandle != (NWDS_ITERATION)-1);
Out:
if (retBuf)
NWDSFreeBuf(retBuf);
if (cur)
NWDSFreeFilter(cur, NULL);
if (searchFilter)
NWDSFreeBuf(searchFilter);
if (attrNames)
NWDSFreeBuf(attrNames);
return(0);
}
/****************************************************************************
** Function: int ActOnData(NWDS_BUFFER NWFAR *buffer)
** The buffer returned from NWDSSearch() is passed to this function. This
** buffer contains all of the object/attribute information that met the
** criteria of the search. This function reads this information from the
** buffer.
*/
int ActOnData(NWDS_BUFFER NWFAR *buffer)
{
NWDSCCODE dscCode;
NWCCODE cCode;
NWCOUNT attrCount, attrValCount, objectCount;
NWOBJECT_INFO objectInfo;
static char objectName[MAX_DN_CHARS + 1];
char shortName[48];
int err=1,
i,
j,
k;
NWNUMBER maxConnections;
NWSIZE attrValSize;
void *attrVal;
char attrName[MAX_SCHEMA_NAME_CHARS + 1];
NWSYNTAX_ID syntaxID;
dscCode = NWDSGetObjectCount(
/* Req. Context */ dContext,
/* P. to read buf.*/ buffer,
/* Num. of OBJs */ &objectCount
);
if (dscCode < 0)
{
printf("\nNWDSGetObjectCount returned : %04X\n", dscCode);
return(err);
}
for (i = 0; i < objectCount; i++)
{
dscCode = NWDSGetObjectName(
/* Req. Context */ dContext,
/* P. to result buf */ buffer,
/* object name */ objectName,
/* Attribute count */ &attrCount,
/* object Info */ &objectInfo
);
if (dscCode < 0)
{
printf("\nNWDSGetObjectName returned: %04X\n", dscCode);
return(dscCode);
}
printf("\nObject Name: %s", objectName);
} /* end of objects loop */
return(0);
}