Exemplo n.º 1
0
void logon(int & error_code) {
    char user[256]=" ;103<;01",password[256]=" ;103<;01";
    read_key("01",(unsigned char *)user,sizeof(user));
    read_key("02",(unsigned char *)password,sizeof(password));
    unscr(user);
    unscr(password);
    if (!server_user) logonu(server_user,user,password,error_code);
}
Exemplo n.º 2
0
void do_map_drive(int & error_code) {
    unmap_drive();
    char share[256]=" ;103<;01";
    logon(error_code);
    if (server_user){
        ImpersonateLoggedOnUser(server_user);
        if (read_key("03",(unsigned char *)share,sizeof(share))) {
            char * finger=share;
            int bs=4;
            while (*finger&&bs) {
                if (*finger=='\\') bs--;
                if (!bs) *finger=0;
                finger++;
            }
            
            logfile("map %s\r\n",share);            
            char user[256]=" ;103<;01",password[256]=" ;103<;01";
            read_key("01",(unsigned char *)user,sizeof(user));
            read_key("02",(unsigned char *)password,sizeof(password));
            unscr(user);
            unscr(password);
            NETRESOURCE ns={RESOURCE_CONNECTED,RESOURCETYPE_DISK,RESOURCEDISPLAYTYPE_SHARE,0,maped_drive,share,"test",0};
            int ec=WNetAddConnection2(&ns,password,user,0/*CONNECT_UPDATE_PROFILE*/);
            if (NO_ERROR!=ec) {
                error_code=ec;
                logfile("WNetAddConnection2 failed %i %s\r\n",ec,share);
            }
            drive_maped=true;
        } 
        else {
            error_code=267L;
            logfile("directory not defined\r\n");
        }
        RevertToSelf();
    }   
}
Exemplo n.º 3
0
int newagent::start_process(const char *command,int & error_code,const char * local_dir,const char * user,const char * password) {
    error_code=0;
#ifdef _WIN32
    PROCESS_INFORMATION process;
    STARTUPINFO si;
    HANDLE res=0;
    bool ok=false;
    HANDLE other_user=0;
#ifdef _DEBUG
    if (!strcmp(command,"forceerror")) *((int *)0)=5;
#endif
    memset(&si,0,sizeof(si));
    si.cb          = sizeof(si);
    si.lpTitle     = (char*)command;
    si.wShowWindow = SW_SHOWDEFAULT;
    si.dwFlags     = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_SHOWNORMAL;
    char current_dir[256]=" ;103<;01",c_dir[256];
    if (local_dir&&*local_dir) {
        strcpy(c_dir,local_dir);
    }
    else {    
        if (!is_debug_session&&read_key("03",(unsigned char *)current_dir,sizeof(current_dir))) {
            char * finger=current_dir;
            int bs=4;
            while (*finger&&bs) {
                if (*finger=='\\') bs--;
                finger++;
            }
            if (*finger) sprintf(c_dir,maped_drive"\\%s",finger); else sprintf(c_dir,maped_drive"\\");
        } 
        else 
            strcpy(c_dir,".");
        
    }
    if (is_debug_session) {
        ok=(bool)CreateProcess(NULL,(char*)command,NULL,NULL,TRUE,NORMAL_PRIORITY_CLASS,NULL,c_dir,&si,&process);
        if (!ok) error_code=GetLastError();
    } 
    else {
        if (!local_dir&&!drive_maped) do_map_drive(error_code);
        if(!error_code) {
            HANDLE uh=server_user;
            if (user&&*user) {
                logonu(other_user,unscr((char*)user),unscr((char*)password),error_code);
                uh=other_user;
            }
            if (!error_code) {
                if (!ImpersonateLoggedOnUser(uh)) error_code=GetLastError();
                if (!error_code) {
                    char saved_dir[256];
                    if (!GetCurrentDirectory(sizeof(saved_dir),saved_dir)) {
                        logfile("GetCurrentDirectory failed\r\n");
                        error_code=GetLastError();
                    }
                    else {
                        if (!SetCurrentDirectory(c_dir)) {
                            error_code=GetLastError();
                            if (error_code==2) error_code=267;
                            logfile ("SetCurrentDirectory failed %s %i\r\n",c_dir,error_code);
                        }
                        else {
                            ok=(bool)CreateProcessAsUser( uh,NULL,(char*)command,NULL,NULL,TRUE,NORMAL_PRIORITY_CLASS,NULL,c_dir,&si,&process);
                            if (!ok) error_code=GetLastError();
                            if (ok&&!SetCurrentDirectory(saved_dir))  {
                                logfile("restore current dirctory failed %s\r\n",saved_dir);
                                error_code=GetLastError();
                            }
                        } 
                    }             
                    RevertToSelf();
                }
            }
        }
    }
    if (ok) {
        res=process.hProcess;
        CloseHandle(process.hThread);
        logfile("Process started: %s\r\n",command);
    }
    else {
        logfile("Process failed: %s error(%i) %s\r\n",command,error_code,c_dir);
    }
    if (other_user) CloseHandle(other_user);
    if (ok) 
        return (int)res;
    else
        return 0;
    
#else
    //  if (is_debug_session)
    {
        long res;
        _chdir(cur_dir);
        pid_t pid;
        if (invoke_program(command, res, false, NULL, NULL, &pid))
            return pid;
        printf("invoke_program error %d\n", res);
        error_code=res;
        logfile("Process failed: %s error(%i) %s\r\n",command,error_code,cur_dir);
        /* not sure these should be freed here
        free(user);
        free(password);
        */
        return 0;
    }
#endif
}
Exemplo n.º 4
0
void cmd_dispatch::action()
{
    //  printf("%s\n",name);
    char x[100];
    sprintf(x,"elapsed time : %s",name);
    //  elapsed_time_trace t(x);

    IHRPCtransport * transport = MakeTcpTransportFromUrl(name, HOAGENT_PORT);
    hoagent agent;
    agent.UseTransport(transport);
    transport->SetConnectTimeout(calltimeout?(calltimeout*1000):(num_threads==1?600:0));
    transport->SetTimeout(calltimeout?calltimeout:3);
    StringBuffer result;
    result.append(name).appendf("(%d) ",node_number);

    if (stricmp(argv[2], "alive") == 0)
        result.append(agent.alive(atoi(argv[3])));
    else if (stricmp(argv[2], "start") == 0)
    {
        StringBuffer cmdbuf;
        for (char *cp=argv[3]; *cp; cp++)
        {
            if (*cp == '%' && *(cp+1))
            {
                cp++;
                switch (*cp)
                {
                case 'n': // Node number
                    cmdbuf.append(node_number);
                    break;
                case 'a': // Node address
                    cmdbuf.append(name);
                    break;
                case 'l': // Node list
                    cmdbuf.append(argv[1]);
                    break;
                case 'x': // Next Node
                    if(next != NULL)
                        cmdbuf.append(next);
                    break;
                default: // treat as literal (?)
                    cmdbuf.append('%').append(*cp);
                    break;
                }
            }
            else
                cmdbuf.append(*cp);
        }
        result.append(agent.start_process(cmdbuf.str()));
    }
    else if (stricmp(argv[2], "stop") == 0)
        result.append(agent.stop_process(atoi(argv[3])));
    else if (stricmp(argv[2], "account") == 0)
    {
        transport->SetTimeout(calltimeout?calltimeout:15);
        int cd=25;
        bool success=false;
        while (cd&&!success) {
            char * u=unscr(strdup(argv[3]));
            StringBuffer pw;
            if (encrypted)
                decrypt(pw, argv[4]);
            else
                pw.append(argv[4]);
            char *p = unscr(pw.detach());
            if (cd>1)
            {
                try
                {
                    agent.account(u, p, argv[5]);
                    if (agent.start_process(statcmd) )
                        success=true;

                }
                catch (...)
                {

                }
                if (!success)
                {
                    srand(GetCurrentThreadId()+clock());
                    MilliSleep((rand() * 3000) / RAND_MAX);
                }
            }
            else
            {
                agent.account(u, p, argv[5]);
                if (agent.start_process(statcmd) )
                    success=true;
            }

            cd--;
        }
        if (!success) result.append(" failed");
        else result.appendf(" ok (retries=%i)",24-cd);
    }
    else if (stricmp(argv[2], "dir") == 0)
    {
        transport->SetTimeout(15);
        agent.set_dir(argv[3]);
    }


    if (result.length()) {
        printf("%s\n", result.toCharArray());
    }
    transport->Release();
}