void ListAppendItems(ListNode *Dest, ListNode *Src, LIST_ITEM_CLONE_FUNC ItemCloner) { ListNode *Curr; void *Item; Curr=ListGetNext(Src); while (Curr !=NULL) { if (ItemCloner) { Item=ItemCloner(Curr->Item); ListAddNamedItem(Dest, Curr->Tag, Item); } else ListAddNamedItem(Dest, Curr->Tag, Curr->Item); Curr=ListGetNext(Curr); } }
void STREAMSetItem(STREAM *S, const char *Name, void *Value) { ListNode *Curr; if (! S->Items) S->Items=ListCreate(); Curr=ListFindNamedItem(S->Items,Name); if (Curr) Curr->Item=Value; else ListAddNamedItem(S->Items,Name,Value); }
static const char *ParserYAMLItems(int ParserType, const char *Doc, ListNode *Parent, int IndentLevel) { const char *ptr, *tptr; char *Token=NULL, *PrevToken=NULL, *Name=NULL; int count=0, BreakOut=FALSE; ListNode *Node; ptr=GetToken(Doc, "\n|#|[|]|{|}|:",&Token,GETTOKEN_MULTI_SEP|GETTOKEN_INCLUDE_SEP|GETTOKEN_HONOR_QUOTES); while (ptr) { switch(*Token) { case ':': Name=CopyStr(Name, PrevToken); PrevToken=CopyStr(PrevToken,""); break; case '#': while ((*ptr != '\0') && (*ptr != '\n')) ptr++; break; case '\n': if (StrValid(PrevToken)) { StripTrailingWhitespace(PrevToken); StripLeadingWhitespace(PrevToken); Node=ListAddNamedItem(Parent, Name, CopyStr(NULL, PrevToken)); Node->ItemType=ITEM_VALUE; } count=0; for (tptr=ptr; *tptr==' '; tptr++) count++; if (count > IndentLevel) ptr=ParserAddNewStructure(ParserType, tptr, Parent, ITEM_ENTITY, Name, count); else if (count < IndentLevel) BreakOut=TRUE; PrevToken=CopyStr(PrevToken,""); break; default: PrevToken=CopyStr(PrevToken, Token); break; } if (BreakOut) { break; } ptr=GetToken(ptr, "\n|#[|]|{|}|:",&Token,GETTOKEN_MULTI_SEP|GETTOKEN_INCLUDE_SEP|GETTOKEN_HONOR_QUOTES); } DestroyString(PrevToken); DestroyString(Token); DestroyString(Name); return(ptr); }
void DataProcessorSetValue(TProcessingModule *M, const char *Name, const char *Value) { char *ptr; ListNode *Curr; if (! M->Values) M->Values=ListCreate(); Curr=ListFindNamedItem(M->Values,Name); if (Curr) Curr->Item = (void *) CopyStr( (char *) Curr->Item, Value); else ListAddNamedItem(M->Values,Name,CopyStr(NULL,Value)); }
//This parses a 'standard crayonization', which means those things that //can occur anywhere in the config, not just at the highest level static TCrayon *ParseCrayonization(const char *Type, const char *Config, ListNode *CrayonList) { TCrayon *Crayon=NULL, *CLS=NULL, *Action=NULL; Crayon=(TCrayon *) calloc(1,sizeof(TCrayon)); ParseCrayonEntry(Crayon, Type, Config); ListAddNamedItem(CrayonList,Crayon->Match,Crayon); if (Crayon->Type==CRAYON_STATUSBAR) { //Add a CLS entry CLS=(TCrayon *) calloc(1,sizeof(TCrayon)); ParseCrayonEntry(CLS, "string", "[2J"); //ParseCrayonEntry(CLS, "string", "\x1b[;H"); Action=NewCrayonAction(Crayon,0); Action->ActType=ACTION_REDRAW; ListAddNamedItem(CrayonList,CLS->Match,CLS); } return(Crayon); }
void SetVar(ListNode *Vars, const char *Name, const char *Data) { ListNode *Node; char *Tempstr=NULL; Tempstr=CopyStr(Tempstr,Name); //strlwr(Tempstr); Node=ListFindNamedItem(Vars,Tempstr); if (Node) Node->Item=(void *) CopyStr((char *) Node->Item,Data); else ListAddNamedItem(Vars,Tempstr,CopyStr(NULL,Data)); DestroyString(Tempstr); }
const char *ParserAddNewStructure(int ParserType, const char *Data, ListNode *Parent, int Type, const char *Name, int IndentLevel) { ListNode *Item, *Node; char *Token=NULL; const char *ptr; Item=ListCreate(); Item->Tag=CopyStr(Item->Tag,Name); if (Name) Node=ListAddNamedItem(Parent, Name, Item); else { if (StrValid(Parent->Tag)) Token=FormatStr(Token, "%s:%d",Parent->Tag, ListSize(Parent)); else Token=FormatStr(Token, "item:%d",ListSize(Parent)); Node=ListAddNamedItem(Parent, Token, Item); } Node->ItemType=Type; ptr=ParserParseItems(ParserType, Data, Item, IndentLevel); DestroyString(Token); return(ptr); }
static void CGIDisplayOptions(const char *HashType, const char *Encoding, const char *LineEnding, int OutputLength) { char *Token=NULL, *Tempstr=NULL; const char *ptr; ListNode *Items=NULL; int i; Items=ListCreate(); printf("<tr>\r\n"); Tempstr=HashAvailableTypes(Tempstr); ptr=GetToken(Tempstr, ",",&Token,0); while (ptr) { ListAddNamedItem(Items, Token, CopyStr(NULL, Token)); ptr=GetToken(ptr, ",",&Token,0); } printf("<td align=left>Type: "); CGIPrintSelect("HashType", HashType, Items); ListClear(Items, Destroy); printf("</td>\r\n"); printf("<td align=right>Encoding: "); for (i=0; EncodingNames[i] !=NULL; i++) SetVar(Items, EncodingNames[i], EncodingDescriptions[i]); CGIPrintSelect("Encoding", Encoding, Items); ListClear(Items, Destroy); printf("</td>\r\n"); printf("<tr>\r\n"); printf("<td align=left>Line Ending: </td>"); printf("<td align=right>"); for (i=0; LineEndingNames[i] !=NULL; i++) SetVar(Items, LineEndingNames[i], LineEndingDescriptions[i]); CGIPrintSelect("LineEnding", LineEnding, Items); ListClear(Items, Destroy); printf("</td>\r\n"); printf("</tr>\r\n"); printf("<tr>\r\n"); printf("<td align=left>Hash Length: </td>"); printf("<td align=right>"); if (OutputLength > 0) printf("<input type=text width=90%% name=\"OutputLength\" style=\"font-weight: bold; font-size:16px\" value=\"%d\">\r\n",OutputLength); else printf("<input type=text width=90%% name=\"OutputLength\" style=\"font-weight: bold; font-size:16px\">\r\n"); printf("</td>\r\n"); printf("</tr>\r\n"); ListDestroy(Items, Destroy); Destroy(Tempstr); Destroy(Token); }
int IDriveLoadDir(TFileStore *FS, char *InPattern, ListNode *Items, int Flags) { int result; char *Tempstr=NULL, *XML=NULL; char *TagName=NULL, *TagData=NULL, *ptr; TFileInfo *FI; HTTPInfoStruct *Info; ListNode *Vars; Tempstr=MCopyStr(Tempstr,"https://",FS->Host,"/evs/browseFolder?uid=",FS->Logon,"&pwd=",FS->Passwd,"&p=",FS->CurrDir,NULL); FS->S=HTTPMethod("POST",Tempstr,"","","","",0); Tempstr=STREAMReadDocument(Tempstr, FS->S, TRUE); ptr=XMLGetTag(Tempstr,NULL,&TagName,&TagData); while (ptr) { if (strcmp(TagName,"item")==0) { FI=IDriveReadFileEntry(TagData); if (Items) ListAddNamedItem(Items,FI->Name,FI); } ptr=XMLGetTag(ptr,NULL,&TagName,&TagData); } STREAMClose(FS->S); FS->S=NULL; Tempstr=MCopyStr(Tempstr,"https://",FS->Host,"/evs/getAccountQuota?uid=",FS->Logon,"&pwd=",FS->Passwd,NULL); FS->S=HTTPMethod("POST",Tempstr,"","","","",0); Tempstr=STREAMReadDocument(Tempstr, FS->S, TRUE); Vars=ListCreate(); IDriveParseResponse(Tempstr,Vars); FS->BytesAvailable=strtod(GetVar(Vars,"totalquota"),NULL); FS->BytesUsed=strtod(GetVar(Vars,"usedquota"),NULL); ListDestroy(Vars,DestroyString); DestroyString(TagName); DestroyString(TagData); DestroyString(Tempstr); DestroyString(XML); return(TRUE); }
OAUTH *OAuthCreate(const char *Type, const char *Name, const char *ClientID, const char *ClientSecret, const char *Scopes, const char *RefreshURL) { OAUTH *Ctx; char *Tempstr=NULL, *Token=NULL; const char *ptr; if (OAuthTypes==NULL) SetupOAuthTypes(); ptr=GetVar(OAuthTypes, Type); if (! StrValid(ptr)) return(NULL); Ctx=(OAUTH *) calloc(1,sizeof(OAUTH)); ptr=GetToken(ptr,",",&(Ctx->Stage1), 0); ptr=GetToken(ptr,",",&(Ctx->Stage2), 0); ptr=GetToken(ptr,",",&(Ctx->VerifyTemplate), 0); Ctx->Name=CopyStr(Ctx->Name, Name); Ctx->Vars=ListCreate(); SetVar(Ctx->Vars,"client_name",ClientID); SetVar(Ctx->Vars,"client_id",ClientID); SetVar(Ctx->Vars,"client_secret",ClientSecret); Tempstr=HTTPQuote(Tempstr, Scopes); SetVar(Ctx->Vars,"scope",Tempstr); SetVar(Ctx->Vars,"redirect_uri","urn:ietf:wg:oauth:2.0:oob"); SetVar(Ctx->Vars,"connect_back_page","<html><body><h1>Code Accepted By Application</h1><body></html>"); Ctx->AccessToken=CopyStr(Ctx->AccessToken, ""); Ctx->RefreshToken=CopyStr(Ctx->RefreshToken, ""); Ctx->RefreshURL=CopyStr(Ctx->RefreshURL, RefreshURL); Ctx->SavePath=MCopyStr(Ctx->SavePath, GetCurrUserHomeDir(), "/.oauth.creds",NULL); if (strcasecmp(Type, "getpocket.com")==0) { ptr=GetToken(ClientID,"-",&Token,0); Tempstr=MCopyStr(Tempstr, "pocketapp",Token,":authorizationFinished",NULL); SetVar(Ctx->Vars, "redirect_uri",Tempstr); //Ctx->VerifyURL=MCopyStr(Ctx->VerifyURL,"https://getpocket.com/auth/authorize?request_token=",Ctx->AccessToken,"&redirect_uri=",Args,NULL); } else if (strcasecmp(Type, "implicit")==0) Ctx->Flags |= OAUTH_IMPLICIT; if (! OAuthKeyChain) OAuthKeyChain=ListCreate(); ListAddNamedItem(OAuthKeyChain, Name, Ctx); DestroyString(Tempstr); DestroyString(Token); return(Ctx); }
void ConfigFileSaveFileStore(TFileStore *FS) { ListNode *Curr; TFileStore *Old; Curr=ListFindNamedItem(FileStores,FS->Name); if (Curr) { Old=(TFileStore *) Curr->Item; DestroyFileStore(Old); Curr->Item=FileStoreClone(FS); } else ListAddNamedItem(FileStores,FS->Name,FileStoreClone(FS)); ConfigFileSaveFileStores(); }
void ParseEventConfig(char *ConfigLine) { const char *EventTypeStrings[]={"Method","Path","User","ClientIP","BadURL","Header","ResponseCode",NULL}; char *Token=NULL, *ptr; ListNode *Node; int Type; if (! Settings.Events) Settings.Events=ListCreate(); ptr=GetToken(ConfigLine,":",&Token,0); Type=MatchTokenFromList(Token,EventTypeStrings,0); ptr=GetToken(ptr,":",&Token,0); Node=ListAddNamedItem(Settings.Events,Token,CopyStr(NULL,ptr)); Node->ItemType=Type; DestroyString(Token); }
ListNode *ListClone(ListNode *ListStart, LIST_ITEM_CLONE_FUNC ItemCloner) { ListNode *Curr,*NewList; void *Item; if (! ItemCloner) return(NULL); NewList=ListCreate(); Curr=ListGetNext(ListStart); while (Curr !=NULL) { if (ItemCloner) { Item=ItemCloner(Curr->Item); ListAddNamedItem(NewList,Curr->Tag,Item); } Curr=ListGetNext(Curr); } return(NewList); }
void ExtractVarsReadVar(const char **Fmt, const char **Msg, ListNode *Vars) { const char *FmtPtr, *MsgPtr; char *VarName=NULL; int len=0; ListNode *Node; FmtPtr=*Fmt; if (*FmtPtr=='(') FmtPtr++; while (*FmtPtr != ')') { VarName=AddCharToBuffer(VarName,len,*FmtPtr); len++; FmtPtr++; } if (*FmtPtr==')') FmtPtr++; MsgPtr=*Msg; while ((*MsgPtr !=0) && (*MsgPtr != *FmtPtr)) { if (*MsgPtr=='"') { do { MsgPtr++; } while ((*MsgPtr != '"') && (*MsgPtr != 0)); } MsgPtr++; } Node=ListFindNamedItem(Vars,VarName); if (Node) Node->Item=(void *) CopyStrLen((char *) Node->Item, *Msg, MsgPtr-*Msg); else Node=ListAddNamedItem(Vars,VarName,CopyStrLen(NULL, *Msg, MsgPtr-*Msg)); *Fmt=FmtPtr; *Msg=MsgPtr; DestroyString(VarName); }
TLogFile *LogFileGetEntry(const char *FileName) { ListNode *Node; TLogFile *LogFile=NULL; STREAM *S=NULL; if (! StrLen(FileName)) return(NULL); if (! LogFiles) LogFiles=ListCreate(); if (! LogFileDefaults) LogFileSetupDefaults(); Node=ListFindNamedItem(LogFiles,FileName); if (Node) LogFile=(TLogFile *) Node->Item; else { if (strcmp(FileName,"STDOUT")==0) S=STREAMFromFD(1); else if (strcmp(FileName,"STDERR")==0) S=STREAMFromFD(2); else if (strcmp(FileName,"SYSLOG")==0) S=STREAMCreate(); else { S=STREAMOpenFile(FileName,SF_CREAT | SF_APPEND | SF_WRONLY); } if (S) { LogFile=(TLogFile *) calloc(1,sizeof(TLogFile)); LogFile->Path=CopyStr(LogFile->Path,FileName); LogFile->LogFacility=LogFileDefaults->LogFacility; LogFile->Flags=LogFileDefaults->Flags; LogFile->MaxSize=LogFileDefaults->MaxSize; LogFile->S=S; if (strcmp(FileName,"SYSLOG")==0) LogFile->Flags |= LOGFILE_SYSLOG; ListAddNamedItem(LogFiles,FileName,LogFile); STREAMSetItem(S,"TLogFile",LogFile); } } return(LogFile); }
void HTTPParseHeader(STREAM *S, HTTPInfoStruct *Info, char *Header) { char *Token=NULL, *Tempstr=NULL; int count=0; int result; char *ptr; if (Info->Flags & HTTP_DEBUG) fprintf(stderr,"HEADER: %s\n",Header); ptr=GetToken(Header,":",&Token,0); while (isspace(*ptr)) ptr++; Tempstr=MCopyStr(Tempstr,"HTTP:",Token,NULL); STREAMSetValue(S,Tempstr,ptr); ListAddNamedItem(Info->ServerHeaders,Token,CopyStr(NULL,ptr)); if (StrLen(Token) && StrLen(ptr)) { if (strcasecmp(Token,"Location")==0) { if ( (strncasecmp(ptr,"http:",5)==0) || (strncasecmp(ptr,"https:",6)==0) ) { Info->RedirectPath=CopyStr(Info->RedirectPath,ptr); } else Info->RedirectPath=FormatStr(Info->RedirectPath,"http://%s:%d%s",Info->Host,Info->Port,ptr); } else if (strcasecmp(Token,"Content-length")==0) { Info->ContentLength=atoi(ptr); } else if (strcasecmp(Token,"Content-type")==0) { Info->ContentType=CopyStr(Info->ContentType,ptr); } else if (strcasecmp(Token,"WWW-Authenticate")==0) HTTPHandleWWWAuthenticate(Info,ptr); else if (strcasecmp(Token,"Set-Cookie")==0) HTTPParseCookie(Info,ptr); else if (strcasecmp(Token,"Date")==0) Info->Timestamp=CopyStr(Info->Timestamp,ptr); else if (strcasecmp(Token,"Connection")==0) { if (strcasecmp(ptr,"Close")==0) Info->Flags &= ~HTTP_KEEPALIVE; } else if ( (strcasecmp(Token,"Transfer-Encoding")==0) ) { if (! (Info->Flags & HTTP_NODECODE)) { strlwr(ptr); if (strstr(ptr,"chunked")) { Info->Flags |= HTTP_CHUNKED; } } } else if ((strcasecmp(Token,"Content-Encoding")==0) ) { if (! (Info->Flags & HTTP_NODECODE)) { strlwr(ptr); if ( (strcmp(ptr,"gzip")==0) || (strcmp(ptr,"x-gzip")==0) ) { Info->Flags |= HTTP_GZIP; } if ( (strcmp(ptr,"deflate")==0) ) { Info->Flags |= HTTP_DEFLATE; } } } } DestroyString(Token); DestroyString(Tempstr); }
static const char *ParserConfigItems(int ParserType, const char *Doc, ListNode *Parent, int IndentLevel) { const char *ptr; char *Token=NULL, *PrevToken=NULL, *Name=NULL; ListNode *Node; int BreakOut=FALSE; ptr=Doc; while (ptr && (! BreakOut)) { //while (isspace(*ptr)) ptr++; ptr=GetToken(ptr, CONFIG_FILE_TOKENS, &Token,GETTOKEN_MULTI_SEP|GETTOKEN_INCLUDE_SEP|GETTOKEN_HONOR_QUOTES); switch (*Token) { case '#': ptr=GetToken(ptr,"\n",&Token,0); break; case '{': StripLeadingWhitespace(Name); StripTrailingWhitespace(Name); ptr=ParserAddNewStructure(ParserType, ptr, Parent, ITEM_ENTITY, Name, IndentLevel+1); Name=CopyStr(Name,""); Token=CopyStr(Token,""); break; case '}': BreakOut=TRUE; break; case ' ': case ' ': case ':': case '=': Name=CopyStr(Name, PrevToken); ptr=GetToken(ptr,"\n|;|}|{",&Token,GETTOKEN_MULTI_SEP | GETTOKEN_INCLUDE_SEP | GETTOKEN_HONOR_QUOTES); break; case '\r': break; case '\n': if (ParserConfigCheckForBrace(&ptr)) { Name=MCatStr(Name, " ", PrevToken, NULL); break; } case ';': if (StrValid(PrevToken)) { StripLeadingWhitespace(PrevToken); StripTrailingWhitespace(PrevToken); if (StrValid(Name)) { Node=ListAddNamedItem(Parent, Name, CopyStr(NULL, PrevToken)); Node->ItemType=ITEM_VALUE; } Name=CopyStr(Name,""); //we don't want \r \n or ; tokens included in anything Token=CopyStr(Token,""); } break; default: break; } PrevToken=CopyStr(PrevToken, Token); } DestroyString(PrevToken); DestroyString(Token); DestroyString(Name); return(ptr); }
void VPathParse(ListNode *List, const char *PathType, const char *Data) { const char *PathTypes[]={"Files","Cgi","Websocket","Stream","Logout","Proxy","MimeIcons","FileType",NULL}; char *URL=NULL, *Path=NULL, *Tempstr=NULL; char *User=NULL, *Group=NULL, *Password=NULL; const char *ptr; TPathItem *PI=NULL; int Type, Flags=0; unsigned int CacheTime=0; Type=MatchTokenFromList(PathType,PathTypes,0); if (Type > -1) { ptr=GetToken(Data,",",&Tempstr,0); StripLeadingWhitespace(Tempstr); if (*Tempstr !='/') URL=MCopyStr(URL,"/",Tempstr,NULL); else URL=CopyStr(URL,Tempstr); ptr=GetToken(ptr,",",&Tempstr,0); while (ptr) { StripLeadingWhitespace(Tempstr); if (strncasecmp(Tempstr,"cache=",6)==0) CacheTime=atoi(Tempstr+6); else if (strncasecmp(Tempstr,"user="******"pass="******"passwd=",7)==0) Password=CopyStr(Password, Tempstr+7); else if (strncasecmp(Tempstr,"password="******"group=",6)==0) Group=CopyStr(Group, Tempstr+6); else if ( (strncasecmp(Tempstr,"exec=",5)==0) && YesNoTrueFalse(Tempstr+5)) Flags |= PATHITEM_EXEC; else if ( (strncasecmp(Tempstr,"upload=",7)==0) && YesNoTrueFalse(Tempstr+7)) Flags |= PATHITEM_UPLOAD; else if ( (strncasecmp(Tempstr,"uploads=",8)==0) && YesNoTrueFalse(Tempstr+8)) Flags |= PATHITEM_UPLOAD; else if (strncasecmp(Tempstr,"compress=",9)==0) { if (YesNoTrueFalse(Tempstr+9)) Flags |= PATHITEM_COMPRESS; else Flags |= PATHITEM_NO_COMPRESS; } else { if (StrLen(Path)) Path=MCatStr(Path, ":", Tempstr,NULL); else Path=CopyStr(Path, Tempstr); } ptr=GetToken(ptr,",",&Tempstr,0); } PI=PathItemCreate(Type, URL, Path); PI->Flags=Flags; PI->CacheTime=CacheTime; PI->User=CopyStr(PI->User, User); PI->Password=CopyStr(PI->Password, Password); PI->Group=CopyStr(PI->Group, Group); switch (PI->Type) { case PATHTYPE_LOGOUT: Settings.Flags |= FLAG_LOGOUT_AVAILABLE; break; case PATHTYPE_FILETYPE: ptr=PI->URL; if (*ptr=='/') ptr++; PI->Path=CopyStr(PI->Path, ptr); break; } ListAddNamedItem(List,PI->URL,PI); } else LogToFile(Settings.LogPath,"ERROR: Unknown Path type '%s' in Config File",Tempstr); DestroyString(Tempstr); DestroyString(Password); DestroyString(Group); DestroyString(User); DestroyString(Path); DestroyString(URL); }
void HandleTransferResult(TTransferContext *Ctx, TFileInfo *FI, int result) { TFileInfo *NewFI; char *Tempstr=NULL, *ResultStr=NULL; int val; //A 'show' command might result in this being called with no 'Ctx->DestFS' if (! Ctx->DestFS) return; switch (result) { case TRANSFER_OKAY: NewFI=FileInfoClone(FI); NewFI->Path=MCopyStr(NewFI->Path,Ctx->DestFS->CurrDir,NewFI->Name,NULL); ListAddNamedItem(Ctx->DestFS->DirListing,FI->Name,NewFI); if (FI->Type==FTYPE_DIR) { if (Ctx->CmdFlags & FLAG_CMD_QUIET) ResultStr=CopyStr(ResultStr,""); else ResultStr=FormatStr(ResultStr,"OKAY: %s directory '%s'",Ctx->ActionName,FI->Name); } else { if (Settings.Flags & FLAG_INTEGRITY_CHECK) val=FileStoreCompareFile(Ctx->SrcFS,Ctx->DestFS,FI,NewFI,Ctx->IntegrityLevel); else val=CMP_EXISTS; if (val==CMP_FAIL) { ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' integrity check failed",Ctx->ActionName,FI->Name); result=FALSE; } else if (val==CMP_MD5) ResultStr=FormatStr(ResultStr,"OKAY: %s '%s' confirmed with MD5",Ctx->ActionName,FI->Name); else if (val==CMP_SHA1) ResultStr=FormatStr(ResultStr,"OKAY: %s '%s' confirmed with SHA1",Ctx->ActionName,FI->Name); else if (val==CMP_SIZE) ResultStr=FormatStr(ResultStr,"OKAY: %s '%s' confirmed with filesize",Ctx->ActionName,FI->Name); else if (val==CMP_SIZE_TIME) ResultStr=FormatStr(ResultStr,"OKAY: %s '%s' confirmed with filesize & time",Ctx->ActionName,FI->Name); else ResultStr=FormatStr(ResultStr,"OKAY: %s '%s' complete",Ctx->ActionName, FI->Name); } if (Ctx->CmdFlags & FLAG_CMD_EXTN_TEMP) { ResultStr=PathChangeExtn(ResultStr, FI->Name, GetVar(Ctx->Vars,"TransferTempExtn")); //Different sense than AlterFileExtn! We rename to FI->Name, not from it FileStoreRename(Ctx->DestFS,ResultStr,FI->Name); if (Ctx->CmdFlags & FLAG_CMD_VERBOSE) HandleEventMessage(Settings.Flags,"Renaming: %s -> %s",ResultStr,FI->Name); } if (Ctx->CmdFlags & FLAG_CMD_EXTN_DEST) AlterFileExtn(Ctx->DestFS,Ctx->CmdFlags,FI->Name,GetVar(Ctx->Vars,"TransferDestExtn")); //Use FI->Path here, because local files can be referred to by full paths if (Ctx->CmdFlags & FLAG_CMD_EXTN_SOURCE) AlterFileExtn(Ctx->SrcFS,Ctx->CmdFlags,FI->Path,GetVar(Ctx->Vars,"TransferSourceExtn")); /* if (Ctx->CmdFlags & FLAG_CMD_CHMOD) { if (FI->Type==FTYPE_DIR) FileStoreChMod(Ctx->DestFS, FI, GetVar(Ctx->Vars,"DChMod")); else FileStoreChMod(Ctx->DestFS, FI, GetVar(Ctx->Vars,"FChMod")); } */ break; case ERR_FORBID: ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' FORBIDDEN. Insufficient Permissions.",Ctx->ActionName,FI->Name); break; case ERR_LOCKED: ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' LOCKED. File locked.",Ctx->ActionName, FI->Name); break; case ERR_EXISTS: ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' CONFLICT. Cannot overwrite.",Ctx->ActionName,FI->Name); break; case ERR_TOOBIG: ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' TOO BIG. File too big.",Ctx->ActionName,FI->Name); break; case ERR_FULL: ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' FULL. Insufficient storage.",Ctx->ActionName,FI->Name); break; case ERR_RATELIMIT: ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' REFUSED. Rate Limiting in effect. Try later.",Ctx->ActionName,FI->Name); break; case ERR_BADNAME: ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' Bad filename.",Ctx->ActionName,FI->Name); break; case ERR_SOURCEFILE: ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' Cannot open source file for upload",Ctx->ActionName,FI->Name); break; case ERR_DESTFILE: ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' Cannot open destination file for writing",Ctx->ActionName,FI->Name); break; case ERR_INTERRUPTED: ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' Transfer interrupted.",Ctx->ActionName,FI->Name); break; case ERR_READ_NOTSUPPORTED: ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' Filestore does not support read/get.",Ctx->ActionName,FI->Name); break; case ERR_WRITE_NOTSUPPORTED: ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' Filestore does not support write/put.",Ctx->ActionName,FI->Name); break; case ERR_CUSTOM: ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' %s.",Ctx->ActionName,FI->Name,GetVar(Ctx->Vars,"Error")); break; default: ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' Unknown error.",Ctx->ActionName,FI->Name); break; } if (StrLen(ResultStr)) HandleEventMessage(Settings.Flags,ResultStr); if (result==TRANSFER_OKAY) { if (StrLen(Ctx->PostCopyHook)) { Tempstr=FormatStr(Tempstr,"%s '%s' '%s' '%s'",Ctx->PostCopyHook,FI->Name,FI->Path,ResultStr); system(Tempstr); } } DestroyString(ResultStr); DestroyString(Tempstr); }
int ConnectHopSSH(STREAM *S, int Type, const char *ProxyURL, const char *Destination) { char *Tempstr=NULL, *Token=NULL, *Token2=NULL; char *Host=NULL, *User=NULL, *Pass=NULL; STREAM *tmpS; int result=FALSE, val, i; unsigned int Port=0, DPort=0; ParseConnectDetails(ProxyURL, NULL, &Host, &Token, &User, &Pass, NULL); if (StrValid(Token)) Port=atoi(Token); if (Type==CONNECT_HOP_SSHTUNNEL) { //get hostname and port of next hop DPort=(rand() % (0xFFFF - 9000)) +9000; //Host will be Token, and port Token2 ParseConnectDetails(Destination, NULL, &Token, &Token2, NULL, NULL, NULL); Tempstr=FormatStr(Tempstr,"tunnel:%d:%s:%s ",DPort,Token,Token2); tmpS=SSHConnect(Host, Port, User, Pass, Tempstr); if (tmpS) { if (! S->Items) S->Items=ListCreate(); ListAddNamedItem(S->Items, "LU:AssociatedStream", tmpS); for (i=0; i < 60; i++) { S->in_fd=TCPConnect("127.0.0.1",DPort,0); if (S->in_fd > -1) { S->out_fd=S->in_fd; result=TRUE; break; } usleep(200000); } } } else { ParseConnectDetails(Destination, NULL, &Token, &Token2, NULL, NULL, NULL); DPort=atoi(Token2); Tempstr=FormatStr(Tempstr,"stdin:%s:%d", Token, DPort); tmpS=SSHConnect(Host, Port, User, Pass, Tempstr); if (tmpS) { usleep(200000); result=TRUE; S->in_fd=tmpS->in_fd; S->out_fd=tmpS->out_fd; } if (tmpS) { //set these to -1 so STREAMClose won't close our connection tmpS->in_fd=-1; tmpS->out_fd=-1; STREAMClose(tmpS); } } if (! result) RaiseError(0, "ConnectHopSSH", "failed to sshtunnel via %s to %s", ProxyURL, Destination); DestroyString(Tempstr); DestroyString(Token2); DestroyString(Token); DestroyString(Host); DestroyString(User); DestroyString(Pass); return(result); }
int InternalCopyFile(TTransferContext *Ctx, TFileInfo *iSrcFI) { STREAM *Src=NULL, *Dest=NULL; TFileInfo *SrcFI=NULL, *DestFI=NULL; char *Buffer=NULL, *Tempstr=NULL, *ptr; int BuffSize=4096; int result, bytes=0, towrite=0; unsigned int TotalBytes=0, BytesSent=0, percent=0, secs=0, val; double bigval; //TFileInfo *FI; int RetVal=TRANSFER_OKAY; struct timeval StartTime, Now; TProcessingModule *CompressionMod=NULL; if (! Ctx->SrcFS->ReadBytes) return(ERR_READ_NOTSUPPORTED); if (Ctx->DestFS && (! Ctx->DestFS->WriteBytes)) return(ERR_WRITE_NOTSUPPORTED); gettimeofday(&StartTime,NULL); if (Ctx->DestFS && Ctx->DestFS->BuffSize) BuffSize=Ctx->DestFS->BuffSize; Buffer=SetStrLen(Buffer,BuffSize*2+1); SrcFI=FileInfoClone(iSrcFI); DestFI=FileInfoClone(iSrcFI); DestFI->Path=FileStoreFormatPath(DestFI->Path, Ctx->DestFS, iSrcFI->Name); //if a PreCopyHook script is specified, then run it if (StrLen(Ctx->PreCopyHook)) { Tempstr=FormatStr(Tempstr,"%s '%s' '%s' '%s'",Ctx->PreCopyHook,DestFI->Name,DestFI->Path,""); system(Tempstr); } Src=InternalCopyOpenSource(Ctx, SrcFI); TotalBytes=SrcFI->Size; if (Ctx->CmdFlags & FLAG_CMD_RESUME) BytesSent=SrcFI->ResumePoint; if (Src) { if (Ctx->SrcFS->Settings & FS_COMPRESSION) CompressionMod=ActivateCompression(Ctx->DestFS,Ctx->SrcFS); if (Ctx->DestFS) Dest=InternalCopyOpenDest(Ctx, SrcFI, DestFI); else Dest=STREAMFromFD(1); if (Dest) { result=Ctx->SrcFS->ReadBytes(Ctx->SrcFS,Src,Buffer,BuffSize*2); while ((bytes > 0) || (result != EOF)) { if (CompressionMod) result=HandleCompression(CompressionMod,Buffer,result); if (result > 0) bytes+=result; if (bytes > BuffSize) towrite=BuffSize; else towrite=bytes; if (Ctx->DestFS) result=Ctx->DestFS->WriteBytes(Ctx->DestFS,Dest,Buffer,towrite); else result=STREAMWriteBytes(Dest,Buffer,towrite); if (result==-1) { RetVal=ERR_INTERRUPTED; break; } if (Settings.Flags & FLAG_INTERRUPT) { Settings.Flags &= ~FLAG_INTERRUPT; RetVal=ERR_CANCEL; break; } BytesSent+=result; bytes-=result; if (bytes > 0) memmove(Buffer,Buffer+result,bytes); gettimeofday(&Now,NULL); val=Now.tv_sec - StartTime.tv_sec; if (val > secs) { secs=val; if (secs > 0) DisplayTransferStatus(SrcFI->Name,BytesSent,TotalBytes,&percent,secs,Ctx->CmdFlags,Ctx->Throttle); } result=Ctx->SrcFS->ReadBytes(Ctx->SrcFS,Src,Buffer+bytes,BuffSize*2 -bytes); } if (Ctx->SrcFS->CloseFile) Ctx->SrcFS->CloseFile(Ctx->SrcFS,Src); if (Ctx->DestFS && Ctx->DestFS->CloseFile) { result=Ctx->DestFS->CloseFile(Ctx->DestFS,Dest); if (result !=TRUE) { //Handle Errors returned by Close file if (RetVal >= FALSE) RetVal=result; SetVar(Ctx->Vars,"Error",GetVar(Ctx->DestFS->Vars,"Error")); SetVar(Ctx->DestFS->Vars,"Error",""); } else ListAddNamedItem(Ctx->DestFS->DirListing,DestFI->Name,FileInfoClone(DestFI)); } else STREAMFlush(Dest); if ((TotalBytes > 0) && (BytesSent < TotalBytes)) RetVal=ERR_INTERRUPTED; } else RetVal=ERR_DESTFILE; secs=Now.tv_sec - StartTime.tv_sec; if (secs < 1) secs=1; DisplayTransferStatus(SrcFI->Name,BytesSent,TotalBytes,&percent,secs,0,0); //if (! (CmdFlags & FLAG_QUIET)) printf("\n"); } else RetVal=ERR_SOURCEFILE; Ctx->TotalBytes+=BytesSent; FileInfoDestroy(DestFI); FileInfoDestroy(SrcFI); DestroyString(Buffer); DestroyString(Tempstr); return(RetVal); }
ListNode *ListAddItem(ListNode *ListStart,void *Item) { return(ListAddNamedItem(ListStart,NULL,Item)); }
void InitTTY(int tty, int LineSpeed, int Flags) { struct termios tty_data, *old_tty_data; int result, val; char *Tempstr=NULL; ListNode *Curr; Tempstr=FormatStr(Tempstr,"%d",tty); if (! TTYAttribs) TTYAttribs=ListCreate(); Curr=ListFindNamedItem(TTYAttribs,Tempstr); if (! Curr) { old_tty_data=(struct termios *) calloc(1,sizeof(struct termios)); ListAddNamedItem(TTYAttribs,Tempstr,old_tty_data); } else old_tty_data=(struct termios *) Curr->Item; tcgetattr(tty,old_tty_data); //tcgetattr(tty,&tty_data); memset(&tty_data,0,sizeof(tty_data)); //ignore break characters and parity errors tty_data.c_iflag=IGNBRK | IGNPAR; //Enable Special Characters if (Flags & TTYFLAG_CANON) tty_data.c_iflag|= ICANON; //else tty_data.c_iflag &= ~ICANON; /* if (! (Flags & TTYFLAG_CRLF_KEEP)) { //translate carriage-return to newline if (Flags & TTYFLAG_CRLF) tty_data.c_iflag |= ICRNL; else tty_data.c_iflag &= ~ICRNL; //translate newline to carriage return if (Flags & TTYFLAG_LFCR) { tty_data.c_iflag |= INLCR; } else tty_data.c_iflag &= ~INLCR; //output flags tty_data.c_oflag=0; //postprocess and translate newline to cr-nl if (Flags & TTYFLAG_LFCR) tty_data.c_oflag |= ONLCR | OPOST; } */ tty_data.c_cflag=CREAD | CS8 | HUPCL | CLOCAL; if (Flags & TTYFLAG_SOFTWARE_FLOW) { tty_data.c_iflag |= IXON | IXOFF; } if (Flags & TTYFLAG_HARDWARE_FLOW) tty_data.c_cflag |=CRTSCTS; if (Flags & TTYFLAG_ECHO) tty_data.c_cflag |= ECHO; tty_data.c_lflag=0; if (Flags & TTYFLAG_ISIG) tty_data.c_lflag=ISIG; tty_data.c_cc[VMIN]=1; tty_data.c_cc[VTIME]=0; if (LineSpeed > 0) { switch (LineSpeed) { case 2400: val=B2400; break; case 4800: val=B4800; break; case 9600: val=B9600; break; case 19200: val=B19200; break; case 38400: val=B38400; break; case 57600: val=B57600; break; case 115200: val=B115200; break; case 230400: val=B230400; break; #ifdef B460800 case 460800: val=B460800; break; #endif #ifdef B500000 case 500000: val=B500000; break; #endif #ifdef B1000000 case 10000000: val=B1000000; break; #endif #ifdef B1152000 case 1152000: val=B1152000; break; #endif #ifdef B2000000 case 2000000: val=B2000000; break; #endif #ifdef B4000000 case 4000000: val=B4000000; break; #endif default: val=B115200; break; } cfsetispeed(&tty_data,val); cfsetospeed(&tty_data,val); } tcflush(tty,TCIFLUSH); result=tcsetattr(tty,TCSANOW,&tty_data); DestroyString(Tempstr); }
void ParseConfigItem(char *ConfigLine) { const char *ConfTokens[]={"Chroot","Chhome","AllowUsers","DenyUsers","Port","LogFile","AuthPath","BindAddress","LogPasswords","HttpMethods","AuthMethods","DefaultUser","DefaultGroup","Path","FileType","LogVerbose","AuthRealm","Compression","DirListType","DisplayNameLen","MaxLogSize","ScriptHandler","ScriptHashFile","WebsocketHandler","LookupClientName","SanitizeAllowTags","CustomHeader","UserAgentSettings", "SSLKey","SSLCert","SSLCiphers","SSLDHParams","SSLClientCertificate","SSLVerifyPath", "SSLVersion", "Event","FileCacheTime","HttpKeepAlive","AccessTokenKey","Timezone","MaxMemory","MaxStack","ActivityTimeout","PackFormats",NULL}; typedef enum {CT_CHROOT, CT_CHHOME, CT_ALLOWUSERS,CT_DENYUSERS,CT_PORT, CT_LOGFILE,CT_AUTHFILE,CT_BINDADDRESS,CT_LOGPASSWORDS,CT_HTTPMETHODS, CT_AUTHMETHODS,CT_DEFAULTUSER, CT_DEFAULTGROUP, CT_PATH, CT_FILETYPE, CT_LOG_VERBOSE, CT_AUTH_REALM, CT_COMPRESSION, CT_DIRTYPE, CT_DISPLAYNAMELEN, CT_MAXLOGSIZE, CT_SCRIPTHANDLER, CT_SCRIPTHASHFILE, CT_WEBSOCKETHANDLER, CT_LOOKUPCLIENT, CT_SANITIZEALLOW, CT_CUSTOMHEADER, CT_USERAGENTSETTINGS, CT_SSLKEY, CT_SSLCERT, CT_SSLCIPHERS, CT_SSLDHPARAMS, CT_CLIENT_CERTIFICATION, CT_SSLVERIFY_PATH, CT_SSL_VERSION, CT_EVENT, CT_FILE_CACHE_TIME, CT_SESSION_KEEPALIVE, CT_ACCESS_TOKEN_KEY, CT_TIMEZONE, CT_MAX_MEM, CT_MAX_STACK, CT_ACTIVITY_TIMEOUT, CT_ARCHIVE_FORMATS} TConfigTokens; char *Token=NULL, *ptr; struct group *grent; struct stat Stat; TConfigTokens TokType; ptr=GetToken(ConfigLine,"=|:",&Token,GETTOKEN_MULTI_SEPARATORS); StripLeadingWhitespace(Token); StripTrailingWhitespace(Token); TokType=MatchTokenFromList(Token,ConfTokens,0); switch(TokType) { case CT_PORT: Settings.Port=atoi(ptr); break; case CT_CHROOT: Settings.Flags &= ~FLAG_CHHOME; Settings.Flags |= FLAG_CHROOT; Settings.DefaultDir=CopyStr(Settings.DefaultDir,ptr); break; case CT_CHHOME: Settings.Flags &= ~FLAG_CHROOT; Settings.Flags|=FLAG_CHHOME; break; case CT_ALLOWUSERS: Settings.AllowUsers=CopyStr(Settings.AllowUsers,ptr); break; case CT_DENYUSERS: Settings.DenyUsers=CopyStr(Settings.DenyUsers,ptr); break; case CT_AUTHFILE: Settings.AuthPath=CopyStr(Settings.AuthPath,ptr); break; case CT_BINDADDRESS: Settings.BindAddress=CopyStr(Settings.BindAddress,ptr); break; case CT_LOGPASSWORDS: // Settings.Flags |= FLAG_LOGPASSWORDS; break; case CT_DISPLAYNAMELEN: Settings.DisplayNameLen=atoi(ptr); break; case CT_AUTHMETHODS: Settings.AuthMethods=CopyStr(Settings.AuthMethods,ptr); break; case CT_HTTPMETHODS: Settings.HttpMethods=CopyStr(Settings.HttpMethods,ptr); break; case CT_DEFAULTUSER: Settings.DefaultUser=CopyStr(Settings.DefaultUser,ptr); break; case CT_DEFAULTGROUP: Settings.DefaultGroup=CopyStr(Settings.DefaultGroup,ptr); grent=getgrnam(ptr); if (grent) Settings.DefaultGroupID=grent->gr_gid; break; case CT_SSLKEY: if (! Settings.SSLKeys) Settings.SSLKeys=ListCreate(); Token=FormatStr(Token,"SSL_KEY_FILE:%d",ListSize(Settings.SSLKeys)); ListAddNamedItem(Settings.SSLKeys,Token,CopyStr(NULL,ptr)); Settings.Flags |=FLAG_SSL; break; case CT_SSLCERT: if (! Settings.SSLKeys) Settings.SSLKeys=ListCreate(); Token=FormatStr(Token,"SSL_CERT_FILE:%d",ListSize(Settings.SSLKeys)); ListAddNamedItem(Settings.SSLKeys,Token,CopyStr(NULL,ptr)); Settings.Flags |=FLAG_SSL; break; case CT_SSLCIPHERS: LibUsefulSetValue("SSL-Permitted-Ciphers",ptr); break; case CT_SSLDHPARAMS: LibUsefulSetValue("SSL-DHParams-File",ptr); break; case CT_SSL_VERSION: LibUsefulSetValue("SSL-Level",ptr); break; case CT_AUTH_REALM: Settings.AuthRealm=CopyStr(Settings.AuthRealm,ptr); break; case CT_COMPRESSION: if (strcasecmp(ptr,"partial")==0) { Settings.Flags &= ~FLAG_COMPRESS; Settings.Flags |= FLAG_PARTIAL_COMPRESS; } else if (! YesNoTrueFalse(ptr)) Settings.Flags &= ~(FLAG_COMPRESS | FLAG_PARTIAL_COMPRESS); else { Settings.Flags &= ~FLAG_PARTIAL_COMPRESS; Settings.Flags |= FLAG_COMPRESS; } break; case CT_PATH: ptr=GetToken(ptr,",",&Token,0); VPathParse(Settings.VPaths, Token, ptr); break; case CT_FILETYPE: VPathParse(Settings.VPaths, Token, ptr); break; case CT_DIRTYPE: ParseDirListType(ptr); break; case CT_LOGFILE: Settings.LogPath=CopyStr(Settings.LogPath,ptr); break; case CT_LOG_VERBOSE: if (YesNoTrueFalse(ptr)) Settings.Flags |= FLAG_LOG_VERBOSE; else Settings.Flags &= ~FLAG_LOG_VERBOSE; break; case CT_MAXLOGSIZE: Settings.MaxLogSize = (int) ParseHumanReadableDataQty(ptr, 0); break; case CT_SCRIPTHANDLER: ptr=GetToken(ptr,"=",&Token,0); if (! Settings.ScriptHandlers) Settings.ScriptHandlers=ListCreate(); SetTypedVar(Settings.ScriptHandlers,Token,ptr,PATHTYPE_CGI); break; case CT_SCRIPTHASHFILE: Settings.ScriptHashFile=CopyStr(Settings.ScriptHashFile,ptr); Settings.Flags |= FLAG_CHECK_SCRIPTS; break; case CT_WEBSOCKETHANDLER: if (! Settings.ScriptHandlers) Settings.ScriptHandlers=ListCreate(); ptr=GetToken(ptr,"=",&Token,0); SetTypedVar(Settings.ScriptHandlers,Token,ptr,PATHTYPE_WEBSOCKET); break; case CT_SANITIZEALLOW: if (! Settings.SanitizeArgumentsAllowedTags) Settings.SanitizeArgumentsAllowedTags=ListCreate(); ptr=GetToken(ptr,",",&Token,0); while (ptr) { SetVar(Settings.SanitizeArgumentsAllowedTags,Token,"Y"); ptr=GetToken(ptr,",",&Token,0); } break; case CT_CUSTOMHEADER: if (! Settings.CustomHeaders) Settings.CustomHeaders=ListCreate(); ptr=GetToken(ptr,":",&Token,0); ListAddNamedItem(Settings.CustomHeaders,Token,CopyStr(NULL,ptr)); break; case CT_LOOKUPCLIENT: if (YesNoTrueFalse(ptr)) Settings.Flags |= FLAG_LOOKUP_CLIENT; else Settings.Flags &= ~FLAG_LOOKUP_CLIENT; break; case CT_USERAGENTSETTINGS: if (! Settings.UserAgents) Settings.UserAgents=ListCreate(); ptr=GetToken(ptr,",",&Token,0); ListAddNamedItem(Settings.UserAgents,Token,CopyStr(NULL,ptr)); break; case CT_SSLVERIFY_PATH: if (stat(ptr,&Stat)==0) { if (S_ISDIR(Stat.st_mode)) LibUsefulSetValue("SSL_VERIFY_CERTDIR",ptr); else if (S_ISREG(Stat.st_mode)) LibUsefulSetValue("SSL_VERIFY_CERTFILE",ptr); } else HandleError(ERR_PRINT|ERR_LOG|ERR_EXIT, "ERROR: Can't access SSL certificate verify data at '%s'",ptr); break; case CT_CLIENT_CERTIFICATION: if (strcasecmp(ptr,"ask")==0) Settings.AuthFlags |= FLAG_AUTH_CERT_ASK; if (strcasecmp(ptr,"required")==0) Settings.AuthFlags |= FLAG_AUTH_CERT_REQUIRED; if (strcasecmp(ptr,"sufficient")==0) Settings.AuthFlags |= FLAG_AUTH_CERT_SUFFICIENT; if (strcasecmp(ptr,"optional")==0) Settings.AuthFlags |= FLAG_AUTH_CERT_SUFFICIENT; if (strcasecmp(ptr,"required+sufficient")==0) Settings.AuthFlags |= FLAG_AUTH_CERT_REQUIRED | FLAG_AUTH_CERT_SUFFICIENT; break; case CT_EVENT: ParseEventConfig(ptr); break; case CT_FILE_CACHE_TIME: Settings.DocumentCacheTime=strtol(ptr,NULL,10); break; case CT_SESSION_KEEPALIVE: if (YesNoTrueFalse(ptr)) Settings.Flags |= FLAG_KEEPALIVES; else Settings.Flags &= ~FLAG_KEEPALIVES; break; case CT_ACCESS_TOKEN_KEY: Settings.AccessTokenKey=CopyStr(Settings.AccessTokenKey,ptr); break; case CT_TIMEZONE: Settings.Timezone=CopyStr(Settings.Timezone,ptr); break; case CT_MAX_MEM: Settings.AddressSpace=CopyStr(Settings.AddressSpace,ptr); break; case CT_MAX_STACK: Settings.StackSize=CopyStr(Settings.StackSize,ptr); break; case CT_ACTIVITY_TIMEOUT: Settings.ActivityTimeout=atoi(ptr); break; case CT_ARCHIVE_FORMATS: Settings.PackFormats=ParsePackFormats(Settings.PackFormats, ptr); break; } DestroyString(Token); }
int HTTPServerReadHeaders(HTTPSession *Session) { char *Tempstr=NULL, *Token=NULL, *ptr; ListNode *Curr; int val; HTTPSessionClear(Session); Tempstr=STREAMReadLine(Tempstr,Session->S); if (! Tempstr) return(FALSE); StripTrailingWhitespace(Tempstr); //First line of the HTTP request is the 'Command' in the form "<method> <url>?<arguments> <HTTP version>" HTTPServerParseCommand(Session, Session->S, Tempstr); Tempstr=STREAMReadLine(Tempstr,Session->S); if (Tempstr) { StripTrailingWhitespace(Tempstr); StripLeadingWhitespace(Tempstr); } while (StrLen(Tempstr) ) { if (Settings.Flags & FLAG_LOG_VERBOSE) LogToFile(Settings.LogPath,"<< %s",Tempstr); ptr=GetToken(Tempstr,":",&Token,0); while (isspace(*ptr)) ptr++; val=MatchTokenFromList(Token,HeaderStrings,0); ListAddNamedItem(Session->Headers,Token,CopyStr(NULL,ptr)); switch (val) { case HEAD_PROXYAUTH: if (IsProxyMethod(Session->MethodID)) { ptr=GetToken(ptr,"\\S",&Token,0); HTTPServerHandleAuthHeader(Session,val,Token,ptr); Session->AuthFlags |= FLAG_AUTH_PRESENT; } break; case HEAD_AUTH: if (IsProxyMethod(Session->MethodID)) { Session->RemoteAuthenticate=CopyStr(Session->RemoteAuthenticate,ptr); } if (StrLen(Session->UserName)==0) { ptr=GetToken(ptr,"\\S",&Token,0); HTTPServerHandleAuthHeader(Session,val,Token,ptr); Session->AuthFlags |= FLAG_AUTH_PRESENT; } break; case HEAD_HOST: Session->Host=CopyStr(Session->Host,ptr); ptr=strchr(Session->Host,':'); if (! ptr) { Token=FormatStr(Token,":%d",Settings.Port); Session->Host=CatStr(Session->Host,Token); } break; case HEAD_DEST: Session->Destination=HTTPUnQuote(Session->Destination,ptr); break; case HEAD_CONTENT_TYPE: HTTPServerParsePostContentType(Session, ptr); break; case HEAD_CONTENT_LENGTH: Session->ContentSize=atoi(ptr); break; case HEAD_DEPTH: if (strcasecmp(ptr,"infinity")==0) Session->Depth=INT_MAX; else Session->Depth=atoi(ptr); break; case HEAD_OVERWRITE: if (*ptr=='T') Session->Flags |= SESSION_OVERWRITE; break; case HEAD_CONNECTION: if ((Settings.Flags & FLAG_KEEPALIVES) && (strcasecmp(ptr,"Keep-Alive")==0)) Session->Flags |= SESSION_KEEPALIVE; break; case HEAD_AGENT: Session->UserAgent=CopyStr(Session->UserAgent,ptr); Curr=ListGetNext(Settings.UserAgents); while (Curr) { if (fnmatch(Curr->Tag,Session->UserAgent,0)==0) { if (Settings.Flags & FLAG_LOG_VERBOSE) LogToFile(Settings.LogPath,"Applying User Agent Settings: %s",Curr->Item); ParseConfigItemList((char *) Curr->Item); } Curr=ListGetNext(Curr); } break; case HEAD_COOKIE: if (StrLen(Session->Cookies)) Session->Cookies=MCopyStr(Session->Cookies,"; ",ptr,NULL); else Session->Cookies=CopyStr(Session->Cookies,ptr); Session->AuthFlags |= FLAG_AUTH_PRESENT; break; case HEAD_REFERER: Session->ClientReferrer=CopyStr(Session->ClientReferrer,ptr); break; case HEAD_ACCEPT_ENCODING: ptr=GetToken(ptr,",",&Token,0); while (ptr) { if (strcmp(Token,"gzip")==0) Session->Flags|=SESSION_ENCODE_GZIP; if (strcmp(Token,"x-gzip")==0) Session->Flags|=SESSION_ENCODE_GZIP | SESSION_ENCODE_XGZIP; ptr=GetToken(ptr,",",&Token,0); } break; case HEAD_ICECAST: if (atoi(ptr)) Session->Flags |= SESSION_ICECAST; break; case HEAD_IFMOD_SINCE: Session->IfModifiedSince=DateStrToSecs("%a, %d %b %Y %H:%M:%S %Z",ptr,NULL); break; case HEAD_UPGRADE: if ((strcasecmp(ptr,"Upgrade")==0) && SSLAvailable()) { if (! HTTPServerActivateSSL(Session,Settings.SSLKeys)) return; } else if (strcasecmp(ptr,"websocket")==0) Session->MethodID = METHOD_WEBSOCKET; break; case HEAD_WEBSOCK_KEY: Session->ContentBoundary=CopyStr(Session->ContentBoundary, ptr); break; case HEAD_WEBSOCK_KEY1: Session->ContentBoundary=CopyStr(Session->ContentBoundary, ptr); if (Session->MethodID==METHOD_WEBSOCKET) Session->MethodID = METHOD_WEBSOCKET75; break; case HEAD_WEBSOCK_KEY2: Session->ContentType=CopyStr(Session->ContentType, ptr); if (Session->MethodID==METHOD_WEBSOCKET) Session->MethodID = METHOD_WEBSOCKET75; break; case HEAD_WEBSOCK_PROTOCOL: Session->ContentType=CopyStr(Session->ContentType, ptr); break; case HEAD_WEBSOCK_VERSION: break; case HEAD_ORIGIN: break; } Tempstr=STREAMReadLine(Tempstr,Session->S); StripTrailingWhitespace(Tempstr); StripLeadingWhitespace(Tempstr); } if (strstr(Session->Arguments,"AccessToken")) Session->AuthFlags |= FLAG_AUTH_PRESENT | FLAG_AUTH_ACCESS_TOKEN; Session->URL=HTTPUnQuote(Session->URL,Session->OriginalURL); if (*Session->URL=='/') Session->Path=CopyStr(Session->Path,Session->URL); else Session->Path=MCopyStr(Session->Path,"/",Session->URL,NULL); DestroyString(Tempstr); DestroyString(Token); return(TRUE); }
static const char *ParserRSSItems(int ParserType, const char *Doc, ListNode *Parent, int IndentLevel) { const char *ptr; char *Token=NULL, *PrevToken=NULL, *Name=NULL; ListNode *Node; int BreakOut=FALSE, InTag=FALSE; ptr=Doc; while (ptr && (! BreakOut)) { ptr=GetToken(ptr, RSS_TOKENS, &Token,GETTOKEN_MULTI_SEP|GETTOKEN_INCLUDE_SEP); switch (*Token) { case '>': InTag=FALSE; while (isspace(*ptr)) ptr++; break; case '<': InTag=TRUE; while (isspace(*ptr)) ptr++; ptr=GetToken(ptr, RSS_TOKENS, &Token,GETTOKEN_MULTI_SEP|GETTOKEN_INCLUDE_SEP); switch (*Token) { case '/': if (strcasecmp(Token,"/item")==0) BreakOut=TRUE; else if (strcasecmp(Token,"/image")==0) BreakOut=TRUE; else if (strcasecmp(Token,"/channel")==0) /*ignore */ ; else if (strcasecmp(Token,"/rss")==0) /*ignore */ ; //if this is a 'close' for a previous 'open' then add all the data we collected else if (strcasecmp(Token+1, Name)==0) { Node=ListAddNamedItem(Parent, Name, CopyStr(NULL, PrevToken)); Node->ItemType=ITEM_VALUE; PrevToken=CopyStr(PrevToken,""); } break; case 'i': case 'I': if (strcasecmp(Token,"item")==0) { ptr=ParserAddNewStructure(ParserType, ptr, Parent, ITEM_ENTITY, NULL, IndentLevel+1); } else if (strcasecmp(Token,"image")==0) { ptr=ParserAddNewStructure(ParserType, ptr, Parent, ITEM_ENTITY, Token, IndentLevel+1); } else Name=CopyStr(Name, Token); break; case '!': if (strncmp(ptr,"[CDATA[",7)==0) { ptr=GetToken(ptr+7, "]]", &Token,0); PrevToken=CatStr(PrevToken, Token); } break; default: PrevToken=CopyStr(PrevToken,""); if (strcasecmp(Token,"channel")==0) /*ignore */ ; else if (strcasecmp(Token,"rss")==0) /*ignore */ ; /*someone's always got to be different. 'enclosure' breaks the structure of RSS */ else if (strcasecmp(Token,"enclosure")==0) ptr=ParserRSSEnclosure(Parent, ptr); else Name=CopyStr(Name, Token); break; } break; default: if (! InTag) PrevToken=CatStr(PrevToken, Token); break; } } DestroyString(PrevToken); DestroyString(Token); DestroyString(Name); return(ptr); }
static const char *ParserJSONItems(int ParserType, const char *Doc, ListNode *Parent, int IndentLevel) { const char *ptr; char *Token=NULL, *PrevToken=NULL, *Name=NULL; ListNode *Node; int BreakOut=FALSE; ptr=GetToken(Doc, JSON_TOKENS, &Token,GETTOKEN_MULTI_SEP|GETTOKEN_INCLUDE_SEP|GETTOKEN_HONOR_QUOTES); while (ptr) { switch (*Token) { case '[': ptr=ParserAddNewStructure(ParserType, ptr, Parent, ITEM_ARRAY, Name, IndentLevel+1); Name=CopyStr(Name,""); break; case ']': if (ptr && (*ptr==',')) ptr++; BreakOut=TRUE; break; case '{': ptr=ParserAddNewStructure(ParserType, ptr, Parent, ITEM_ENTITY, Name, IndentLevel+1); Name=CopyStr(Name,""); break; case '}': //we can have an item right before a '}' that doesn't terminate with a ',' becasue the '}' terminates it if (StrValid(Name) && StrValid(PrevToken)) { Node=ListAddNamedItem(Parent, Name, CopyStr(NULL, PrevToken)); Node->ItemType=ITEM_VALUE; } if (ptr && (*ptr==',')) ptr++; BreakOut=TRUE; break; case ':': Name=CopyStr(Name, PrevToken); break; case '\r': //ignore and do nothing break; case '\n': case ',': Node=ListAddNamedItem(Parent, Name, CopyStr(NULL, PrevToken)); Node->ItemType=ITEM_VALUE; break; default: StripTrailingWhitespace(Token); StripQuotes(Token); PrevToken=CopyStr(PrevToken, Token); break; } if (BreakOut) break; while (isspace(*ptr)) ptr++; ptr=GetToken(ptr, JSON_TOKENS, &Token,GETTOKEN_MULTI_SEP|GETTOKEN_INCLUDE_SEP|GETTOKEN_HONOR_QUOTES); } DestroyString(PrevToken); DestroyString(Token); DestroyString(Name); return(ptr); }
void ParseCommandLine(int argc, char *argv[], ListNode *DL_List, int *OverrideType) { int i,j, DebugLevel=0; char *ptr; ProgName=CopyStr(ProgName,argv[0]); CmdLine=argv[0]; for (i=1; i < argc; i++) { if (strcmp(argv[i],"-p")==0) Proxy=CopyStr(Proxy,argv[++i]); else if (strcmp(argv[i],"-proxy")==0) Proxy=CopyStr(Proxy,argv[++i]); else if (strcmp(argv[i],"-a")==0) { ptr=GetToken(argv[++i],":",&Username,0); ptr=GetToken(ptr,":",&Password,0); } else if (strcmp(argv[i],"-v")==0) { DebugLevel++; } else if (strcmp(argv[i],"-o")==0) { i++; AddOutputFile(argv[i], TRUE); if (strcmp(argv[i],"-")==0) Flags |= FLAG_STDOUT; } else if (strcmp(argv[i],"+o")==0) { i++; AddOutputFile(argv[i], FALSE); if (strcmp(argv[i],"-")==0) Flags |= FLAG_STDOUT; } else if (strcmp(argv[i],"-n")==0) { ItemSelectionArg=CopyStr(ItemSelectionArg,argv[++i]); } else if (strcmp(argv[i],"-t")==0) *OverrideType=ParseType(argv[++i]); else if (strcmp(argv[i],"-f")==0) FormatPreference=CopyStr(FormatPreference,argv[++i]); else if (strcmp(argv[i],"-q")==0) Flags |= FLAG_QUIET; else if (strcmp(argv[i],"-b")==0) Flags |= FLAG_BACKGROUND; else if (strcmp(argv[i],"-r")==0) Flags |= FLAG_RESUME; else if (strcmp(argv[i],"-x")==0) Flags |= FLAG_PORN; else if (strcmp(argv[i],"-T")==0) Flags |= FLAG_TEST; else if (strcmp(argv[i],"-w")==0) Flags |= FLAG_STDIN; else if (strcmp(argv[i],"-dt")==0) DisplayTitleWidth=atoi(argv[++i]); else if (strcmp(argv[i],"-st")==0) STREAMTimeout=atoi(argv[++i]); else if (strcmp(argv[i],"-P")==0) Player=CopyStr(Player,argv[++i]); else if (strcmp(argv[i],"-Pp")==0) PlayerLaunchPercent=atoi(argv[++i]); else if (strcmp(argv[i],"-?")==0) Flags |= FLAG_PRINT_USAGE; else if (strcmp(argv[i],"-h")==0) Flags |= FLAG_PRINT_USAGE; else if (strcmp(argv[i],"-help")==0) Flags |= FLAG_PRINT_USAGE; else if (strcmp(argv[i],"--help")==0) Flags |= FLAG_PRINT_USAGE; else if (strcmp(argv[i],"-version")==0) Flags |= FLAG_PRINT_VERSION; else if (strcmp(argv[i],"--version")==0) Flags |= FLAG_PRINT_VERSION; else if (strcmp(argv[i],"-test-sites")==0) { Flags |= FLAG_TEST_SITES | FLAG_QUIET; for (j=1; TestLinks[j] !=NULL; j++) { if (StrLen(TestLinks[j])) ListAddNamedItem(DL_List,DownloadTypes[j],CopyStr(NULL,TestLinks[j])); } ItemSelectionArg=CopyStr(ItemSelectionArg,"0"); } else { ListAddItem(DL_List,CopyStr(NULL,argv[i])); } } if (Flags & FLAG_BACKGROUND) { Flags |= FLAG_QUIET; demonize(); } if (Flags & FLAG_QUIET) DebugLevel=0; if (DebugLevel==1) Flags |= FLAG_DEBUG1; if (DebugLevel==2) Flags |= FLAG_DEBUG2; if (DebugLevel > 2) Flags |= FLAG_DEBUG3; }