/* ** Close SSH transport. */ void transport_ssh_close(void){ if( sshPid ){ /*printf("Closing SSH tunnel: ");*/ fflush(stdout); pclose2(sshIn, sshOut, sshPid); sshPid = 0; } }
void transport_global_shutdown(void){ if( g.urlIsSsh && sshPid ){ printf("Closing SSH tunnel: "); fflush(stdout); pclose2(sshIn, sshOut, sshPid); sshPid = 0; } if( g.urlIsHttps ){ #ifdef FOSSIL_ENABLE_SSL ssl_global_shutdown(); #endif }else{ socket_global_shutdown(); } }
bool Activator::doScript(const string &strServerId, const string &strScript, string &strResult, map <string,string> &mResult, const string &sEndMark) { LOG->info()<<"doScript "<<strScript<<" begin----"<<endl; if(!TC_File::isFileExistEx(strScript)) { strResult = "The script file: " + strScript+" is not exist"; return false; } string sRealEndMark = sEndMark; if(sRealEndMark == "") { sRealEndMark = "end-"+TC_File::extractFileName(strScript); } LOG->info()<<"doScript "<<strScript<<" endMark----"<<sRealEndMark<<endl; if(!TC_File::canExecutable(strScript)) { TC_File::setExecutable(strScript,true); } string sRedirect =""; if(_sRedirectPath != "") { sRedirect = " 2>&1 >>" +_sRedirectPath; } string sCmd = strScript + sRedirect + " " + strServerId + " &"; FILE *fp = popen2(sCmd.c_str(),"r"); if(fp == NULL) { strResult = "popen script file: "+ strScript +" error "; return false; } int flags; if((flags=fcntl(fileno(fp), F_GETFL, 0)) < 0) { strResult = "fcntl get error.script file: "+ strScript; fflush(fp); pclose2(fp); return false; } flags |= O_NONBLOCK; if( fcntl(fileno(fp), F_SETFL, flags) < 0 ) { strResult = "fcntl set error.script file: "+ strScript; fflush(fp); pclose2(fp); return false; } //strResult = ""; char c; time_t tNow = TC_TimeProvider::getInstance()->getNow(); while(TC_TimeProvider::getInstance()->getNow()- 2 < tNow) { usleep(200000); while((c=fgetc(fp))!=EOF ) { strResult += c; } if(sRealEndMark == "" || strResult.find(sRealEndMark) != string::npos) { LOG->info()<<"doScript "<<sCmd<<"|sEndMark "<<sRealEndMark <<" finded|"<<strResult<<endl; break; } } strResult = TC_Common::replace(strResult,"\n","\r\n"); LOG->info()<<"doScript "<<sCmd<<" result:"<<strResult<<endl; fflush(fp); pclose2(fp); mResult = parseScriptResult(strServerId,strResult); return true; }
int execute(char *command, char *request, char *obuf, size_t olen) { int ret; popen2_t ctx; char *r = request; ret = popen2(command, environ, &ctx); if (ret < 0) { fprintf(stderr, "Command '%s' failed=%d", command, ret); return -1; } int offset = 0; size_t request_len = strlen(request); int wcount = 0; #ifdef MVS r = clax_etoa_alloc(request, request_len); #endif while (1) { ret = write(ctx.in, r + offset, request_len - offset); wcount += ret; if (wcount == request_len) break; if (ret < 0 && errno == EAGAIN) { continue; } offset += ret; } #ifdef MVS free(r); #endif offset = 0; while (1) { ret = read(ctx.out, obuf + offset, olen - offset); if (ret == 0) break; if (ret < 0 && errno == EAGAIN) { continue; } offset += ret; } obuf[offset] = 0; #ifdef MVS clax_atoe(obuf, offset); #endif int exit_code = pclose2(&ctx); if (exit_code != 0) { fprintf(stderr, "Exit code=%d\n", exit_code); return -1; } return offset; }
/* ** Global initialization of the transport layer */ void transport_global_startup(void){ if( g.urlIsSsh ){ /* Only SSH requires a global initialization. For SSH we need to create ** and run an SSH command to talk to the remote machine. */ const char *zSsh; /* The base SSH command */ Blob zCmd; /* The SSH command */ char *zHost; /* The host name to contact */ char zIn[200]; /* An input line received back from remote */ zSsh = db_get("ssh-command", zDefaultSshCmd); blob_init(&zCmd, zSsh, -1); if( g.urlPort!=g.urlDfltPort ){ #ifdef __MINGW32__ blob_appendf(&zCmd, " -P %d", g.urlPort); #else blob_appendf(&zCmd, " -p %d", g.urlPort); #endif } if( g.urlUser && g.urlUser[0] ){ zHost = mprintf("%s@%s", g.urlUser, g.urlName); #ifdef __MINGW32__ /* Only win32 (and specifically PLINK.EXE support the -pw option */ if( g.urlPasswd && g.urlPasswd[0] ){ Blob pw; blob_zero(&pw); if( g.urlPasswd[0]=='*' ){ char *zPrompt; zPrompt = mprintf("Password for [%s]: ", zHost); prompt_for_password(zPrompt, &pw, 0); free(zPrompt); }else{ blob_init(&pw, g.urlPasswd, -1); } blob_append(&zCmd, " -pw ", -1); shell_escape(&zCmd, blob_str(&pw)); blob_reset(&pw); } #endif }else{ zHost = mprintf("%s", g.urlName); } blob_append(&zCmd, " ", 1); shell_escape(&zCmd, zHost); free(zHost); /* printf("%s\n", blob_str(&zCmd)); */ popen2(blob_str(&zCmd), &sshIn, &sshOut, &sshPid); if( sshPid==0 ){ fossil_fatal("cannot start ssh tunnel using [%b]", &zCmd); } blob_reset(&zCmd); /* Send an "echo" command to the other side to make sure that the ** connection is up and working. */ fprintf(sshOut, "echo test\n"); fflush(sshOut); sshin_read(zIn, sizeof(zIn)); if( memcmp(zIn, "test", 4)!=0 ){ pclose2(sshIn, sshOut, sshPid); fossil_fatal("ssh connection failed: [%s]", zIn); } } }