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 >
Wrap
Text File
|
1994-09-08
|
11KB
|
369 lines
/****************************************************************************
** File: XTRU1.C
**
** Desc: Sample code to add a trustee to a directory in the NetWare 4.x env.
**
** 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.
**
**
** QMK386 options used:
**
** /id NDS (Directory Services) symbols.
**
** Programmers:
**
** Ini Who Firm
** -----------------------------------------------------------------------
** ABJ Adam B. Jerome Novell Developer Support.
**
** History:
**
** When Who What
** -----------------------------------------------------------------------
** 09-06-94 ABJ First code.
*/
/****************************************************************************
** Include headers, function prototypes, macros, etc.
*/
/*------------------------------------------------------------------------
** ANSI
*/
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <string.h>
#include <conio.h> /* getch() */
#include <errno.h> /* ESUCCESS */
#include <ctype.h> /* islower() */
/*------------------------------------------------------------------------
** NetWare NLM
*/
#include <nwdsapi.h> /* NWDSCreateContext() NWDSAuthenticate() */
#include <nwdsdc.h> /* NWDSCreateContext() */
#include <nwdsdsa.h> /* NWDSAuthenticate() */
#include <nwconn.h> /* GetConnectionNumber()*/
#include <nwdir.h> /* AddTrustee() */
#include <niterror.h>/* ERR_NO_MODIFY_PRIVILEGES, ERR_NO_SUCH_BINDERY_OBJECT */
#include <nwcntask.h>/* ReturnConnection() */
#include <nwmisc.h> /* LongSwap() */
/*------------------------------------------------------------------------
** This module.
*/
#define STREDIT_NOFLAGS 0x00000000 /* Just to be thourough */
#define STREDIT_NEW 0x00000001 /* Zero's out the strBuf before editing.*/
#define STREDIT_NOECHO 0x00000002 /* Does not echo to the screen. */
#define STREDIT_ECHOAS 0x00000004 /* Echos astricts. */
#define STREDIT_UPPER 0x00000008 /* Upper-case alpha. */
#define STREDIT_ERR_TOOLONG (-1)
#define STREDIT_KEY_ESC 27
#define STREDIT_KEY_ENTER 13
#define STREDIT_KEY_BACKSPACE 8
/****************************************************************************
** Edit String function.
**
** Parameters:
**
** strBuf [INPUT/OUTPUT] String to edit.
**
** maxStrLen [INPUT] Maximum length of strBuf including ZERO
** terminator.
**
** flags [INPUT] Editing flags; including:
**
** STREDIT_NEW Zero's out the strBuf before editing.
** STREDIT_NOECHO Does not echo to the screen.
** STREDIT_ECHOAS Echos astricts.
** STREDIT_UPPER Upper-case only.
**
** return Value: STREDIT_ERR_TOOLONG Initial string is longer than the
** specified maxStrLen.
**
** STREDIT_KEY_ESC User pressed the ESCAPE key to
** terminate the edit process.
**
** STREDIT_KEY_ENTER User pressed the ENTER key to
** terminate the edit process.
*/
int StringEdit(char *strBuf, int maxStrLen, int flags, char *prompt)
{
int ch;
/*------------------------------------------------------------------------
** New buffer?
*/
if(flags & STREDIT_NEW) memset(strBuf, 0x00, maxStrLen);
/*------------------------------------------------------------------------
** Check args.
*/
if(strlen(strBuf) > maxStrLen - 1) return(STREDIT_ERR_TOOLONG);
/*------------------------------------------------------------------------
** Print the prompt & current strBuf value..
*/
if(prompt != NULL) printf("%s%s", prompt, strBuf);
/*------------------------------------------------------------------------
** Handle keyboard.
*/
while(1)
{
ch=getch(); if(!ch) ch=getch() + 256;
if((ch < 32) || (ch == 127) || (ch > 255)) /* Control chars & Function Keys */
{
switch(ch)
{
case STREDIT_KEY_ESC:
case STREDIT_KEY_ENTER:
printf("\n");
return(ch);
case STREDIT_KEY_BACKSPACE:
if(strlen(strBuf) == 0) continue;
strBuf[strlen(strBuf)-1] = '\0';
printf("\b \b");
continue;
}
continue;
}
else if(ch <= 126) /* -Normal- characters */
{
if(strlen(strBuf) == (maxStrLen -1)) continue;
if(flags & STREDIT_UPPER) if(islower(ch)) ch -= 32;
strBuf[strlen(strBuf)+1] = '\0';
strBuf[strlen(strBuf)] = (char)ch;
if(!(flags & STREDIT_NOECHO))
{
if(flags & STREDIT_ECHOAS) printf("*");
else printf("%c", (char)ch);
}
continue;
}
else continue; /* Extended chars from 128 - 255 */
}
}
/****************************************************************************
** Program start.
*/
void main(void)
{
NWDSContextHandle contextHandle;
NWDSCCODE loggedIn;
NWDSCCODE attached;
NWDSCCODE cCode;
char operatorUserName[47+1];
char operatorPassWord[47+1];
char contextName[255+1];
char targetObjectName[47+1];
uint32 targetObjectID;
char trusteeServerName[47+1];
WORD trusteeServerID;
char trusteePathName[255+1];
NWCONN_HANDLE trusteeConnNumber;
/*------------------------------------------------------------------------
** Get a context handle.
*/
contextHandle = NWDSCreateContext();
if((signed)contextHandle < 0) switch(contextHandle)
{
case ERR_CONTEXT_CREATION:
printf("ERROR: NWDSCreateContext() indicated a Context Creation Error.\n");
goto END;
default:
printf("ERROR: NWDSCreateContext() indicated an unknown error: %ld\n", contextHandle);
goto END;
}
/*------------------------------------------------------------------------
** Get operator's name and password.
*/
printf("Please authenticate yourself:\n");
StringEdit(operatorUserName, sizeof(operatorUserName), STREDIT_NEW, " Enter login name: ");
StringEdit(operatorPassWord, sizeof(operatorPassWord), STREDIT_NEW|STREDIT_NOECHO, " Password: ");
printf("\n");
/*------------------------------------------------------------------------
** Authenticate.
*/
loggedIn = NWDSLogin(
/* I- context */ contextHandle,
/* I- optionsFlag */ NULL,
/* I- objectName */ operatorUserName,
/* I- password */ operatorPassWord,
/* I- validityPeriod */ 0
);
switch(loggedIn)
{
case 0:
break;
default:
printf("ERROR: NWDSLogin() indicated an unknown error: %ld\n", loggedIn);
goto END;
}
/*------------------------------------------------------------------------
** Get tartet user's name.
*/
printf("Target object info:\n");
StringEdit(contextName, sizeof(contextName), STREDIT_NEW, " Enter target context name: ");
StringEdit(targetObjectName, sizeof(targetObjectName), STREDIT_NEW, " Enter target user's name: ");
printf("\n");
/*------------------------------------------------------------------------
** The server that I am running this on is in the LNC context.
** The server also has a replica on it.
*/
cCode = NWDSSetContext(
/* I- context */ contextHandle,
/* I- key */ DCK_NAME_CONTEXT,
/* I- value */ contextName
);
if(cCode < 0) switch(cCode)
{
default:
printf("ERROR: NWDSSetContext() returned unknown error: %d", cCode);
goto END;
}
/*------------------------------------------------------------------------
** Attach us to the file server on which we will manipulate trustee info.
*/
printf("Target path info:\n");
StringEdit(trusteeServerName, sizeof(trusteeServerName), STREDIT_NEW|STREDIT_UPPER, " File server name: ");
attached=AttachToFileServer(
/* I- fileServerName */ trusteeServerName,
/* I- filsServerID */ &trusteeServerID
);
if(attached) switch(NetWareErrno)
{
default:
printf("ERROR: AttachToFileServer() returned unknown error: %d\n", NetWareErrno);
goto END;
}
/*------------------------------------------------------------------------
** What's our connection number?
*/
trusteeConnNumber = GetConnectionNumber();
/*------------------------------------------------------------------------
** Have DS authenticate our connection to the server.
*/
cCode=NWDSAuthenticate(
/* I- connID */ trusteeConnNumber,
/* I- optionsFlags */ 0,
/* I- sessionKey */ NULL
);
switch(cCode)
{
case 0:
break;
default:
printf("ERROR: NWDSAuthenticate() returned error: %d\n");
goto END;
}
/*------------------------------------------------------------------------
** Get the target object's ID.
*/
cCode=NWDSMapNameToID(
/* I- context */ contextHandle,
/* I- connID */ trusteeConnNumber,
/* I- object */ targetObjectName,
/* -O objectID */ &targetObjectID
);
if(cCode < 0) switch(cCode)
{
case (-333):
printf("ERROR: NWDSMapNameToID() reports \"No Connection.\"\n");
goto END;
default:
printf("ERROR: NWDSMapNameToID() returned unknown error: %d\n", cCode);
goto END;
}
/*------------------------------------------------------------------------
** Get target directory and/or file path.
*/
StringEdit(trusteePathName, sizeof(trusteePathName), STREDIT_NEW|STREDIT_UPPER, " Grant rights to (vol:dir\\...): ");
printf("\n");
/*------------------------------------------------------------------------
** Add trustee to sys:root.
**
** Add trustee, being an older function, requires that the trusteeObjectID
** be long-swapped from the newer DS functions.
*/
cCode=AddTrustee(
/* I- pathName */ trusteePathName,
/* I- trusteeObjectID */ LongSwap(targetObjectID),
/* I- trusteeRightsMask */ TA_READ|TA_SEEFILES
);
switch(cCode)
{
case ESUCCESS:
break;
case BADSERVERNAME:
printf("ERROR: AddTrustee() reports \"Bad server name.\"\n");
goto END;
case ERR_INVALID_PATH:
printf("ERROR: AddTrustee() reports \"Invalid Path.\": [%s]\n", trusteePathName);
goto END;
case ERR_NO_MODIFY_PRIVILEGE:
printf("ERROR: AddTrustee() reports \"No modify privileges.\"\n");
goto END;
case ERR_NO_SUCH_BINDERY_OBJECT:
printf("ERROR: AddTrustee() reports \"No modify privileges.\"\n");
goto END;
default:
printf("ERROR: AddTrustee() returned unknown error: %d\n", cCode);
goto END;
}
printf("Success\n");
END:
if(attached == 0) ReturnConnection(trusteeConnNumber);
if(loggedIn >= 0) NWDSLogout(contextHandle);
if((signed)contextHandle >= 0) NWDSFreeContext(contextHandle);
return;
}