home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
LMUTIL.ZIP
/
ISMEMBER.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-03-19
|
5KB
|
197 lines
/*
** Program: ISMEMBER
**
** Author: Roger L Soles
**
** Description:
**
** This example C program uses the NetUserGetGroups call to obtain
** information to determine whether the user is a member of a group
** or not.
**
** Instructions:
**
** ISMEMBER [-?q] [GROUP_NAME]
**
** Errorlevel = 0, user is a member
** Errorlevel = 1, user is not a member
**
** if '-q' is not specified, command will execute in verbose mode
** (it will provide messages on the screen).
**
**
** if no GROUP_NAME is specified, a list of groups is the current
** user is a member of is diplayed.
**
*/
#define LINT_ARGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INCL_NETWKSTA
#define INCL_NETGROUP
#define INCL_NETERRORS
#include <lan.h>
#include "getsrv.h"
void main (int argc, char * argv[])
{
char far * cptr;
int qmode = 0;
API_RET_TYPE uRetCode = 0;
struct group_info_0 far * user_groups;
char far * pszServer;
char far * pszGroupName;
char far * pszUserName;
short sLevel = 0;
char far * pbBuffer;
unsigned short cbBuffer = 0;
unsigned short pcEntriesRead = 0;
unsigned short pcTotalAvail = 0;
argv++; argc--;
cptr = argv[0];
if ( *cptr++ == '-' )
{
switch (*cptr)
{
case 'q':
case 'Q':
/*
** quite mode inhibits messages -- rather uses only
** the error level
*/
qmode = 1;
break;
case '?':
default:
printf ("\nUSAGE: ISMEMBER [-?q] [GROUP_NAME]\n");
exit (1);
}
argv++; argc--;
}
if (argc != 1)
{
pszGroupName = NULL;
}
else
{
/*
** Group Name
*/
strupr (argv[0]);
pszGroupName = argv[0];
}
/*
** Get Logon Server & User Name
*/
if ( get_logon_server (&pszServer, &pszUserName) != 0 )
{
printf ("Error Obtaining Logon Server/User Name\n");
exit (1);
}
/*
** Determine how large buffer needs to be
*/
uRetCode = NetUserGetGroups ( pszServer,
pszUserName,
sLevel,
NULL,
0,
&pcEntriesRead,
&pcTotalAvail);
/*
** If call fails with an error other than these, that
** means the user is not a member and does not have account
** priviledges!
**
** If call succeeds, it means the user is a member, or has
** account priviledges.
*/
if ( (uRetCode == NERR_BufTooSmall) || (uRetCode == ERROR_MORE_DATA) )
{
cbBuffer = sizeof(struct group_info_0) * pcTotalAvail;
pbBuffer = (char *) malloc (cbBuffer);
}
else
{
if (qmode == 0)
{
printf ("%Fs is not a member of group %Fs\n", pszUserName, pszGroupName);
}
exit (1);
}
/*
** Get the list of users in this group
*/
uRetCode = NetUserGetGroups ( pszServer,
pszUserName,
sLevel,
pbBuffer,
cbBuffer,
&pcEntriesRead,
&pcTotalAvail);
if (uRetCode != NERR_Success)
{
if (qmode == 0)
{
printf ("%Fs is not a member of group %Fs\n", pszUserName, pszGroupName);
}
exit (1);
}
user_groups = (struct group_info_0 far *) pbBuffer;
/*
** Check to see if this user is a member of this group/print groups
*/
if ( pszGroupName == NULL)
{
printf ("%Fs is a member of:\n", pszUserName);
}
while (pcTotalAvail != 0)
{
if ( pszGroupName != NULL)
{
if ( strcmp (user_groups->grpi0_name, pszGroupName ) == 0 )
{
if (qmode == 0)
{
printf ("%Fs is a member of group %Fs\n", pszUserName, pszGroupName);
}
exit (0);
}
}
else
{
printf (" %Fs\n", user_groups->grpi0_name);
}
pcTotalAvail--;
user_groups++;
}
if ( (qmode == 0) && (pszGroupName != NULL) )
{
printf ("%Fs is not a member of group %Fs\n", pszUserName, pszGroupName);
}
exit (1);
}