int copyFileAndStatX(PCStr(src),PCStr(dst),PCStr(mode)){ FILE *sfp,*dfp; CStr(xnew,1024); CStr(sav,1024); CStr(dir,1024); const char *dp; lineScan(dst,dir); if( dp = strrchr(dir,'/') ){ truncVStr(dp); if( !File_is(dir) ){ mkdirRX(dir); } } if( File_isreg(src) ){ if( sfp = dgfopen("copyFile-src","",src,"r") ){ if( strchr(mode,'a') ){ if( dfp = dgfopen("copyFile-append","",dst,mode) ){ copyfile1(sfp,dfp); fclose(dfp); File_copymod(src,xnew); fprintf(stderr,"#### appended %s to %s\n",src,dst); }else{ fprintf(stderr,"#### cannot open %s(%s)\n",dst,mode); } fclose(sfp); }else{ sprintf(xnew,"%s-new",dst); if( dfp = dgfopen("copyFile-create","",xnew,"w") ){ copyfile1(sfp,dfp); fclose(dfp); File_copymod(src,xnew); fprintf(stderr,"#### copied %s to %s\n",src,dst); }else{ fprintf(stderr,"#### cannot open %s\n",dst); } fclose(sfp); if( File_is(dst) ){ sprintf(sav,"%s-old",dst); rename(dst,sav); } rename(xnew,dst); } } }else{ CStr(path,1024); if( fullpathSUCOM("dgcpnod","r",AVStr(path)) == 0 ){ fprintf(stderr,"#### ERROR: %s not found.\n","dgcpnod"); }else{ if( fork() == 0 ){ execlp(path,path,src,dst,NULL); exit(0); } wait(0); } } return 0; }
static void notifyAdmin(Connection *Conn,FILE *admfp,FILE *infp,PCStr(adminid),PCStr(adminmbox),PCStr(event)) { CStr(mailer,128); CStr(line,1024); CStr(stime,128); FILE *fp; mailer[0] = 0; fseek(admfp,0,0); while( fgets(line,sizeof(line),admfp) != NULL ){ if( Xsscanf(line,"MAILER: %s",AVStr(mailer)) ) break; } if( mailer[0] ){ fp = SMTP_POST(mailer,25,adminmbox,MailGate(Conn)); fprintf(fp,"Subject: Notice: GACL %s\r\n",event); fprintf(fp,"\r\n"); fprintf(fp,"ACTION: %s\r\n",event); fprintf(fp,"MUID: %s\r\n",adminid); fprintf(fp,"\r\n--CONTENT--\r\n"); if( infp ){ fseek(infp,0,0); copyfile1(infp,fp); } fprintf(fp,"\r\n--REQUEST--\r\n"); HTTP_putRequest(Conn,fp); fprintf(fp,"--END--\r\n"); pclose(fp); } fseek(admfp,0,2); StrftimeLocal(AVStr(stime),sizeof(stime),"%Y/%m/%d-%H:%M:%S",time(0),0); fprintf(admfp,"[%s] %s\r\n",stime,event); fflush(admfp); }
void uploadACL(Connection *Conn,PCStr(user),FILE *tc,FILE *oldacl,FILE *newacl,PCStr(aclID),PCStr(mbox),PCStr(durl),void *env,sFUNCP ckfunc) { int errors; int osize; CStr(line,1024); fprintf(tc,"<PLAINTEXT>\r\n"); fprintf(tc,"UPLOAD/UPDATE/REMOVE Gateway Access Control List (GACL)\r\n"); fprintf(tc,"Source GACL URL: <%s>\r\n",durl); fprintf(tc,"\r\n--DIAGNOSIS--\r\n"); osize = file_size(fileno(oldacl)); if( 0 < osize ){ while( fgets(line,sizeof(line),oldacl) != 0 ){ /* skip HTTP header */ if( line[0] == '#' || line[0] == '\r' || line[0] == '\n' ) break; } setACL(Conn,0,aclID,user,durl,oldacl,tc,env,ckfunc); fseek(oldacl,0,0); } if( file_size(fileno(newacl)) <= 0 ){ if( 0 < osize ) fprintf(tc,"Your ACL is removed\r\n"); }else{ errors = setACL(Conn,1,aclID,user,durl,newacl,tc,env,ckfunc); if( errors == 0 ) fprintf(tc,"NO ERROR\r\n"); fseek(newacl,0,0); fprintf(tc,"\r\n--SOURCE--\r\n"); copyfile1(newacl,tc); } if( 0 < osize ){ fprintf(tc,"\r\n--PREVIOUS--\r\n"); copyfile1(oldacl,tc); fseek(oldacl,0,0); } fprintf(tc,"\r\n--END--\r\n"); }
static int relay_commands(LprJob *lpj,FILE *ts,FILE *fs,FILE *tc,FILE *fc) { CStr(req,1024); CStr(resp,0x8000); char op; char rcc; while( fgets(req,sizeof(req),fc) ){ op = req[0]; syslog_ERROR("C-S: < %02x %s",op,&req[1]); fputs(req,ts); fflush(ts); lpj->l_off += strlen(req); switch( op ){ case JC_SENDQSTATS: case JC_SENDQSTATL: if( fs == NULL || tc == NULL ) return op; rcc = copyfile1(fs,tc); fflush(tc); return 0; } if( fs != NULL ) IGNRETP fread(resp,1,1,fs); else resp[0] = 0; if( tc != NULL ){ putc(resp[0],tc); fflush(tc); } syslog_ERROR("S-C: > %02x\n",resp[0]); if( resp[0] != OK ) continue; if( op == JC_RECEIVE ){ relay_RECEIVE(lpj,ts,fs,tc,fc); break; } } return 0; }
void httpAdmin(Connection *Conn,PVStr(user),FILE *tc,PCStr(group),PVStr(search),void *env,iFUNCP prfunc,sFUNCP ckfunc,PVStr(adminid)) { const char *np; CStr(sub,128); CStr(dhtml,128); CStr(url,1024); CStr(durl,1024); CStr(aurl,1024); CStr(aclurl,1024); CStr(adminmbox,1024); FILE *cfp,*acl; CStr(opath,1024); FILE *admfp; CStr(line,1024); nonxalpha_unescape(search,AVStr(search),0); strdelchr(search,AVStr(search),"\"'<>"); sub[0] = 0; Xsscanf(search,"Admin=%[^&]",AVStr(sub)); if( np = strstr(search,"USER="******"USER=%[^&]",AVStr(user)); setVStrEnd(adminid,0); if( np = strstr(search,"ADMINID=") ) Xsscanf(np,"ADMINID=%[^&]",AVStr(adminid)); url[0] = 0; if( np = strstr(search,"URL=") ) Xsscanf(np,"URL=%[^&]",AVStr(url)); nonxalpha_unescape(url,AVStr(durl),0); /* if( !streq(user,"anonymous") ){ fprintf(tc,"you cannot control %s users\r\n",)user); return; } */ if( streq(sub,"GetACL") || streq(sub,"PutACL") ){ CStr(line,1024); if( adminid[0] == 0 ){ fprintf(tc,"You must show your <I>Admin-ID</I>.\r\n"); return; } if( streq(sub,"PutACL") ) if( durl[0] == 0 ){ fprintf(tc,"You must show your <I>ACL-URL</I> (URL of your GACL).\r\n"); return; }else if( durl[0] ){ cfp = tmpfile(); URLget(durl,1,cfp); if( file_size(fileno(cfp)) <= 0 ){ fprintf(tc,"Your GACL at <%s> is not accessible\r\n",durl); return; } } if( (admfp = openMuid(0,adminid,AVStr(adminmbox))) == NULL ){ fprintf(tc,"The <I>Admin-ID</I> shown [%s] does not exist.\r\n",adminid); return; } if( streq(sub,"GetACL") ) if( (acl = openGACL(0,adminid)) && 0 < file_size(fileno(acl)) ){ line[0] = 0; while( fgets(line,sizeof(line),acl) != NULL ){ if( line[0] == '#' ){ fputs(line,tc); break; } if( line[0] == '\r' || line[0] == '\n' ) break; } if( line[0] != '#' ) /* maybe template */ fprintf(tc,"#<PLAINTEXT>\r\n"); copyfile1(acl,tc); fflush(tc); notifyAdmin(Conn,admfp,acl,adminid,adminmbox,"downloaded"); return; } acl = openGACL(1,adminid); line[0] = 0; fgets(line,sizeof(line),acl); fseek(acl,0,0); if( streq(sub,"GetACL") ){ putBuiltinHTML(Conn,acl,"NNTP/HTTP-Gateway-Admin","news/adminNewACL.dhtml",NULL,prfunc,env); fflush(acl); fseek(acl,0,0); copyfile1(acl,tc); notifyAdmin(Conn,admfp,acl,adminid,adminmbox,"created+downloaded"); fclose(acl); fflush(tc); return; } if( line[0] == 0 || line[0] == '#' ){ fseek(admfp,0,2); fprintf(admfp,"ACL-URL: %s\r\n",durl); fflush(admfp); }else{ /* check consistensy of ACL-URL */ } uploadACL(Conn,user,tc,acl,cfp,adminid,adminmbox,durl,env,ckfunc); fseek(cfp,0,0); copyfile1(cfp,acl); Ftruncate(acl,0,1); fflush(tc); notifyAdmin(Conn,admfp,cfp,adminid,adminmbox,"uploaded"); fclose(cfp); fclose(acl); fclose(admfp); } sprintf(dhtml,"news/admin%s.dhtml",sub); putBuiltinHTML(Conn,tc,"NNTP/HTTP-Gateway-Admin",dhtml,NULL,prfunc,env); }
/** Main program: copies a file. @param argc Number of command-line arguments (including program name). @param argv Array of pointers to character arays holding arguments. @return 0 if successful, 1 if fail. */ int main(int argc, char* argv[]) { char** infilenames; // Names of files. char* outfilename; int fileCount; int returnstatus; int paramIndex; char method; int bufferSize; int i; //An iterator struct timeval tcpstart; struct timeval tcpfinish; if (argc < 3) { usage(argv[0]); // Must have at least 3 arguments return 1; } //search for copy type if ((paramIndex = searchArrayForString(argv,argc,"-c")) != -1) { if (paramIndex+1 == argc) { printf("Missing parameter. Program terminated.\n"); return 1; } method = atoi(argv[paramIndex+1]); if (method != 1 && method != 2 && method != 3) { printf("Invalid copymethod argument. Valid options are 1, 2, or 3. You entered \"%s\". Program Terminated.\n",argv[paramIndex+1]); return 1; } } else { method = 3; //set default method } //Set buffer size if (method == 3) { if ((paramIndex = searchArrayForString(argv,argc,"-b")) != -1) { if (paramIndex+1 == argc) { printf("Missing parameter. Program terminated.\n"); return 1; } bufferSize = atoi(argv[paramIndex+1]); if (bufferSize <= 0) { printf("Invalid buffer Size. Program terminatred\n"); return 1; } } else { bufferSize = 1024; //set default buffer size } } //find file names if ((paramIndex = findFirstFile(argv,argc)) == -1) { printf("No file names entered. Program Terminated.\n"); return 1; } if ((fileCount = countInputFiles(argv,argc)) < 1) { printf("At least two file names or locations must be specified. Program terminated\n"); return 1; } infilenames = (char**) malloc(sizeof(char*)*fileCount); //Make space for a list of the files to copy //Fill an array of files to copy for (i = 0; i < fileCount; i++) { infilenames[i] = argv[paramIndex + i]; } outfilename = argv[paramIndex + fileCount]; //Copy the files based on the current method gettimeofday(&tcpstart,NULL); switch(method) { case 1: returnstatus = copyfile1(infilenames, outfilename, fileCount); break; case 2: returnstatus = copyfile2(infilenames, outfilename, fileCount); break; case 3: returnstatus = copyfile3(infilenames, outfilename, fileCount, bufferSize); break; } gettimeofday(&tcpfinish,NULL); struct timeval* tcpDifference = timeDifference(&tcpstart, &tcpfinish); if (!returnstatus) { printf("\nCopy started: %d seconds and %d microseconds from UTC start.\n",tcpstart.tv_sec,tcpstart.tv_usec); printf("Copy finished: %d seconds and %d microseconds from UTC start.\n",tcpfinish.tv_sec,tcpfinish.tv_usec); printf("Time for Copy: %d seconds and %d microseconds.\n",tcpDifference->tv_sec,tcpDifference->tv_usec); } free(tcpDifference); free(infilenames); return returnstatus; }
void notify_ADMINX(Connection *xConn,PCStr(admin),PCStr(what),PCStr(body)) { FILE *tmp; FILE *bt; Connection ConnBuff,*Conn = &ConnBuff; CStr(head,1024); CStr(me,128); CStr(date,128); CStr(load,128); CStr(cwd,1024); CStr(uname,128); int now; const char *bugbox = "*****@*****.**"; CStr(msgid,1024); CStr(buf,1024); if( strncasecmp(what,"sig",3) != 0 ) if( strncasecmp(what,"failed",6) != 0 ) if( strncasecmp(what,"modified",8) != 0 ) if( strncasecmp(what,"approved",8) != 0 ) if( strncasecmp(what,"detected",8) != 0 ) if( strncasecmp(what,"[",1) != 0 ) /* abort in child process */ return; if( admin == NULL || *admin == 0 ) admin = getADMIN1(); now = time(NULL); if( xConn ) *Conn = *xConn; else bzero(Conn,sizeof(Connection)); tmp = TMPFILE("NOTIFY"); head[0] = 0; if( gethostname(me,sizeof(me)) != 0 ) strcpy(me,"?"); sprintf(msgid,"%d.%d.%d@%s",getpid(),itime(0),getuid(),me); StrftimeLocal(AVStr(date),sizeof(date),TIMEFORM_RFC822,now,0); Xsprintf(TVStr(head),"Subject: DeleGate-Report: %s\r\n",what); Xsprintf(TVStr(head),"From: [%d]@%s\r\n",getuid(),me); Xsprintf(TVStr(head),"To: %s (DeleGate ADMIN)\r\n",admin); Xsprintf(TVStr(head),"Reply-To: %s\r\n",bugbox); Xsprintf(TVStr(head),"Date: %s\r\n",date); Xsprintf(TVStr(head),"Message-Id: <%s>\r\n",msgid); Xsprintf(TVStr(head),"Content-Type: text/plain\r\n"); fprintf(tmp,"%s\r\n",head); fprintf(tmp,"PLEASE FORWARD THIS MESSAGE TO <%s>.\r\n",bugbox); fprintf(tmp,"IT WILL BE HELPFUL FOR DEBUGGING.\r\n"); fprintf(tmp,"\r\n"); fprintf(tmp,"%s",head); fprintf(tmp,"Event: %s\r\n",what); Uname(AVStr(uname)); fprintf(tmp,"Version: %s (%s)\r\n",DELEGATE_verdate(),uname); fprintf(tmp,"Host: %s\r\n",me); fprintf(tmp,"Owner: uid=%d/%d, gid=%d/%d\r\n", geteuid(),getuid(),getegid(),getgid()); StrftimeLocal(AVStr(date),sizeof(date),TIMEFORM_RFC822,START_TIME,0); fprintf(tmp,"Started: %s\r\n",date); fprintf(tmp,"Pid: %d\r\n",getpid()); cwd[0] = 0; IGNRETS getcwd(cwd,sizeof(cwd)); fprintf(tmp,"Cwd: %s\r\n",cwd); strfLoadStat(AVStr(load),sizeof(load),"%L (%l)",now); fprintf(tmp,"Load: %s\r\n",load); fprintf(tmp,"Stack: %X %d/%d\r\n",p2i(STACK_PEAK),ll2i(STACK_BASE-STACK_PEAK), STACK_SIZE); fprintf(tmp,"\r\n"); if( iamServer() ) { } else { fprintf(tmp,"Client-Proto: %s\r\n",CLNT_PROTO); fprintf(tmp,"Client-Host: %s:%d\r\n",Client_Addr(buf),Client_Port); if( TeleportHost[0] ) fprintf(tmp,"Rident-Host: %s:%d..%s:%d\r\n",TelesockHost,TelesockPort, TeleportAddr,TeleportPort); fprintf(tmp,"\r\n"); } fprintf(tmp,"%s\r\n",body); /* if( strncasecmp(what,"sig",3) == 0 || *what == '[' ){ */ if( strncasecmp(what,"sig",3) == 0 || strncasecmp(what,"failed",6) == 0 || *what == '[' ) { fprintf(tmp,"--iLog--begin\r\n"); iLOGdump1(tmp,0); fprintf(tmp,"--iLog--end\r\n"); fprintf(tmp,"\r\n"); fprintf(tmp,"--AbortLog--begin\r\n"); putAbortLog(tmp); fprintf(tmp,"--AbortLog--end\r\n"); } if( strncasecmp(what,"sig",3) == 0 ) { int btout[2]; double Start = Time(); int rcc; fprintf(tmp,"\r\n"); fprintf(tmp,"--BackTrace--begin\r\n"); fflush(tmp); if( pipe(btout) == 0 ) { setNonblockingIO(btout[1],1); bt = fdopen(btout[1],"w"); putBackTrace(bt); fclose(bt); bt = fdopen(btout[0],"r"); rcc = file_copyTimeout(bt,tmp,NULL,128*1024,NULL,15); fclose(bt); sv1log("BatckTrace: %dB / %.1fs\n",rcc,Time()-Start); } else { bt = TMPFILE("BackTrace"); putBackTrace(bt); fseek(bt,0,0); copyfile1(bt,tmp); fclose(bt); } fprintf(tmp,"\r\n"); fprintf(tmp,"--BackTrace--end\r\n"); } fflush(tmp); fseek(tmp,0,0); if( curLogFp() ) { CStr(line,1024); while( fgets(line,sizeof(line),tmp) != NULL ) fprintf(curLogFp(),"--ABORT-- %s",line); fseek(tmp,0,0); } Conn->co_mask = (CONN_NOPROXY | CONN_NOMASTER); sendmail1(Conn,admin,admin,tmp,NULL); fclose(tmp); }
static void relay_RECEIVE(LprJob *lpj,FILE *ts,FILE *fs,FILE *tc,FILE *fc) { CStr(req,1024); CStr(resp,1); char op; const char *buff; int size,rcc; FILE *ts_sav,*fs_sav; while( fgets(req,sizeof(req),fc) ){ op = req[0]; syslog_ERROR("C-S: < RECEIVE %02x %s",op,&req[1]); fputs(req,ts); fflush(ts); lpj->l_off += strlen(req); if( fs != NULL ){ IGNRETP fread(resp,1,1,fs); syslog_ERROR("S-C: > RECEIVE %02x (%s)\n",resp[0], resp[0]==OK?"OK":"ERROR"); }else{ resp[0] = OK; } if( tc != NULL ){ fwrite(resp,1,1,tc); fflush(tc); } if( op == RJ_CONTROL || op == RJ_DATA ){ size = atoi(req+1); if( size == 0 ){ rcc = copyfile1(fc,ts); fflush(ts); lpj->l_data_off = lpj->l_off; lpj->l_data_siz = rcc; lpj->l_off += rcc; if( fs != NULL ) IGNRETP fread(resp,1,1,fs); else resp[0] = OK; if( tc != NULL ) fwrite(resp,1,1,tc); break; } rcc = size; buff = freadfile(fc,&rcc); if( rcc != size ) break; if( op == RJ_CONTROL ){ lpj->l_ctrl_off = lpj->l_off; lpj->l_ctrl_siz = rcc; }else{ lpj->l_data_off = lpj->l_off; lpj->l_data_siz = rcc; } fwrite(buff,1,size,ts); free((char*)buff); fflush(ts); lpj->l_off += rcc; IGNRETP fread(resp,1,1,fc); fwrite(resp,1,1,ts); fflush(ts); lpj->l_off += 1; if( fs != NULL ) IGNRETP fread(resp,1,1,fs); else resp[0] = OK; if( tc != NULL ){ fwrite(resp,1,1,tc); fflush(tc); } syslog_ERROR("S-C: > RECEIVE DATA %02x %s\n",resp[0], resp[0]==OK?"OK":"ERROR"); } } }