int OAuthParseReply(OAUTH *Ctx, const char *ContentType, const char *Reply) { ListNode *P=NULL, *Curr=NULL; const char *ptr; if (! StrValid(ContentType)) return(FALSE); if (! StrValid(Reply)) return(FALSE); P=ParserParseDocument(ContentType, Reply); Curr=ListGetNext(P); while (Curr) { SetVar(Ctx->Vars, Curr->Tag, (char *) Curr->Item); Curr=ListGetNext(Curr); } ptr=ParserGetValue(P, "access_token"); if (StrValid(ptr)) Ctx->AccessToken=CopyStr(Ctx->AccessToken, ptr); ptr=ParserGetValue(P, "refresh_token"); if (StrValid(ptr)) Ctx->RefreshToken=CopyStr(Ctx->RefreshToken, ptr); Ctx->VerifyCode=CopyStr(Ctx->VerifyCode, ParserGetValue(P, "user_code")); Ctx->VerifyURL=CopyStr(Ctx->VerifyURL, ParserGetValue(P, "verification_url")); ParserItemsDestroy(P); return(TRUE); }
TFingerprint *CheckForMatch(HashratCtx *Ctx, const char *Path, struct stat *FStat, const char *HashStr) { TFingerprint *Lookup, *Head=NULL, *Prev=NULL, *Item=NULL, *Result=NULL; void *ptr; if (! StrValid(Path)) return(NULL); Lookup=TFingerprintCreate(HashStr,"","",Path); switch (Ctx->Action) { case ACT_FINDMATCHES_MEMCACHED: Lookup->Data=MemcachedGet(Lookup->Data, Lookup->Hash); if (StrValid(Lookup->Data)) Result=TFingerprintCreate(Lookup->Hash, Lookup->HashType, Lookup->Data, ""); break; case ACT_FINDDUPLICATES: case ACT_FINDMATCHES: ptr=tfind(Lookup, &Tree, MatchCompareFunc); if (ptr) { Item=*(TFingerprint **) ptr; //we have to make a copy because 'Result' is destroyed by parent function Result=TFingerprintCreate(Item->Hash, Item->HashType, Item->Data, Item->Path); } break; default: ptr=tfind(Lookup, &Tree, MatchCompareFunc); if (ptr) { Item=FindPathMatches(Ctx, *(TFingerprint **) ptr, Path); if (Item) { Result=TFingerprintCreate(Item->Hash, Item->HashType, Item->Data, Item->Path); if (Ctx->Action==ACT_CHECK) { if (Item==Head) { if (Item->Next==NULL) { // tree functions take a copy of the 'head' item, so we cannot // destroy it. No idea how they do this, it's magic // however we can destroy non-head items that we hang off // the tree tdelete(Lookup, &Tree, MatchCompareFunc); } else Item->Path=CopyStr(Item->Path, ""); } //else TFingerprintDestroy(Item); } } } break; } TFingerprintDestroy(Lookup); return(Result); }
int OAuthSave(OAUTH *Ctx, const char *Path) { STREAM *S; const char *Fields[]= {"client_id","client_secret","access_token","refresh_token",NULL}; const char *ptr; char *Tempstr=NULL; int i; if (! StrValid(Path)) { if (! StrValid(Ctx->SavePath)) return(FALSE); S=STREAMOpen(Ctx->SavePath,"aEL"); } else S=STREAMOpen(Path,"aEL"); if (S) { Tempstr=MCopyStr(Tempstr, "'", Ctx->Name,"' ",NULL); for (i=0; Fields[i] !=NULL; i++) { ptr=GetVar(Ctx->Vars,Fields[i]); if (StrValid(ptr)) Tempstr=MCatStr(Tempstr, Fields[i], "='", ptr, "' ",NULL); } Tempstr=CatStr(Tempstr,"\n"); STREAMWriteLine(Tempstr, S); STREAMClose(S); } DestroyString(Tempstr); return(TRUE); }
int OAuthLoad(OAUTH *Ctx, const char *ReqName, const char *Path) { STREAM *S; char *Tempstr=NULL, *Token=NULL, *Name=NULL; const char *ptr; int result=FALSE; if (StrValid(ReqName)) Name=CopyStr(Name, ReqName); else Name=CopyStr(Name, Ctx->Name); Ctx->AccessToken=CopyStr(Ctx->AccessToken, ""); Ctx->RefreshToken=CopyStr(Ctx->RefreshToken, ""); if (! StrValid(Path)) S=STREAMOpen(Ctx->SavePath,"rl"); else S=STREAMOpen(Path,"rl"); if (S) { Tempstr=STREAMReadLine(Tempstr, S); while (Tempstr) { ptr=GetToken(Tempstr, "\\S", &Token, GETTOKEN_QUOTES); if (strcmp(Token, Name)==0) { OAuthParse(Ctx, Tempstr); if (StrValid(Ctx->AccessToken) || StrValid(Ctx->RefreshToken)) result=TRUE; } Tempstr=STREAMReadLine(Tempstr, S); } STREAMClose(S); } DestroyString(Tempstr); DestroyString(Token); DestroyString(Name); return(result); }
ListNode *ParserParseDocument(const char *TypeStr, const char *Doc) { ListNode *Node, *Items; const char *Types[]= {"json","rss","yaml","config","ini","url",NULL}; const char *ptr; char *Token=NULL; int Type; if (! StrValid(TypeStr)) return(NULL); if (! StrValid(Doc)) return(NULL); GetToken(TypeStr,";",&Token,0); StripTrailingWhitespace(Token); StripLeadingWhitespace(Token); ptr=Token; if (strncmp(ptr,"application/",12)==0) ptr+=12; if (strncmp(ptr,"x-www-form-urlencoded",21)==0) ptr="url"; Type=MatchTokenFromList(ptr,Types,0); Items=ListCreate(); ptr=Doc; while (isspace(*ptr)) ptr++; if (*ptr=='{') ptr++; if (*ptr=='[') ptr++; ParserParseItems(Type, ptr, Items, 0); fflush(NULL); return(ParserOpenItem(Items,"/")); }
int OAuthRefresh(OAUTH *Ctx, const char *URL) { char *Tempstr=NULL, *Args=NULL; const char *ptr; int result; /* POST, GET client_id (integer) : Your app's client_id (obtained during app registration) client_id and client_secrect can be provided via HTTP Basic Authentication see http://tools.ietf.org/html/rfc6750#section-2.1 POST, GET client_secret (string) : Your app's client_secret (obtained during app registration) client_id and client_secrect can be provided via HTTP Basic Authentication see http://tools.ietf.org/html/rfc6750#section-2.1 POST, GET grant_type (string) : The value must be refresh_token POST, GET refresh_token (string) : required */ ptr=GetVar(Ctx->Vars, "client_id"); if (StrValid(ptr)) { Tempstr=HTTPQuote(Tempstr, ptr); Args=MCopyStr(Args,"client_id=",Tempstr,NULL); } ptr=GetVar(Ctx->Vars, "client_secret"); if (StrValid(ptr)) { Tempstr=HTTPQuote(Tempstr, ptr); Args=MCatStr(Args,"&client_secret=",Tempstr,NULL); } if (StrValid(Ctx->RefreshToken)) { Tempstr=HTTPQuote(Tempstr, Ctx->RefreshToken); Args=MCatStr(Args,"&refresh_token=",Tempstr,NULL); } Args=MCatStr(Args,"&grant_type=refresh_token",NULL); result=OAuthGrant(Ctx, URL, Args); DestroyString(Tempstr); DestroyString(Args); return(result); }
void M3UStreamVarName(const char *Config, char **VarName) { char *Name=NULL, *Value=NULL; char *Resolution=NULL, *Codec=NULL, *Bandwidth=NULL; const char *ptr; ptr=GetNameValuePair(Config, ",", "=", &Name, &Value); while (ptr) { StripTrailingWhitespace(Name); StripTrailingWhitespace(Value); if (strcasecmp(Name,"resolution")==0) Resolution=CopyStr(Resolution, Value); if (strcasecmp(Name,"codec")==0) Codec=CopyStr(Codec, Value); if (strcasecmp(Name,"bandwidth")==0) Bandwidth=CopyStr(Bandwidth, Value); while (isspace(*ptr)) ptr++; ptr=GetNameValuePair(ptr, ",", "=", &Name, &Value); } if (StrValid(Resolution)) *VarName=MCopyStr(*VarName, "item:m3u8-stream:", Resolution, NULL); else *VarName=MCopyStr(*VarName, "item:m3u8-stream:", Bandwidth, NULL); DestroyString(Name); DestroyString(Value); DestroyString(Codec); DestroyString(Resolution); DestroyString(Bandwidth); }
ListNode *ParserFindItem(ListNode *Items, const char *Name) { ListNode *Node, *Curr; char *Token=NULL; const char *ptr; ptr=Name; if (*ptr=='/') { Node=Items; ptr++; if (*ptr=='\0') return(Node); } else if (! Items->Side) Node=Items; else if (Items->Side->ItemType != ITEM_VALUE) Node=(ListNode *) Items->Side; if (Node && StrValid(ptr)) { ptr=GetToken(ptr,"/",&Token,0); while (ptr) { if (Node->ItemType== ITEM_ROOT) Node=(ListNode *) ListFindNamedItem(Node,Token); else if (Node->ItemType != ITEM_VALUE) Node=(ListNode *) ListFindNamedItem((ListNode *) Node->Item,Token); if (! Node) break; ptr=GetToken(ptr,"/",&Token,0); } } else Node=ListGetNext(Items); DestroyString(Token); return(Node); }
int HashFinish(HASH *Hash, int Encoding, char **Return) { char *Token=NULL, *Bytes=NULL, *Hashed=NULL, *ptr; int len; ptr=GetToken(Hash->Type, "\\S", &Token, 0); len=Hash->Finish(Hash, &Bytes); while (StrValid(ptr)) { ptr=GetToken(ptr, "\\S", &Token, 0); len=HashBytes(&Hashed, Token, Bytes, len, 0); Bytes=SetStrLen(Bytes, len); memcpy(Bytes,Hashed,len); } if (Encoding > 0) { *Return=EncodeBytes(*Return, Bytes, len, Encoding); len=StrLen(*Return); } else { *Return=SetStrLen(*Return, len); memcpy(*Return, Bytes, len); } DestroyString(Hashed); DestroyString(Token); DestroyString(Bytes); return(len); }
int SMTPHelo(STREAM *S) { int RetVal=0; char *Tempstr=NULL, *Token=NULL; const char *ptr; ptr=LibUsefulGetValue("SMTP:HELO"); if (! StrValid(ptr)) ptr=STREAMGetValue(S,"SMTP:HELO"); if (! StrValid(ptr)) { Token=GetExternalIP(Token); ptr=Token; } Tempstr=MCopyStr(Tempstr, "EHLO ", ptr, "\r\n", NULL); STREAMWriteLine(Tempstr,S); Tempstr=SMTPRead(Tempstr, S); if (*Tempstr == '2') { RetVal |= CAP_EHLO; ptr=GetToken(Tempstr,"\n",&Token,0); while (ptr) { StripTrailingWhitespace(Token); RetVal |= SMTPParseCapabilities(Token); ptr=GetToken(ptr,"\n",&Token,0); } } //Some old server that doesn't support EHLO, switch to HELO else { Tempstr=MCopyStr(Tempstr, "HELO ", ptr, "\r\n", NULL); STREAMWriteLine(Tempstr,S); if (SMTPInteract(Tempstr, S)) RetVal |= CAP_HELO; } DestroyString(Tempstr); DestroyString(Token); return(RetVal); }
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); }
static int CrayonType(const char *String) { int val; if (! StrValid(String)) return(-1); if (*String=='#') return(-1); val=MatchTokenFromList(String, CrayonTypes, 0); if (val==-1) val=0; return(val); }
const char *OAuthLookup(const char *Name, int Refresh) { ListNode *Curr; OAUTH *OA; Curr=ListFindNamedItem(OAuthKeyChain, Name); if (Curr) { OA=(OAUTH *) Curr->Item; if (Refresh && StrValid(OA->RefreshToken)) { OAuthRefresh(OA, OA->RefreshURL); OAuthSave(OA, ""); } if (StrValid(OA->AccessToken)) return(OA->AccessToken); } return(""); }
char *SlashTerminateDirectoryPath(char *DirPath) { char *ptr, *RetStr=NULL; if (! StrValid(DirPath)) return(CopyStr(DirPath,"/")); RetStr=DirPath; ptr=RetStr+StrLen(RetStr)-1; if (*ptr != '/') RetStr=AddCharToStr(RetStr,'/'); return(RetStr); }
int OAuthFinalize(OAUTH *Ctx, const char *URL) { char *Tempstr=NULL; int result; Tempstr=SubstituteVarsInString(Tempstr, Ctx->Stage2, Ctx->Vars,0); result=OAuthGrant(Ctx, URL, Tempstr); if (StrValid(Ctx->AccessToken)) OAuthSave(Ctx, ""); DestroyString(Tempstr); return(result); }
int SMTPInteract(const char *Line, STREAM *S) { char *Tempstr=NULL; int result=FALSE; if (StrValid(Line)) STREAMWriteLine(Line, S); STREAMFlush(S); Tempstr=SMTPRead(Tempstr, S); /* syslog(LOG_DEBUG,"mail >> %s",Line); syslog(LOG_DEBUG,"mail << %s",Tempstr); */ if ( StrValid(Tempstr) && ( (*Tempstr=='2') || (*Tempstr=='3') ) ) result=TRUE; DestroyString(Tempstr); return(result); }
char *StripDirectorySlash(char *DirPath) { char *ptr; //don't strip '/' (root dir) if (! StrValid(DirPath)) return(DirPath); if (strcmp(DirPath,"/")==0) return(DirPath); ptr=DirPath+StrLen(DirPath)-1; if (*ptr == '/') *ptr='\0'; return(DirPath); }
static void CGIPrintSelect(const char *Name, const char *CurrType, ListNode *Items) { ListNode *Curr; printf("<select name=%s>\r\n", Name); Curr=ListGetNext(Items); while(Curr) { if (StrValid(CurrType) && (strcmp(Curr->Tag,CurrType)==0)) printf("<option selected value=%s> %s\r\n",Curr->Tag, Curr->Item); else printf("<option value=%s> %s\r\n",Curr->Tag, Curr->Item); Curr=ListGetNext(Curr); } printf("</select>\r\n"); }
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); }
ListNode *ListInsertTypedItem(ListNode *InsertNode, uint16_t Type, const char *Name, void *Item) { ListNode *NewNode; if (! InsertNode) return(NULL); NewNode=(ListNode *) calloc(1,sizeof(ListNode)); ListThreadNode(InsertNode, NewNode); NewNode->Item=Item; NewNode->ItemType=Type; if (StrValid(Name)) NewNode->Tag=CopyStr(NewNode->Tag,Name); if (InsertNode->Head->Flags & LIST_FLAG_STATS) { NewNode->Stats=(ListStats *) calloc(1,sizeof(ListStats)); NewNode->Stats->Time=GetTime(TIME_CACHED); } return(NewNode); }
void *MatchesLoad(int Flags) { char *Line=NULL, *Tempstr=NULL, *Type=NULL, *ptr; TFingerprint *FP; STREAM *S; int count=0; S=STREAMFromFD(0); STREAMSetTimeout(S,100); Line=STREAMReadLine(Line,S); if (! StrValid(Line)) return(NULL); if (strncasecmp(Line,"<?xml ",6)==0) { //xml document. Must be an OpenIOC fileq while (Line) { StripTrailingWhitespace(Line); Tempstr=CatStr(Tempstr,Line); Line=STREAMReadLine(Line,S); } count=LoadFromIOC(Tempstr,Flags); } else { while (Line) { StripTrailingWhitespace(Line); FP=TFingerprintParse(Line); if (MatchAdd(FP, "", Flags)) count++; Line=STREAMReadLine(Line, S); } } if (Flags & FLAG_MEMCACHED) printf("Stored %d hashes in memcached server\n", count); DestroyString(Tempstr); DestroyString(Line); DestroyString(Type); return(Tree); }
const char *ParseHostDetails(const char *Data,char **Host,char **Port,char **User, char **Password) { char *Token=NULL, *wptr; const char *ptr, *tptr; if (Port) *Port=CopyStr(*Port, ""); if (Host) *Host=CopyStr(*Host, ""); if (User) *User=CopyStr(*User, ""); if (Password) *Password=CopyStr(*Password, ""); ptr=strrchr(Data,'@'); if (ptr) { //in some cases there will be an '@' in the username, so GETTOKEN_QUOTES //should handle any @ which is prefixed with a \ to quote it out ptr=GetToken(Data,"@",&Token, GETTOKEN_QUOTES); if (User) { tptr=GetToken(Token,":",User,0); if (StrValid(tptr)) *Password=CopyStr(*Password,tptr); } } else ptr=Data; ptr=GetToken(ptr,"/",&Token,0); ptr=ParsePort(Token, Port); if (Host) { if (*Token=='[') { wptr=strrchr(Token,']'); if (wptr) *wptr='\0'; tptr=Token+1; } else tptr=Token; *Host=CopyStr(*Host, tptr); } DestroyString(Token); return(ptr); }
void OutputUnmatchedItem(const void *p_Item, const VISIT which, const int depth) { TFingerprint *Item; if ((which==preorder) || (which==leaf)) { Item=*(TFingerprint **) p_Item; while (Item) { //if a root node of the linked list has been deleted, its path is //set blank, rather than actually deleting it, as we need it to //continue acting as the head node if (StrValid(Item->Path)) { if (access(Item->Path, F_OK) !=0) HandleCheckFail(Item->Path, "Missing"); } Item=Item->Next; } } }
char *ResolveURL(char *RetStr, const char *Parent, const char *SubItem) { char *Proto=NULL, *Host=NULL, *Port=NULL, *Path=NULL; char *BasePath=NULL; ParseURL(Parent,&Proto,&Host,&Port,NULL,NULL,&Path,NULL); if (StrValid(Port)) BasePath=FormatStr(BasePath, "%s://%s:%s/", Proto,Host,Port); else BasePath=FormatStr(BasePath, "%s://%s/", Proto,Host); //if it starts with '/' then we don't append to existing path if (*SubItem=='/') RetStr=MCopyStr(RetStr, BasePath, SubItem, NULL); else RetStr=MCopyStr(RetStr, BasePath, Path, "/", SubItem, NULL); DestroyString(Proto); DestroyString(Host); DestroyString(Port); DestroyString(Path); DestroyString(BasePath); return(RetStr); }
int OAuthImplicit(OAUTH *Ctx, const char *URL, const char *Args) { HTTPInfoStruct *Info; char *Tempstr=NULL; int result=FALSE; STREAM *S; Tempstr=MCopyStr(Tempstr,URL,"?",Args,NULL); Info=HTTPInfoFromURL("GET", Tempstr); //Info->Flags |= HTTP_NOREDIRECT; S=HTTPTransact(Info); if (S) { if (StrValid(Info->RedirectPath)) result=OAuthParseReply(Ctx, "url", Info->RedirectPath); } STREAMClose(S); DestroyString(Tempstr); return(result); }
// Token will either be a test (with an operation like '<' '=' etc) or a list of // actions (colors, uppercase) to apply // this is used elsewhere, so not static const char *ParseActionToken(const char *Operations, TCrayon *Crayon) { char *Token=NULL; const char *ptr, *nptr; TCrayon *Action=NULL; int val=0; //Do this before anything else! ptr=Operations; Action=NewCrayonAction(Crayon,0); while (StrLen(ptr)) { nptr=GetToken(ptr,"\\S",&Token,GETTOKEN_QUOTES); switch (*Token) { case '=': case '!': case '<': case '>': if (StrValid(Action->Op)) Action=NewCrayonAction(Crayon,0); Action->Op=CopyStrLen(Action->Op, Token, 1); Action->Value=atof(Token+1); Action->String=CopyStr(Action->String, Token+1); Action->Type=CRAYON_COMPARATOR; break; default: nptr=ParseAttribs(ptr,Crayon,&Action); break; } ptr=nptr; } Destroy(Token); return(ptr); }
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); }
void ParseConnectDetails(const char *Str, char **Type, char **Host, char **Port, char **User, char **Pass, char **Path) { char *Token=NULL, *Args=NULL; const char *ptr; ptr=GetToken(Str," ",&Token,0); ParseURL(Token, Type, Host, Port, User, Pass, Path, &Args); if (Path && StrValid(Args)) *Path=MCatStr(*Path,"?",Args,NULL); while (ptr) { if (strcmp(Token,"-password")==0) ptr=GetToken(ptr," ",Pass,0); else if (strcmp(Token,"-keyfile")==0) { ptr=GetToken(ptr," ",&Token,0); *Pass=MCopyStr(*Pass,"keyfile:",Token,NULL); } ptr=GetToken(ptr," ",&Token,0); } DestroyString(Token); DestroyString(Args); }
STREAM *SMTPConnect(const char *Sender, const char *Recipients, int Flags) { char *MailFrom=NULL, *Recip=NULL, *Tempstr=NULL; char *Proto=NULL, *User=NULL, *Pass=NULL, *Host=NULL, *PortStr=NULL; const char *p_MailServer, *ptr; int result=FALSE, Caps=0, RecipientAccepted=FALSE; STREAM *S; p_MailServer=LibUsefulGetValue("SMTP:Server"); if (! StrValid(p_MailServer)) { RaiseError(0, "SendMail", "No Mailserver set"); return(NULL); } if (strncmp(p_MailServer,"smtp:",5) !=0) Tempstr=MCopyStr(Tempstr,"smtp:",p_MailServer,NULL); else Tempstr=CopyStr(Tempstr, p_MailServer); ParseURL(Tempstr, &Proto, &Host, &PortStr, &User, &Pass, NULL, NULL); if (! StrValid(PortStr)) PortStr=CopyStr(PortStr, "25"); Tempstr=MCopyStr(Tempstr,"tcp:",Host,":",PortStr,NULL); //syslog(LOG_DEBUG, "mailto: %s [%s] [%s] [%s]",Tempstr,Proto,Host,PortStr); S=STREAMOpen(Tempstr, ""); if (S) { if (SMTPInteract("", S)) { Caps=SMTPHelo(S); if (Caps > 0) { //try STARTTLS, the worst that will happen is the server will say no if ((! (Flags & SMTP_NOSSL)) && SSLAvailable() && SMTPInteract("STARTTLS\r\n", S)) DoSSLClientNegotiation(S, 0); if ( (Caps & (CAP_AUTH_LOGIN | CAP_AUTH_PLAIN)) && (StrValid(User) && StrValid(Pass)) ) SMTPLogin(S, Caps, User, Pass); //Whether login was needed or not, worked or not, let's try to send a mail Tempstr=MCopyStr(Tempstr, "MAIL FROM: ", Sender, "\r\n", NULL); if (! SMTPInteract(Tempstr, S)) RaiseError(0,"SendMail","mailserver refused sender"); else if (! SmtpSendRecipients(Recipients, S)) RaiseError(0,"SendMail","No recipients accepted by mailserver"); else if (! SMTPInteract("DATA\r\n", S)) RaiseError(0,"SendMail","mailserver refused mail"); else { //we got this far, rest of the process is handled by the calling function result=TRUE; } } else RaiseError(0,"SendMail","Initial mailserver handshake failed"); } else RaiseError(0,"SendMail","Initial mailserver handshake failed"); } else RaiseError(0,"SendMail","mailserver connection failed"); DestroyString(Tempstr); DestroyString(Recip); DestroyString(Proto); DestroyString(User); DestroyString(Pass); DestroyString(Host); DestroyString(PortStr); if (! result) { STREAMClose(S); return(NULL); } return(S); }
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); }