home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / LMUTIL.ZIP / ISMEMBER.C < prev    next >
C/C++ Source or Header  |  1991-03-19  |  5KB  |  197 lines

  1. /*
  2. ** Program: ISMEMBER
  3. **
  4. ** Author:  Roger L Soles
  5. **
  6. ** Description:
  7. **
  8. ** This example C program uses the NetUserGetGroups call to obtain
  9. ** information to determine whether the user is a member of a group
  10. ** or not.
  11. **
  12. ** Instructions:
  13. **
  14. ** ISMEMBER [-?q] [GROUP_NAME]
  15. **
  16. ** Errorlevel = 0, user is a member
  17. ** Errorlevel = 1, user is not a member
  18. **
  19. ** if '-q' is not specified, command will execute in verbose mode
  20. **    (it will provide messages on the screen).
  21. **
  22. **
  23. ** if no GROUP_NAME is specified, a list of groups is the current
  24. ** user is a member of is diplayed.
  25. **
  26. */
  27.  
  28. #define LINT_ARGS
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #define INCL_NETWKSTA
  33. #define INCL_NETGROUP
  34. #define INCL_NETERRORS
  35. #include <lan.h>
  36. #include "getsrv.h"
  37.  
  38. void main (int argc, char * argv[])
  39. {
  40.  
  41.    char far * cptr;
  42.    int qmode = 0;
  43.  
  44.    API_RET_TYPE uRetCode = 0;
  45.  
  46.    struct group_info_0 far * user_groups;
  47.  
  48.    char far * pszServer;
  49.    char far * pszGroupName;
  50.    char far * pszUserName;
  51.    short sLevel = 0;
  52.    char far * pbBuffer;
  53.    unsigned short cbBuffer = 0;
  54.    unsigned short pcEntriesRead = 0;
  55.    unsigned short pcTotalAvail = 0;
  56.  
  57.    argv++; argc--;
  58.  
  59.    cptr = argv[0];
  60.    if ( *cptr++ == '-' )
  61.    {
  62.       switch (*cptr)
  63.       {
  64.       case 'q':
  65.       case 'Q':
  66.          /*
  67.          ** quite mode inhibits messages -- rather uses only
  68.          ** the error level
  69.          */
  70.          qmode = 1;
  71.          break;
  72.       case '?':
  73.       default:
  74.          printf ("\nUSAGE:  ISMEMBER [-?q] [GROUP_NAME]\n");
  75.          exit (1);
  76.       }
  77.  
  78.       argv++; argc--;
  79.    }
  80.  
  81.    if (argc != 1)
  82.    {
  83.       pszGroupName = NULL;
  84.    }
  85.    else
  86.    { 
  87.       /*
  88.       ** Group Name
  89.       */
  90.       strupr (argv[0]);
  91.       pszGroupName = argv[0];
  92.    }
  93.  
  94.    /*
  95.    ** Get Logon Server & User Name
  96.    */
  97.    if ( get_logon_server (&pszServer, &pszUserName) != 0 )
  98.    {
  99.       printf ("Error Obtaining Logon Server/User Name\n");
  100.       exit (1);
  101.    }
  102.  
  103.    /*
  104.    ** Determine how large buffer needs to be
  105.    */
  106.    uRetCode = NetUserGetGroups ( pszServer,
  107.                                  pszUserName,
  108.                                  sLevel,
  109.                                  NULL,
  110.                                  0,
  111.                                  &pcEntriesRead,
  112.                                  &pcTotalAvail);
  113.  
  114.    /*
  115.    ** If call fails with an error other than these, that
  116.    ** means the user is not a member and does not have account
  117.    ** priviledges!
  118.    **
  119.    ** If call succeeds, it means the user is a member, or has
  120.    ** account priviledges.
  121.    */
  122.    if ( (uRetCode == NERR_BufTooSmall) || (uRetCode == ERROR_MORE_DATA) )
  123.    {
  124.       cbBuffer = sizeof(struct group_info_0) * pcTotalAvail;
  125.       pbBuffer = (char *) malloc (cbBuffer);
  126.    }
  127.    else
  128.    {
  129.       if (qmode == 0)
  130.       {
  131.          printf ("%Fs is not a member of group %Fs\n", pszUserName, pszGroupName);
  132.       }
  133.       exit (1);
  134.    }
  135.  
  136.    /*
  137.    ** Get the list of users in this group
  138.    */
  139.    uRetCode = NetUserGetGroups ( pszServer,
  140.                                  pszUserName,
  141.                                  sLevel,
  142.                                  pbBuffer,
  143.                                  cbBuffer,
  144.                                  &pcEntriesRead,
  145.                                  &pcTotalAvail);
  146.  
  147.    if (uRetCode != NERR_Success)
  148.    {
  149.       if (qmode == 0)
  150.       {
  151.          printf ("%Fs is not a member of group %Fs\n", pszUserName, pszGroupName);
  152.       }
  153.       exit (1);
  154.    }
  155.  
  156.    user_groups = (struct group_info_0 far *) pbBuffer;
  157.  
  158.    /*
  159.    ** Check to see if this user is a member of this group/print groups
  160.    */
  161.    if ( pszGroupName == NULL)
  162.    {
  163.       printf ("%Fs is a member of:\n", pszUserName);
  164.    }
  165.  
  166.    while (pcTotalAvail != 0)
  167.    {
  168.       if ( pszGroupName != NULL)
  169.       {
  170.          if ( strcmp (user_groups->grpi0_name, pszGroupName ) == 0 )
  171.          {
  172.             if (qmode == 0)
  173.             {
  174.                printf ("%Fs is a member of group %Fs\n", pszUserName, pszGroupName);
  175.             }
  176.             exit (0);
  177.          }
  178.       }
  179.       else
  180.       {
  181.          printf ("        %Fs\n", user_groups->grpi0_name);
  182.       }
  183.  
  184.       pcTotalAvail--;
  185.       user_groups++;
  186.    
  187.    }
  188.  
  189.    if ( (qmode == 0) && (pszGroupName != NULL) )
  190.    {
  191.       printf ("%Fs is not a member of group %Fs\n", pszUserName, pszGroupName);
  192.    }
  193.  
  194.    exit (1);
  195. }
  196.  
  197.