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

  1. /*
  2. ** Program: RIGHTS
  3. **
  4. ** Author:  Roger L Soles
  5. **
  6. ** Description:
  7. **
  8. ** This example C program uses the NetAccessGetUserPerms to obtain
  9. ** information to determine rights in a directory.
  10. **
  11. ** Instructions:
  12. **
  13. ** RIGHTS [-?qgrwcxdapf] [PATH_NAME]
  14. **
  15. **   ?  help
  16. **
  17. **   q  quite - errorlevel information only
  18. **
  19. **   g  group membership establishes rights
  20. **
  21. **   r  read
  22. **
  23. **   w  write
  24. **
  25. **   c  create
  26. **
  27. **   x  execute
  28. **
  29. **   d  delete
  30. **
  31. **   a  attributes
  32. **
  33. **   p  permissions
  34. **
  35. **   f  full (r-p)
  36. **
  37. */
  38.  
  39. #define LINT_ARGS
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <direct.h>
  44. #define INCL_NETACCESS
  45. #define INCL_NETWKSTA
  46. #define INCL_NETERRORS
  47. #include <lan.h>
  48. #include "getsrv.h"
  49.  
  50. void main (int argc, char * argv[])
  51. {
  52.  
  53.    char far * cptr;
  54.    int qmode = 0;
  55.    unsigned short dPerms = 0x00;
  56.  
  57.    API_RET_TYPE uRetCode = 0;
  58.  
  59.    char far * pszServer;
  60.    char far * pszPathName;
  61.    char far * pszUserName;
  62.    unsigned short pusPerms;
  63.  
  64.    argv++; argc--;
  65.  
  66.    cptr = argv[0];
  67.  
  68.    if ( *cptr++ == '-' )
  69.    {
  70.       strupr (cptr);
  71.  
  72.       while (*cptr != 0)
  73.       {
  74.          switch (*cptr)
  75.          {
  76.             /*
  77.             ** Enumerated permissions
  78.             */
  79.             case 'G':
  80.                dPerms |= ACCESS_GROUP;
  81.                break;
  82.             case 'R':
  83.                dPerms |= ACCESS_READ;
  84.                break;
  85.             case 'W':
  86.                dPerms |= ACCESS_WRITE;
  87.                break;
  88.             case 'C':
  89.                dPerms |= ACCESS_CREATE;
  90.                break;
  91.             case 'X':
  92.                dPerms |= ACCESS_EXEC;
  93.                break;
  94.             case 'D':
  95.                dPerms |= ACCESS_DELETE;
  96.                break;
  97.             case 'A':
  98.                dPerms |= ACCESS_ATRIB;
  99.                break;
  100.             case 'P':
  101.                dPerms |= ACCESS_PERM;
  102.                break;
  103.             case 'F':
  104.                dPerms |= 0x7f;
  105.                break;
  106.  
  107.             case 'Q':
  108.             /*
  109.             ** quite mode inhibits messages -- rather uses only
  110.             ** the error level
  111.             */
  112.                qmode = 1;
  113.                break;
  114.  
  115.             case '?':
  116.             default:
  117.                printf ("\nUSAGE:  RIGHTS [-?qgrwcxdapf] [PATH_NAME]\n");
  118.                exit (1);
  119.          }
  120.          cptr++;
  121.       }
  122.  
  123.       argv++; argc--;
  124.    }
  125.  
  126.    /*
  127.    ** Path Name
  128.    */
  129.    if (argc != 1)
  130.    {
  131.       getcwd(pszPathName, 128);
  132.    }
  133.    else
  134.    {
  135.       strupr (argv[0]);
  136.       pszPathName = argv[0];
  137.    }
  138.  
  139.    /*
  140.    ** Get Logon Server & User Name
  141.    */
  142.    if ( get_logon_server (&pszServer, &pszUserName) != 0 )
  143.    {
  144.       printf ("Error Obtaining Logon Server/User Name\n");
  145.       exit (1);
  146.    }
  147.  
  148.    /*
  149.    ** Get the rights
  150.    */
  151.    uRetCode = NetAccessGetUserPerms ( pszServer,
  152.                                       pszUserName,
  153.                                       pszPathName,
  154.                                       &pusPerms);
  155.  
  156.    if (uRetCode != NERR_Success)
  157.    {
  158.       if (qmode == 0)
  159.       {
  160.          printf ("%Fs has insufficient rights in %Fs\n", pszUserName, pszPathName);
  161.          exit (1);
  162.       }
  163.       else
  164.       {
  165.          /*
  166.          ** No Rights!
  167.          */
  168.          pusPerms = 0x00;
  169.       }
  170.    }
  171.  
  172.  
  173. #define BITTEST(x, y) ( (x & y) == y)
  174.  
  175.    if (qmode == 0)
  176.    {
  177.       printf ("%Fs rights in %Fs = ", pszUserName, pszPathName);
  178.       if ( BITTEST(pusPerms, ACCESS_GROUP) )
  179.       {
  180.          printf ("G:");
  181.       }
  182.       if ( BITTEST(pusPerms, ACCESS_READ) )
  183.       {
  184.          printf ("R");
  185.       }
  186.       if ( BITTEST(pusPerms, ACCESS_WRITE) )
  187.       {
  188.          printf ("W");
  189.       }
  190.       if ( BITTEST(pusPerms, ACCESS_CREATE) )
  191.       {
  192.          printf ("C");
  193.       }
  194.       if ( BITTEST(pusPerms, ACCESS_EXEC) )
  195.       {
  196.          printf ("X");
  197.       }
  198.       if ( BITTEST(pusPerms, ACCESS_DELETE) )
  199.       {
  200.          printf ("D");
  201.       }
  202.       if ( BITTEST(pusPerms, ACCESS_ATRIB) )
  203.       {
  204.          printf ("A");
  205.       }
  206.       if ( BITTEST(pusPerms, ACCESS_PERM) )
  207.       {
  208.          printf ("P");
  209.       }
  210.       printf ("\n");
  211.       exit (0);
  212.    }
  213.    else
  214.    {
  215.       if ( BITTEST(pusPerms, dPerms) )
  216.       {
  217.          exit (0);
  218.       }
  219.       exit (1);
  220.    }
  221. }
  222.  
  223.