home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / security / winnt / sd_flppy / chgflpsd.c next >
C/C++ Source or Header  |  1997-10-05  |  6KB  |  226 lines

  1. ////////////////////////////////////////////////////////
  2. //
  3. //  Client.c --
  4. //
  5. //      This program is a command line oriented
  6. //      demonstration of the FloppyLocker service
  7. //      sample, aka floplock.exe
  8. //
  9. //      Copyright 1992-1997, Microsoft Corp.  All Rights Reserved
  10.  
  11. /****************************************************************************\
  12. *  INCLUDES, DEFINES, TYPEDEFS
  13. \****************************************************************************/
  14. #define STRICT
  15. #include <windows.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19.  
  20. #define PERR(api) printf("\n%s: Error %d from %s on line %d",  \
  21.     __FILE__, GetLastError(), api, __LINE__);
  22. #define PMSG(msg) printf("\n%s line %d: %s",  \
  23.     __FILE__, __LINE__, msg);
  24.  
  25. /****************************************************************************\
  26. * GLOBAL VARIABLES
  27. \****************************************************************************/
  28.  
  29. #define              SZ_NAME_BUF 30
  30. UCHAR   ucMchNameBuf[SZ_NAME_BUF] = "";
  31. LPTSTR  lpszMchName = (LPTSTR)&ucMchNameBuf;
  32. UCHAR   ucOperation;
  33.  
  34. /****************************************************************************\
  35. * FUNCTION PROTOTYPES
  36. \****************************************************************************/
  37.  
  38. BOOL CrackArgs(UINT argc, char *argv[]);
  39. VOID DisplayHelp(VOID);
  40.  
  41. int main(int argc, char *argv[])
  42. {
  43.   char    inbuf[180];
  44.   char    outbuf[180];
  45.   DWORD   bytesRead;
  46.   BOOL    ret;
  47.   #define               SZ_PIPE_NAME_BUF 50
  48.   UCHAR   ucPipeNameBuf[SZ_PIPE_NAME_BUF] = "";
  49.   LPTSTR  lpszPipeName = (LPTSTR)&ucPipeNameBuf;
  50.  
  51.   // check if Win32s, if so, display notice and terminate
  52.       if( GetVersion() & 0x80000000 )
  53.       {
  54.         MessageBox( NULL,
  55.            "This application cannot run on Windows 3.1 or Windows 95.\n"
  56.            "This application will now terminate.",
  57.            "SD_FLPPY",
  58.            MB_OK | MB_ICONSTOP | MB_SETFOREGROUND );
  59.         return( 1 );
  60.       }
  61.  
  62.   if (!CrackArgs(argc,argv))
  63.     exit(1);
  64.  
  65.   strcpy(lpszPipeName,lpszMchName);
  66.   strcat(lpszPipeName,"\\pipe\\sd_flppy");
  67.  
  68.   outbuf[0] = ucOperation;
  69.   outbuf[1] = '\0';
  70.  
  71.   ret = CallNamedPipe(lpszPipeName,
  72.                       outbuf,
  73.                       sizeof(outbuf),
  74.                       inbuf,
  75.                       sizeof(inbuf),
  76.                       &bytesRead,
  77.                       NMPWAIT_WAIT_FOREVER);
  78.  
  79.   if (!ret)
  80.   { if (ERROR_ACCESS_DENIED == GetLastError())
  81.     { printf("\nAccess denied\n");
  82.     }
  83.     else if (ERROR_BAD_NETPATH == GetLastError())
  84.     { printf("\nMachine %s not found\n",lpszMchName);
  85.     }
  86.     else
  87.     { PERR("CallNamedPipe");
  88.     }
  89.     exit(1);
  90.   }
  91.  
  92.   printf("\n%s %s\n",lpszMchName,inbuf);
  93. }
  94.  
  95. /****************************************************************************\
  96. *
  97. * FUNCTION: CrackArgs
  98. *
  99. \****************************************************************************/
  100.  
  101. BOOL CrackArgs(UINT argc, char *argv[])
  102. {
  103.   char *p;
  104.  
  105.   /**************************************************************************\
  106.   *
  107.   * There must be two arguments
  108.   *
  109.   \**************************************************************************/
  110.  
  111.   if (argc != 3)
  112.   { DisplayHelp();
  113.     return(FALSE);
  114.   }
  115.  
  116.   p=argv[1];
  117.  
  118.   /**************************************************************************\
  119.   *
  120.   * The machine name argument must be 3 to (SZ_NAME_BUF-3) chars long
  121.   *
  122.   \**************************************************************************/
  123.  
  124.   if ((strlen(p) < 3) || (strlen(p) > (SZ_NAME_BUF-3) ))
  125.   { DisplayHelp();
  126.     return(FALSE);
  127.   }
  128.  
  129.   /**************************************************************************\
  130.   *
  131.   * The first two chars in the machine name argument must be \
  132.   *
  133.   \**************************************************************************/
  134.  
  135.   if ('\\' != *p)
  136.   { DisplayHelp();
  137.     return(FALSE);
  138.   }
  139.   if ('\\' != *(p+1))
  140.   { DisplayHelp();
  141.     return(FALSE);
  142.   }
  143.  
  144.   /**************************************************************************\
  145.   *
  146.   * Set up the machine name
  147.   *
  148.   \**************************************************************************/
  149.  
  150.   strcpy(lpszMchName,"\\\\");
  151.   strcat(lpszMchName,(p+2));
  152.  
  153.  
  154.   p=argv[2];
  155.  
  156.   /**************************************************************************\
  157.   *
  158.   * The switch argument must be 2 chars long
  159.   *
  160.   \**************************************************************************/
  161.  
  162.   if (strlen(p) != 2)
  163.   { DisplayHelp();
  164.     return(FALSE);
  165.   }
  166.  
  167.   /**************************************************************************\
  168.   *
  169.   * The first char in the switch argument must be /
  170.   *
  171.   \**************************************************************************/
  172.  
  173.   if ('/' != *p)
  174.   { DisplayHelp();
  175.     return(FALSE);
  176.   }
  177.  
  178.   /**************************************************************************\
  179.   *
  180.   * Chars 2 must be U or L or Q
  181.   *
  182.   \**************************************************************************/
  183.  
  184.  
  185.   switch (*(p+1))
  186.   { case 'u':
  187.     case 'U':
  188.       ucOperation = 'U';
  189.       break;
  190.     case 'l':
  191.     case 'L':
  192.       ucOperation = 'L';
  193.       break;
  194.     case 'q':
  195.     case 'Q':
  196.       ucOperation = 'Q';
  197.       break;
  198.     default :
  199.       DisplayHelp();
  200.       return(FALSE);
  201.   }
  202.  
  203.   return(TRUE);
  204. }
  205.  
  206. /****************************************************************************\
  207. *
  208. * FUNCTION: DisplayHelp
  209. *
  210. \****************************************************************************/
  211.  
  212. VOID DisplayHelp(VOID)
  213. {
  214.   printf("\nusage: chgflpsd \\\\machinenam /switch");
  215.   printf("\n                  Values for /switch");
  216.   printf("\n                        /u      Unlock");
  217.   printf("\n                        /l      Lock");
  218.   printf("\n                        /q      Query\n");
  219.   printf("\nFor example to manage DACLs on machine \\\\flip5's floppies");
  220.   printf("\n       chgflpsd \\\\flip5 /u      Gives access to everyone");
  221.   printf("\n       chgflpsd \\\\flip5 /l      Gives access to only local admins");
  222.   printf("\n       chgflpsd \\\\flip5 /q      Query current access on \\\\flip5\n");
  223.  
  224.   return;
  225. }
  226.