mturk       2003/02/07 10:56:33

  Modified:    daemon/src/native/nt/procrun readme.txt procrun.h procrun.c
  Log:
  Added --User and --Password to enable to run the
  redirected process under other account.
  
  Revision  Changes    Path
  1.2       +7 -0      jakarta-commons-sandbox/daemon/src/native/nt/procrun/readme.txt
  
  Index: readme.txt
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/daemon/src/native/nt/procrun/readme.txt,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- readme.txt        7 Feb 2003 07:04:35 -0000       1.1
  +++ readme.txt        7 Feb 2003 18:56:33 -0000       1.2
  @@ -106,6 +106,13 @@
                   The services startup mode Automatic or Manual.
                   Default value is auto.
   
  +--User          <username>
  +                The User account used for launching redirected process.
  +
  +--Password      <password>
  +                The password of User account used for launching
  +                redirected process.
  +                
   Examples:
   
   Installing Tomcat as service:
  
  
  
  1.2       +4 -0      jakarta-commons-sandbox/daemon/src/native/nt/procrun/procrun.h
  
  Index: procrun.h
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/daemon/src/native/nt/procrun/procrun.h,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- procrun.h 7 Feb 2003 07:04:35 -0000       1.1
  +++ procrun.h 7 Feb 2003 18:56:33 -0000       1.2
  @@ -113,6 +113,8 @@
   #define PROCRUN_PARAMS_STARTCLASS   "StartupClass"
   #define PROCRUN_PARAMS_STOPCLASS    "ShutdownClass"
   #define PROCRUN_PARAMS_STARTUP      "Startup"
  +#define PROCRUN_PARAMS_ACCOUNT      "User"
  +#define PROCRUN_PARAMS_PASSWORD     "Password"
   
   
   #define PROCRUN_DEFAULT_CLASS       "Main"
  @@ -173,6 +175,8 @@
           char                  *description;
           char                  *path;
           char                  *image;
  +        char                  *account;
  +        char                  *password;
           HANDLE                infile;
           HANDLE                outfile;
           HANDLE                errfile;
  
  
  
  1.3       +145 -19   jakarta-commons-sandbox/daemon/src/native/nt/procrun/procrun.c
  
  Index: procrun.c
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/daemon/src/native/nt/procrun/procrun.c,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- procrun.c 7 Feb 2003 09:58:48 -0000       1.2
  +++ procrun.c 7 Feb 2003 18:56:33 -0000       1.3
  @@ -215,6 +215,40 @@
       return h;
   }
   
  +static void simple_encrypt(int seed, const char *str, unsigned char bytes[256])
  +{
  +    int i;
  +    char sc[256];
  +
  +    srand(seed);
  +    memset(sc, 0, 256);
  +    strncpy(sc, str, 255);
  +    for (i = 0; i < 256; i ++) {
  +        bytes[i] = ((rand() % 256) ^ sc[i]);
  +    }
  +}
  +
  +static void simple_decrypt(int seed, char *str, unsigned char bytes[256])
  +{
  +    int i;
  +    char sc[256];
  +
  +    srand(seed);
  +    for (i = 0; i < 256; i ++) {
  +        sc[i] = ((rand() % 256) ^ bytes[i]);
  +    }
  +    strcpy(str, sc);
  +}
  +
  +static void test_enc() 
  +{
  +    unsigned char b[256];
  +    char op[32];
  +    simple_encrypt(100, "test encryption", b);
  +    simple_decrypt(100, op, b);
  +    DBPRINTF1("ENC %s", op);
  +}
  +
   static void inject_exitprocess(PROCESS_INFORMATION *child) 
   {
       PFNCREATERTHRD pfn_CreateRemoteThread;
  @@ -237,8 +271,8 @@
       } 
       GenerateConsoleCtrlEvent(CTRL_C_EVENT, child->dwProcessId);
       GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, child->dwProcessId);
  -#if 0
  -    Sleep(500);
  +#if 1
  +    Sleep(2000);
   #endif
       if (!GetExitCodeProcess(child->hProcess, &stat) || 
           (stat != STILL_ACTIVE)) {
  @@ -671,6 +705,19 @@
               proc->service.image = pool_strdup(proc->pool, kval);
           }
           klen = MAX_PATH;
  +        if ((err = RegQueryValueEx(key, PROCRUN_PARAMS_ACCOUNT, NULL, NULL, 
  +                                   (unsigned char *)kval,
  +                                   &klen)) == ERROR_SUCCESS) {
  +            proc->service.account = pool_strdup(proc->pool, kval);
  +        }
  +        klen = MAX_PATH;
  +        if ((err = RegQueryValueEx(key, PROCRUN_PARAMS_PASSWORD, NULL, NULL, 
  +                                   (unsigned char *)kval,
  +                                   &klen)) == ERROR_SUCCESS) {
  +            proc->service.password = pool_calloc(proc->pool, 256);
  +            simple_decrypt(100, proc->service.password,  kval);
  +        }
  +        klen = MAX_PATH;
           if ((err = RegQueryValueEx(key, PROCRUN_PARAMS_STARTCLASS, NULL, NULL, 
                                      (unsigned char *)kval,
                                      &klen)) == ERROR_SUCCESS) {
  @@ -1258,9 +1305,10 @@
       return 0;
   }
   
  +
   int procrun_redirect(char *program, char **envp, procrun_t *env)
   {
  -    STARTUPINFO si; 
  +    STARTUPINFO si;
       DWORD  id;
   
       memset(&si, 0, sizeof(STARTUPINFO));
  @@ -1312,21 +1360,67 @@
           program = env->m->java.path;
       }
       DBPRINTF2("RUN [%s] %s\n", program, env->m->argw);
  -    if (!CreateProcess(program,
  -                       env->m->argw,
  -                       NULL,
  -                       NULL,
  -                       TRUE,
  -                       CREATE_SUSPENDED | CREATE_NEW_CONSOLE | 
  -                       CREATE_NEW_PROCESS_GROUP,
  -                       env->m->envw,
  -                       env->m->service.path,
  -                       &si,
  -                       &env->c->pinfo)) {
  +    if (env->m->service.account && env->m->service.password &&
  +        env->m->service.mode == 1) {
  +        HANDLE user, token;
  +        if (!LogonUser(env->m->service.account, 
  +                       NULL, 
  +                       env->m->service.password, 
  +                       LOGON32_LOGON_SERVICE,
  +                       LOGON32_PROVIDER_DEFAULT,
  +                       &user)) {
  +            DBPRINTF0(NULL);
  +            DBPRINTF0("LogonUser failed");
  +            return -1;
  +        }
   
  -        DBPRINTF1("Error redirecting '%s'\n", program);
  +        DuplicateTokenEx(user, 
  +                         TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_ASSIGN_PRIMARY, 
  +                         NULL,
  +                         SecurityImpersonation,
  +                         TokenPrimary,
  +                         &token);
           DBPRINTF0(NULL);
  -        return -1;
  +
  +        DBPRINTF2("Launching as %s:%s", env->m->service.account, 
env->m->service.password);
  +        ImpersonateLoggedOnUser(token);
  +        DBPRINTF0(NULL);
  +        si.lpDesktop = (LPSTR) "Winsta0\\Default";
  +        if (!CreateProcessAsUser(token,
  +                                 program,
  +                                 env->m->argw,
  +                                 NULL,
  +                                 NULL,
  +                                 TRUE,
  +                                 CREATE_SUSPENDED | CREATE_NEW_CONSOLE | 
  +                                 CREATE_NEW_PROCESS_GROUP,
  +                                 env->m->envw,
  +                                 env->m->service.path,
  +                                 &si,
  +                                 &env->c->pinfo)) {
  +
  +            DBPRINTF1("Error redirecting '%s'\n", program);
  +            DBPRINTF0(NULL);
  +            return -1;
  +        }
  +    }
  +    else {
  +        if (!CreateProcess(program,
  +                           env->m->argw,
  +                           NULL,
  +                           NULL,
  +                           TRUE,
  +                           CREATE_SUSPENDED | CREATE_NEW_CONSOLE | 
  +                           CREATE_NEW_PROCESS_GROUP,
  +                           env->m->envw,
  +                           env->m->service.path,
  +                           &si,
  +                           &env->c->pinfo)) {
  +        
  +            DBPRINTF1("Error redirecting '%s'\n", program);
  +            DBPRINTF0(NULL);
  +            return -1;
  +        }
       }
       pool_handle(env->c->pool, env->c->pinfo.hThread);
       pool_handle(env->c->pool, env->c->pinfo.hProcess);
  @@ -1356,7 +1450,7 @@
       DWORD c, type = REG_SZ;
   
   
  -    if (service) {
  +    if (service == 1) {
           sprintf(skey, PROCRUN_REGKEY_SERVICES, proc->service.name);
           if ((err = RegOpenKeyEx(HKEY_LOCAL_MACHINE, skey,
                               0, KEY_SET_VALUE, &key)) != ERROR_SUCCESS) {
  @@ -1377,7 +1471,9 @@
       DBPRINTF2("Creating [%s] name  [%s]\n", skey, name);
       DBPRINTF2("Creating [%s] value [%s]\n", skey, value);
       if (value) {
  -        if (len > 0)
  +        if (service == 2)
  +            type = REG_BINARY;
  +        else if (len > 0)
               type = REG_MULTI_SZ;
           else
               len = strlen(value);
  @@ -1454,6 +1550,10 @@
                   else if (stricmp(argv[i], "manual") == 0)
                       proc->service.startup = SERVICE_DEMAND_START;
               }
  +            else if (STRNI_COMPARE(argp, PROCRUN_PARAMS_ACCOUNT))
  +                proc->service.account = pool_strdup(proc->pool, argv[++i]);
  +            else if (STRNI_COMPARE(argp, PROCRUN_PARAMS_PASSWORD))
  +                proc->service.password = pool_strdup(proc->pool, argv[++i]);
               else {
                   DBPRINTF1("Unrecognized option %s\n", argv[i]);
                   break;
  @@ -1550,6 +1650,15 @@
       if (proc->java.stop_class)
           set_service_param(proc, PROCRUN_PARAMS_STOPCLASS,
                             proc->java.stop_class, 0, 0);
  +    if (proc->service.account)
  +        set_service_param(proc, PROCRUN_PARAMS_ACCOUNT,
  +                          proc->service.account, 0, 0);
  +    if (proc->service.password) {
  +        unsigned char b[256];
  +        simple_encrypt(100, proc->service.password, b);
  +        set_service_param(proc, PROCRUN_PARAMS_PASSWORD,
  +                          b, 256, 2);
  +    }
       if (proc->java.opts) {
           int l = strlen(proc->java.opts);
           /* change the string to zero separated for MULYI_SZ */
  @@ -1643,6 +1752,10 @@
                   else if (stricmp(argv[i], "manual") == 0)
                       proc->service.startup = SERVICE_DEMAND_START;
               }
  +            else if (STRNI_COMPARE(argp, PROCRUN_PARAMS_ACCOUNT))
  +                proc->service.account = pool_strdup(proc->pool, argv[++i]);
  +            else if (STRNI_COMPARE(argp, PROCRUN_PARAMS_PASSWORD))
  +                proc->service.password = pool_strdup(proc->pool, argv[++i]);
               else
                   break;
           }
  @@ -1727,6 +1840,15 @@
       if (proc->java.stop_class)
           set_service_param(proc, PROCRUN_PARAMS_STOPCLASS,
                             proc->java.stop_class, 0, 0);
  +    if (proc->service.account)
  +        set_service_param(proc, PROCRUN_PARAMS_ACCOUNT,
  +                          proc->service.account, 0, 0);
  +    if (proc->service.password) {
  +        unsigned char b[256];
  +        simple_encrypt(100, proc->service.password, b);
  +        set_service_param(proc, PROCRUN_PARAMS_PASSWORD,
  +                          b, 256, 2);
  +    }
       if (proc->java.opts) {
           int l = strlen(proc->java.opts);
           /* change the string to zero separated for MULYI_SZ */
  @@ -2026,6 +2148,10 @@
           case PROCRUN_CMD_DELETE_SERVICE:
               rv = procrun_delete_service(env->m);
               break;
  +        case PROCRUN_CMD_STOP_SERVICE:
  +            rv = -1;
  +            DBPRINTF0("STOP SERVICE is unimplemented for now");
  +            break;
           case PROCRUN_CMD_RUN_SERVICE:
               debug_process(argc, argv, env->m);
               env->m->service.mode = 1;
  @@ -2105,11 +2231,11 @@
   
   void __cdecl main(int argc, char **argv)
   {
  +
       procrun_t *env = alloc_environment();
       g_proc_mode = PROCRUN_MODE_CONSOLE;
       g_env = env;
   
  -    AllocConsole();
       procrun_main(argc, argv, _environ, env);
   
       free_environment(env);
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to