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 / instsrv.c < prev    next >
C/C++ Source or Header  |  1996-04-18  |  12KB  |  360 lines

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define FORCE_SERVICE_NAME_TO_FLOPPYLOCK (1==0)
  7.  
  8. #define PERR(api) printf("\n%s: Error %d from %s on line %d",  \
  9.     __FILE__, GetLastError(), api, __LINE__);
  10. #define PMSG(msg) printf("\n%s line %d: %s",  \
  11.     __FILE__, __LINE__, msg);
  12.  
  13. #define MSG_FOR_ACCESS_DENIED "You aren't authorized to do this - please see your system Administrator"
  14. #define MSG_1_FOR_BAD_PATH "The fully qualified path name to the .exe must be given, and"
  15. #define MSG_2_FOR_BAD_PATH "  the drive letter must be for a fixed disk (e.g., not a net drive)"
  16.  
  17. SC_HANDLE schService;
  18. SC_HANDLE schSCManager;
  19.  
  20. VOID DisplayHelp(VOID);
  21.  
  22. VOID InstallService(LPCTSTR serviceName, LPCTSTR serviceExe)
  23. {
  24.   LPCTSTR lpszBinaryPathName = serviceExe;
  25.   LPTSTR  lpszRootPathName="?:\\";
  26.  
  27.   if ( (':' != *(lpszBinaryPathName+1)) || ('\\' != *(lpszBinaryPathName+2)) )
  28.   { printf("\n%s",MSG_1_FOR_BAD_PATH);
  29.     printf("\n%s\n",MSG_2_FOR_BAD_PATH);
  30.     return;
  31.   }
  32.  
  33.   #define DRIVE_TYPE_INDETERMINATE 0
  34.   #define ROOT_DIR_DOESNT_EXIST    1
  35.  
  36.   *lpszRootPathName = *(lpszBinaryPathName+0) ;
  37.  
  38.   switch (  GetDriveType(lpszRootPathName)  )
  39.   {
  40.     case DRIVE_FIXED :
  41.     { // OK
  42.       break;
  43.     }
  44.     case  ROOT_DIR_DOESNT_EXIST :
  45.     { printf("\n%s",MSG_1_FOR_BAD_PATH);
  46.       printf("\n  the root directory where the .exe is specified to be must exist, and");
  47.       printf("\n%s\n",MSG_2_FOR_BAD_PATH);
  48.       return;
  49.     }
  50.     case  DRIVE_TYPE_INDETERMINATE :
  51.     case  DRIVE_REMOVABLE          :
  52.     case  DRIVE_REMOTE             :
  53.     case  DRIVE_CDROM              :
  54.     case  DRIVE_RAMDISK            :
  55.     { printf("\n%s",MSG_1_FOR_BAD_PATH);
  56.       printf("\n%s\n",MSG_2_FOR_BAD_PATH);
  57.       return;
  58.     }
  59.     default :
  60.     { printf("\n%s",MSG_1_FOR_BAD_PATH);
  61.       printf("\n%s\n",MSG_2_FOR_BAD_PATH);
  62.       return;
  63.     }
  64.   }
  65.  
  66.   if (INVALID_HANDLE_VALUE == CreateFile(lpszBinaryPathName,
  67.                                          GENERIC_READ,
  68.                                          FILE_SHARE_READ,
  69.                                          NULL,
  70.                                          OPEN_EXISTING,
  71.                                          FILE_ATTRIBUTE_NORMAL,
  72.                                          NULL))
  73.   { printf("\n%s",MSG_1_FOR_BAD_PATH);
  74.     printf("\n  the file must exist, and");
  75.     printf("\n%s\n",MSG_2_FOR_BAD_PATH);
  76.     return;
  77.   }
  78.  
  79.   if (FORCE_SERVICE_NAME_TO_FLOPPYLOCK)
  80.   { schService = CreateService(
  81.         schSCManager,               // SCManager database
  82.         serviceName,                // name of service
  83.         serviceName,                // name to display (new parameter after october beta)
  84.         SERVICE_ALL_ACCESS,         // desired access
  85.         SERVICE_WIN32_OWN_PROCESS,  // service type
  86.         SERVICE_AUTO_START,         // start type
  87.         SERVICE_ERROR_NORMAL,       // error control type
  88.         lpszBinaryPathName,         // service's binary
  89.         NULL,                       // no load ordering group
  90.         NULL,                       // no tag identifier
  91.         NULL,                       // no dependencies
  92.         NULL,                       // Local System account
  93.         NULL);                      // null password
  94.   }
  95.   else
  96.   { schService = CreateService(
  97.         schSCManager,               // SCManager database
  98.         serviceName,                // name of service
  99.         serviceName,                // name to display (new parameter after october beta)
  100.         SERVICE_ALL_ACCESS,         // desired access
  101.         SERVICE_WIN32_OWN_PROCESS,  // service type
  102.         SERVICE_AUTO_START,         // start type
  103.         SERVICE_ERROR_NORMAL,       // error control type
  104.         lpszBinaryPathName,         // service's binary
  105.         NULL,                       // no load ordering group
  106.         NULL,                       // no tag identifier
  107.         NULL,                       // no dependencies
  108.         ".\\Administrator",         // Admin account
  109.         "");                        // likely incorrect password
  110.   }
  111.  
  112.   if (NULL == schService)
  113.   { switch (GetLastError())
  114.     {
  115.       case ERROR_ACCESS_DENIED :
  116.       { printf("\n%s",MSG_FOR_ACCESS_DENIED);
  117.         break;
  118.       }
  119.       case ERROR_SERVICE_EXISTS :
  120.       { printf("\nThe %s service is already installed",serviceName);
  121.         printf("\nRemove it first if you need to re-install a new version\n");
  122.         break;
  123.       }
  124.       default :
  125.       { PERR("CreateService");
  126.       }
  127.     }
  128.     return;
  129.   }
  130.   else
  131.   { if (FORCE_SERVICE_NAME_TO_FLOPPYLOCK)
  132.     { printf("\nThe FloppyLock service was installed successfully.\n");
  133.       printf("\nImportant! Before you can use the FloppyLock service, start Control");
  134.       printf("\nPanel and choose the Services applet.  Then select the FloppyLock");
  135.       printf("\nservice and press Startup.  Select System Account, or else FloppyLock");
  136.       printf("\nwill not be able to start properly.  If you want the service to start");
  137.       printf("\nautomatically, select Automatic.\n");
  138.     }
  139.     else
  140.     { printf("\nCreateService SUCCESS\n");
  141.       printf("\nDon't forget!!! You must now go to the Control Panel and");
  142.       printf("\n  use the Services applet to change the account name and");
  143.       printf("\n  password that this newly installed service will use when");
  144.       printf("\n  it attempts to logon as a service when it starts.");
  145.       printf("\nTo do this: use the Startup button in the Services applet,");
  146.       printf("\n  and (for example) specify the Administrator account and");
  147.       printf("\n  correct password.");
  148.       printf("\nAlso, use the Services applet to ensure this newly installed");
  149.       printf("\n  service starts automatically, since the point of this");
  150.       printf("\n  service is to start automatically and apply the DACLs");
  151.       printf("\n  to the floppy drives prior to a user logging on at");
  152.       printf("\n  the keyboard\n");
  153.     }
  154.   }
  155.  
  156.   CloseServiceHandle(schService);
  157. }
  158.  
  159. VOID RemoveService(LPCTSTR serviceName)
  160. {
  161.   {
  162.     #define                                     SZ_ENUM_BUF 4096
  163.     ENUM_SERVICE_STATUS        essServiceStatus[SZ_ENUM_BUF];
  164.     DWORD   dwBufSize = sizeof(essServiceStatus);
  165.     DWORD   dwBytesNeeded      = 0;
  166.     DWORD   dwServicesReturned = 0;
  167.     DWORD   dwResumeHandle     = 0;
  168.     DWORD   dwI                = 0;
  169.     BOOLEAN bFound = FALSE;
  170.  
  171.     if (!EnumServicesStatus(schSCManager,
  172.                             SERVICE_WIN32,
  173.                             SERVICE_ACTIVE,
  174.                             (LPENUM_SERVICE_STATUS)&essServiceStatus,
  175.                             dwBufSize,
  176.                             &dwBytesNeeded,
  177.                             &dwServicesReturned,
  178.                             &dwResumeHandle))
  179.     { switch (GetLastError())
  180.       {
  181.         case ERROR_ACCESS_DENIED :
  182.         { printf("\n%s",MSG_FOR_ACCESS_DENIED);
  183.           break;
  184.         }
  185.         default :
  186.         { PERR("EnumServicesStatus");
  187.         }
  188.       }
  189.       return;
  190.     }
  191.  
  192.     for (dwI=0; dwI<dwServicesReturned; dwI++)
  193.     { if(0 == _stricmp(essServiceStatus[dwI].lpServiceName,serviceName))
  194.       { bFound = TRUE;
  195.         break;
  196.       }
  197.     }
  198.  
  199.     if (bFound)
  200.     { printf("\nThe %s service cannot be removed until it has been stopped.",serviceName);
  201.       printf("\nTo stop the %s service, use the Stop button in the Control",serviceName);
  202.       printf("\n  Panel Services applet\n");
  203.       return;
  204.     }
  205.   }
  206.  
  207.   schService = OpenService(schSCManager,
  208.                            serviceName,
  209.                            SERVICE_ALL_ACCESS);
  210.   if (NULL == schService)
  211.   { switch (GetLastError())
  212.     {
  213.       case ERROR_ACCESS_DENIED :
  214.       { printf("\n%s",MSG_FOR_ACCESS_DENIED);
  215.         break;
  216.       }
  217.       case ERROR_SERVICE_DOES_NOT_EXIST :
  218.       { printf("\nThe %s service is not installed, so cannot be removed\n",serviceName);
  219.         break;
  220.       }
  221.       default :
  222.       { PERR("OpenService");
  223.       }
  224.     }
  225.     return;
  226.   }
  227.  
  228.   if (DeleteService(schService))
  229.   { printf("\nDeleteService SUCCESS\n");
  230.   }
  231.   else
  232.   { switch (GetLastError())
  233.     {
  234.       case ERROR_ACCESS_DENIED :
  235.       { printf("\n%s",MSG_FOR_ACCESS_DENIED);
  236.         break;
  237.       }
  238.       default :
  239.       { PERR("DeleteService");
  240.       }
  241.     }
  242.   }
  243. }
  244.  
  245. int main(int argc, char *argv[])
  246. {
  247.   #define           SZ_NAME_BUF  270  // 256 is max, add a little
  248.   UCHAR   ucNameBuf[SZ_NAME_BUF] = "FloppyLock";
  249.   LPTSTR  lpszServName = (LPTSTR)&ucNameBuf;
  250.  
  251.   UCHAR   ucExeNBuf[SZ_NAME_BUF] = "";
  252.   LPTSTR  lpszExeName  = (LPTSTR)&ucExeNBuf;
  253.  
  254.   BOOL    bRemovingService = FALSE;
  255.   char *p;
  256.  
  257.   // check if Win32s, if so, display notice and terminate
  258.       if( GetVersion() & 0x80000000 )
  259.       {
  260.         MessageBox( NULL,
  261.            "This application cannot run on Windows 3.1 or Windows 95.\n"
  262.            "This application will now terminate.",
  263.            "SD_FLPPY",
  264.            MB_OK | MB_ICONSTOP | MB_SETFOREGROUND );
  265.         return( 1 );
  266.       }
  267.  
  268.   if ( (argc != 2) && (argc != 3) )
  269.   { DisplayHelp();
  270.     exit(1);
  271.   }
  272.  
  273.   p=argv[1];
  274.   if (    ('/' == *p)
  275.        || ('-' == *p) )
  276.   { DisplayHelp();
  277.     exit(1);
  278.   }
  279.  
  280.   if (FORCE_SERVICE_NAME_TO_FLOPPYLOCK)
  281.   { if (argc != 2)
  282.     { DisplayHelp();
  283.       exit(1);
  284.     }
  285.     else
  286.     { strcpy(lpszExeName ,argv[1]);
  287.  
  288.       bRemovingService = (!stricmp(argv[1], "remove"));
  289.     }
  290.   }
  291.   else
  292.   { if (argc != 3)
  293.     { DisplayHelp();
  294.       exit(1);
  295.     }
  296.     else
  297.     { if (strlen(argv[1]) > 256)
  298.       { printf("\nThe service name cannot be longer than 256 characters\n");
  299.         exit(1);
  300.       }
  301.       strcpy(lpszServName,argv[1]);
  302.       strcpy(lpszExeName ,argv[2]);
  303.  
  304.       bRemovingService = (!stricmp(argv[2], "remove"));
  305.     }
  306.   }
  307.  
  308.   schSCManager = OpenSCManager(
  309.                       NULL,                   // machine (NULL == local)
  310.                       NULL,                   // database (NULL == default)
  311.                       SC_MANAGER_ALL_ACCESS); // access required
  312.   if (NULL == schSCManager)
  313.   { switch (GetLastError())
  314.     {
  315.       case ERROR_ACCESS_DENIED :
  316.       { printf("\n%s",MSG_FOR_ACCESS_DENIED);
  317.         break;
  318.       }
  319.       default :
  320.       { PERR("OpenSCManager");
  321.       }
  322.     }
  323.     return (0);
  324.   }
  325.  
  326.   if (bRemovingService)
  327.   { RemoveService(lpszServName);
  328.   }
  329.   else
  330.   { InstallService(lpszServName,lpszExeName);
  331.   }
  332.  
  333.   CloseServiceHandle(schSCManager);
  334. }
  335.  
  336. VOID DisplayHelp(VOID)
  337. {
  338.   if (FORCE_SERVICE_NAME_TO_FLOPPYLOCK)
  339.   { printf("\nInstalls or removes the FloppyLock service.  When the FloppyLock");
  340.     printf("\nservice is started, only members of the Administrators group can use");
  341.     printf("\nthe floppy drives on the computer.\n");
  342.     printf("\nTo install the FloppyLock service, type INSTSRV <path>\n");
  343.     printf("\n    path    Absolute path to the FloppyLock service, floplock.exe.  You must");
  344.     printf("\n            use a fully qualified path and the drive letter must be for a");
  345.     printf("\n            fixed, local drive.  For example, INSTSRV c:\\reskit\\floplock.exe.\n");
  346.     printf("\nTo remove the FloppyLock service, type INSTSRV remove\n");
  347.   }
  348.   else
  349.   { printf("\nUsage: instsrv <service name> <exe location>");
  350.     printf("\n           to install a service, or:");
  351.     printf("\n       instsrv <service name> remove");
  352.     printf("\n           to remove a service\n");
  353.     printf("\nIn the case of this service, a more specific example");
  354.     printf("\n       instsrv FloppyLocker c:\\q_a\\samples\\sd_flppy\\floplock.exe");
  355.     printf("\n           (note fully-qualified path name to .exe)");
  356.     printf("\n       instsrv FloppyLocker remove\n");
  357.   }
  358.   return;
  359. }
  360.