Annotation of q_a/samples/sd_flppy/instsrv.c, revision 1.1.1.2

1.1       root        1: #include <windows.h>
                      2: #include <stdio.h>
                      3: #include <stdlib.h>
                      4: #include <string.h>
                      5: 
1.1.1.2 ! root        6: #define FORCE_SERVICE_NAME_TO_FLOPPYLOCK (1==0)
1.1       root        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: 
1.1.1.2 ! root       20: VOID DisplayHelp(VOID);
        !            21: 
1.1       root       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: 
1.1.1.2 ! root       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:   }
1.1       root      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
1.1.1.2 ! root      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:     }
1.1       root      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: VOID 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;
1.1.1.2 ! root      255:   char *p;
        !           256: 
        !           257:   if ( (argc != 2) && (argc != 3) )
        !           258:   { DisplayHelp();
        !           259:     exit(1);
        !           260:   }
        !           261: 
        !           262:   p=argv[1];
        !           263:   if (    ('/' == *p)
        !           264:        || ('-' == *p) )
        !           265:   { DisplayHelp();
        !           266:     exit(1);
        !           267:   }
1.1       root      268: 
                    269:   if (FORCE_SERVICE_NAME_TO_FLOPPYLOCK)
                    270:   { if (argc != 2)
1.1.1.2 ! root      271:     { DisplayHelp();
1.1       root      272:       exit(1);
                    273:     }
                    274:     else
1.1.1.2 ! root      275:     { strcpy(lpszExeName ,argv[1]);
1.1       root      276: 
                    277:       bRemovingService = (!stricmp(argv[1], "remove"));
                    278:     }
                    279:   }
                    280:   else
                    281:   { if (argc != 3)
1.1.1.2 ! root      282:     { DisplayHelp();
1.1       root      283:       exit(1);
                    284:     }
                    285:     else
                    286:     { if (strlen(argv[1]) > 256)
                    287:       { printf("\nThe service name cannot be longer than 256 characters\n");
                    288:         exit(1);
                    289:       }
                    290:       strcpy(lpszServName,argv[1]);
                    291:       strcpy(lpszExeName ,argv[2]);
                    292: 
                    293:       bRemovingService = (!stricmp(argv[2], "remove"));
                    294:     }
                    295:   }
                    296: 
                    297:   schSCManager = OpenSCManager(
                    298:                       NULL,                   // machine (NULL == local)
                    299:                       NULL,                   // database (NULL == default)
                    300:                       SC_MANAGER_ALL_ACCESS); // access required
                    301:   if (NULL == schSCManager)
                    302:   { switch (GetLastError())
                    303:     {
                    304:       case ERROR_ACCESS_DENIED :
                    305:       { printf("\n%s",MSG_FOR_ACCESS_DENIED);
                    306:         break;
                    307:       }
                    308:       default :
                    309:       { PERR("OpenSCManager");
                    310:       }
                    311:     }
                    312:     return;
                    313:   }
                    314: 
                    315:   if (bRemovingService)
                    316:   { RemoveService(lpszServName);
                    317:   }
                    318:   else
                    319:   { InstallService(lpszServName,lpszExeName);
                    320:   }
                    321: 
                    322:   CloseServiceHandle(schSCManager);
                    323: }
1.1.1.2 ! root      324: 
        !           325: VOID DisplayHelp(VOID)
        !           326: {
        !           327:   if (FORCE_SERVICE_NAME_TO_FLOPPYLOCK)
        !           328:   { printf("\nInstalls or removes the FloppyLock service.  When the FloppyLock");
        !           329:     printf("\nservice is started, only members of the Administrators group can use");
        !           330:     printf("\nthe floppy drives on the computer.\n");
        !           331:     printf("\nTo install the FloppyLock service, type INSTSRV <path>\n");
        !           332:     printf("\n    path    Absolute path to the FloppyLock service, floplock.exe.  You must");
        !           333:     printf("\n            use a fully qualified path and the drive letter must be for a");
        !           334:     printf("\n            fixed, local drive.  For example, INSTSRV c:\\reskit\\floplock.exe.\n");
        !           335:     printf("\nTo remove the FloppyLock service, type INSTSRV remove\n");
        !           336:   }
        !           337:   else
        !           338:   { printf("\nUsage: instsrv <service name> <exe location>");
        !           339:     printf("\n           to install a service, or:");
        !           340:     printf("\n       instsrv <service name> remove");
        !           341:     printf("\n           to remove a service\n");
        !           342:     printf("\nIn the case of this service, a more specific example");
        !           343:     printf("\n       instsrv FloppyLocker c:\\q_a\\samples\\sd_flppy\\floplock.exe");
        !           344:     printf("\n           (note fully-qualified path name to .exe)");
        !           345:     printf("\n       instsrv FloppyLocker remove\n");
        !           346:   }
        !           347:   return;
        !           348: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.