|
|
1.1 ! root 1: #include <windows.h> ! 2: #include <stdio.h> ! 3: #include <stdlib.h> ! 4: #include <string.h> ! 5: ! 6: #define FORCE_SERVICE_NAME_TO_FLOPPYLOCK (0==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 InstallService(LPCTSTR serviceName, LPCTSTR serviceExe) ! 21: { ! 22: LPCTSTR lpszBinaryPathName = serviceExe; ! 23: LPTSTR lpszRootPathName="?:\\"; ! 24: ! 25: if ( (':' != *(lpszBinaryPathName+1)) || ('\\' != *(lpszBinaryPathName+2)) ) ! 26: { printf("\n%s",MSG_1_FOR_BAD_PATH); ! 27: printf("\n%s\n",MSG_2_FOR_BAD_PATH); ! 28: return; ! 29: } ! 30: ! 31: #define DRIVE_TYPE_INDETERMINATE 0 ! 32: #define ROOT_DIR_DOESNT_EXIST 1 ! 33: ! 34: *lpszRootPathName = *(lpszBinaryPathName+0) ; ! 35: ! 36: switch ( GetDriveType(lpszRootPathName) ) ! 37: { ! 38: case DRIVE_FIXED : ! 39: { // OK ! 40: break; ! 41: } ! 42: case ROOT_DIR_DOESNT_EXIST : ! 43: { printf("\n%s",MSG_1_FOR_BAD_PATH); ! 44: printf("\n the root directory where the .exe is specified to be must exist, and"); ! 45: printf("\n%s\n",MSG_2_FOR_BAD_PATH); ! 46: return; ! 47: } ! 48: case DRIVE_TYPE_INDETERMINATE : ! 49: case DRIVE_REMOVABLE : ! 50: case DRIVE_REMOTE : ! 51: case DRIVE_CDROM : ! 52: case DRIVE_RAMDISK : ! 53: { printf("\n%s",MSG_1_FOR_BAD_PATH); ! 54: printf("\n%s\n",MSG_2_FOR_BAD_PATH); ! 55: return; ! 56: } ! 57: default : ! 58: { printf("\n%s",MSG_1_FOR_BAD_PATH); ! 59: printf("\n%s\n",MSG_2_FOR_BAD_PATH); ! 60: return; ! 61: } ! 62: } ! 63: ! 64: if (INVALID_HANDLE_VALUE == CreateFile(lpszBinaryPathName, ! 65: GENERIC_READ, ! 66: FILE_SHARE_READ, ! 67: NULL, ! 68: OPEN_EXISTING, ! 69: FILE_ATTRIBUTE_NORMAL, ! 70: NULL)) ! 71: { printf("\n%s",MSG_1_FOR_BAD_PATH); ! 72: printf("\n the file must exist, and"); ! 73: printf("\n%s\n",MSG_2_FOR_BAD_PATH); ! 74: return; ! 75: } ! 76: ! 77: schService = CreateService( ! 78: schSCManager, // SCManager database ! 79: serviceName, // name of service ! 80: serviceName, // name to display (new parameter after october beta) ! 81: SERVICE_ALL_ACCESS, // desired access ! 82: SERVICE_WIN32_OWN_PROCESS, // service type ! 83: SERVICE_AUTO_START, // start type ! 84: SERVICE_ERROR_NORMAL, // error control type ! 85: lpszBinaryPathName, // service's binary ! 86: NULL, // no load ordering group ! 87: NULL, // no tag identifier ! 88: NULL, // no dependencies ! 89: ".\\Administrator", // Admin account ! 90: ""); // null password ! 91: ! 92: if (NULL == schService) ! 93: { switch (GetLastError()) ! 94: { ! 95: case ERROR_ACCESS_DENIED : ! 96: { printf("\n%s",MSG_FOR_ACCESS_DENIED); ! 97: break; ! 98: } ! 99: case ERROR_SERVICE_EXISTS : ! 100: { printf("\nThe %s service is already installed",serviceName); ! 101: printf("\nRemove it first if you need to re-install a new version\n"); ! 102: break; ! 103: } ! 104: default : ! 105: { PERR("CreateService"); ! 106: } ! 107: } ! 108: return; ! 109: } ! 110: else ! 111: { printf("\nCreateService SUCCESS\n"); ! 112: printf("\nDon't forget!!! You must now go to the Control Panel and"); ! 113: printf("\n use the Services applet to change the account name and"); ! 114: printf("\n password that this newly installed service will use when"); ! 115: printf("\n it attempts to logon as a service when it starts."); ! 116: printf("\nTo do this: use the Startup button in the Services applet,"); ! 117: printf("\n and (for example) specify the Administrator account and"); ! 118: printf("\n correct password."); ! 119: printf("\nAlso, use the Services applet to ensure this newly installed"); ! 120: printf("\n service starts automatically, since the point of this"); ! 121: printf("\n service is to start automatically and apply the DACLs"); ! 122: printf("\n to the floppy drives prior to a user logging on at"); ! 123: printf("\n the keyboard\n"); ! 124: } ! 125: ! 126: CloseServiceHandle(schService); ! 127: } ! 128: ! 129: VOID RemoveService(LPCTSTR serviceName) ! 130: { ! 131: { ! 132: #define SZ_ENUM_BUF 4096 ! 133: ENUM_SERVICE_STATUS essServiceStatus[SZ_ENUM_BUF]; ! 134: DWORD dwBufSize = sizeof(essServiceStatus); ! 135: DWORD dwBytesNeeded = 0; ! 136: DWORD dwServicesReturned = 0; ! 137: DWORD dwResumeHandle = 0; ! 138: DWORD dwI = 0; ! 139: BOOLEAN bFound = FALSE; ! 140: ! 141: if (!EnumServicesStatus(schSCManager, ! 142: SERVICE_WIN32, ! 143: SERVICE_ACTIVE, ! 144: (LPENUM_SERVICE_STATUS)&essServiceStatus, ! 145: dwBufSize, ! 146: &dwBytesNeeded, ! 147: &dwServicesReturned, ! 148: &dwResumeHandle)) ! 149: { switch (GetLastError()) ! 150: { ! 151: case ERROR_ACCESS_DENIED : ! 152: { printf("\n%s",MSG_FOR_ACCESS_DENIED); ! 153: break; ! 154: } ! 155: default : ! 156: { PERR("EnumServicesStatus"); ! 157: } ! 158: } ! 159: return; ! 160: } ! 161: ! 162: for (dwI=0; dwI<dwServicesReturned; dwI++) ! 163: { if(0 == _stricmp(essServiceStatus[dwI].lpServiceName,serviceName)) ! 164: { bFound = TRUE; ! 165: break; ! 166: } ! 167: } ! 168: ! 169: if (bFound) ! 170: { printf("\nThe %s service cannot be removed until it has been stopped.",serviceName); ! 171: printf("\nTo stop the %s service, use the Stop button in the Control",serviceName); ! 172: printf("\n Panel Services applet\n"); ! 173: return; ! 174: } ! 175: } ! 176: ! 177: schService = OpenService(schSCManager, ! 178: serviceName, ! 179: SERVICE_ALL_ACCESS); ! 180: if (NULL == schService) ! 181: { switch (GetLastError()) ! 182: { ! 183: case ERROR_ACCESS_DENIED : ! 184: { printf("\n%s",MSG_FOR_ACCESS_DENIED); ! 185: break; ! 186: } ! 187: case ERROR_SERVICE_DOES_NOT_EXIST : ! 188: { printf("\nThe %s service is not installed, so cannot be removed\n",serviceName); ! 189: break; ! 190: } ! 191: default : ! 192: { PERR("OpenService"); ! 193: } ! 194: } ! 195: return; ! 196: } ! 197: ! 198: if (DeleteService(schService)) ! 199: { printf("\nDeleteService SUCCESS\n"); ! 200: } ! 201: else ! 202: { switch (GetLastError()) ! 203: { ! 204: case ERROR_ACCESS_DENIED : ! 205: { printf("\n%s",MSG_FOR_ACCESS_DENIED); ! 206: break; ! 207: } ! 208: default : ! 209: { PERR("DeleteService"); ! 210: } ! 211: } ! 212: } ! 213: } ! 214: ! 215: VOID main(int argc, char *argv[]) ! 216: { ! 217: #define SZ_NAME_BUF 270 // 256 is max, add a little ! 218: UCHAR ucNameBuf[SZ_NAME_BUF] = "FloppyLock"; ! 219: LPTSTR lpszServName = (LPTSTR)&ucNameBuf; ! 220: ! 221: UCHAR ucExeNBuf[SZ_NAME_BUF] = ""; ! 222: LPTSTR lpszExeName = (LPTSTR)&ucExeNBuf; ! 223: ! 224: BOOL bRemovingService = FALSE; ! 225: ! 226: if (FORCE_SERVICE_NAME_TO_FLOPPYLOCK) ! 227: { if (argc != 2) ! 228: { printf("\nUsage: instsrv <exe location>"); ! 229: printf("\n to install the FloppyLock service, or:"); ! 230: printf("\n instsrv remove"); ! 231: printf("\n to remove the FloppyLock service\n"); ! 232: printf("\nA more specific example"); ! 233: printf("\n instsrv c:\\q_a\\samples\\sd_flppy\\service\\floplock.exe"); ! 234: printf("\n (note fully-qualified path name to .exe)"); ! 235: printf("\n instsrv remove\n"); ! 236: exit(1); ! 237: } ! 238: else ! 239: { ! 240: strcpy(lpszExeName ,argv[1]); ! 241: ! 242: bRemovingService = (!stricmp(argv[1], "remove")); ! 243: } ! 244: } ! 245: else ! 246: { if (argc != 3) ! 247: { printf("\nUsage: instsrv <service name> <exe location>"); ! 248: printf("\n to install a service, or:"); ! 249: printf("\n instsrv <service name> remove"); ! 250: printf("\n to remove a service\n"); ! 251: printf("\nIn the case of this service, a more specific example"); ! 252: printf("\n instsrv FloppyLocker c:\\q_a\\samples\\sd_flppy\\service\\floplock.exe"); ! 253: printf("\n (note fully-qualified path name to .exe)"); ! 254: printf("\n instsrv FloppyLocker remove\n"); ! 255: exit(1); ! 256: } ! 257: else ! 258: { if (strlen(argv[1]) > 256) ! 259: { printf("\nThe service name cannot be longer than 256 characters\n"); ! 260: exit(1); ! 261: } ! 262: strcpy(lpszServName,argv[1]); ! 263: strcpy(lpszExeName ,argv[2]); ! 264: ! 265: bRemovingService = (!stricmp(argv[2], "remove")); ! 266: } ! 267: } ! 268: ! 269: schSCManager = OpenSCManager( ! 270: NULL, // machine (NULL == local) ! 271: NULL, // database (NULL == default) ! 272: SC_MANAGER_ALL_ACCESS); // access required ! 273: if (NULL == schSCManager) ! 274: { switch (GetLastError()) ! 275: { ! 276: case ERROR_ACCESS_DENIED : ! 277: { printf("\n%s",MSG_FOR_ACCESS_DENIED); ! 278: break; ! 279: } ! 280: default : ! 281: { PERR("OpenSCManager"); ! 282: } ! 283: } ! 284: return; ! 285: } ! 286: ! 287: if (bRemovingService) ! 288: { RemoveService(lpszServName); ! 289: } ! 290: else ! 291: { InstallService(lpszServName,lpszExeName); ! 292: } ! 293: ! 294: CloseServiceHandle(schSCManager); ! 295: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.