Example #1
0
int DoHTTPProxyTunnel(STREAM *S, const char *Host, int Port, const char *Destination, int Flags)
{
char *Tempstr=NULL, *Token=NULL;
const char *ptr=NULL;
int result=FALSE;

	S->in_fd=ConnectToHost(Host,Port,0); 
	S->out_fd=S->in_fd;
	if (S->in_fd == -1) return(FALSE);

	ptr=Destination;
	if (strncmp(ptr,"tcp:",4)==0) ptr+=4;
	Tempstr=FormatStr(Tempstr,"CONNECT %s HTTP/1.1\r\n\r\n",ptr);

	STREAMWriteLine(Tempstr,S);
	STREAMFlush(S);
	
	Tempstr=STREAMReadLine(Tempstr,S);
	StripTrailingWhitespace(Tempstr);

	ptr=GetToken(Tempstr," ",&Token,0);
	ptr=GetToken(ptr," ",&Token,0);

	if (*Token=='2') result=TRUE;
	while (StrLen(Tempstr))
	{
		Tempstr=STREAMReadLine(Tempstr,S);
		StripTrailingWhitespace(Tempstr);
	}

DestroyString(Tempstr);
DestroyString(Token);

return(result);
}
Example #2
0
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);
}
Example #3
0
int DoHTTPProxyTunnel(STREAM *S, char *Host, int Port, int Flags)
{
char *Tempstr=NULL, *Token=NULL, *ptr=NULL;
int result=FALSE;

	if (Flags & CONNECT_SSL) Tempstr=FormatStr(Tempstr,"CONNECT https://%s:%d HTTP/1.1\r\n\r\n",Host,Port);
	else Tempstr=FormatStr(Tempstr,"CONNECT http://%s:%d HTTP/1.1\r\n\r\n",Host,Port);
	STREAMWriteLine(Tempstr,S);
	STREAMFlush(S);
	
	Tempstr=STREAMReadLine(Tempstr,S);
	StripTrailingWhitespace(Tempstr);

	ptr=GetToken(Tempstr," ",&Token,0);
	ptr=GetToken(ptr," ",&Token,0);

	if (*Token==2) result=TRUE;
	while (StrLen(Tempstr))
	{
		Tempstr=STREAMReadLine(Tempstr,S);
		StripTrailingWhitespace(Tempstr);
	}

DestroyString(Tempstr);
DestroyString(Token);

return(result);
}
Example #4
0
void OAuthParseJSON(char *JSON, ListNode *Vars)
{
char *ptr, *ptr2, *Token=NULL, *Name=NULL, *Value=NULL;

StripLeadingWhitespace(JSON);
StripTrailingWhitespace(JSON);
ptr=JSON+StrLen(JSON)-1;
if (*ptr=='}') *ptr='\0';
ptr=JSON;
if (*ptr=='{') ptr++;
ptr=GetToken(ptr,",",&Token,0);
while (ptr)
{
printf("TOK: %s\n",Token);
ptr2=GetToken(Token,":",&Name,0);
StripTrailingWhitespace(Name);
StripQuotes(Name);
ptr2=GetToken(ptr2,":",&Value,GETTOKEN_QUOTES);
StripLeadingWhitespace(Value);
StripTrailingWhitespace(Value);
StripQuotes(Value);
printf("JSON: %s=%s\n",Name,Value);
SetVar(Vars,Name,Value);
ptr=GetToken(ptr,",",&Token,0);
}


DestroyString(Name);
DestroyString(Value);
DestroyString(Token);
}
Example #5
0
int ConnectHopHTTPSProxy(STREAM *S, const char *Proxy, const char *Destination)
{
    char *Tempstr=NULL, *Token=NULL;
    char *Proto=NULL, *Host=NULL, *User=NULL, *Pass=NULL;
    const char *ptr=NULL;
    int result=FALSE, Port;

    ParseConnectDetails(Proxy, &Token, &Host, &Token, &User, &Pass, NULL);
    Port=atoi(Token);
    if (! (S->State & SS_INITIAL_CONNECT_DONE))
    {
        if (Port==0) Port=443;
        S->in_fd=TCPConnect(Host,Port,0);
        S->out_fd=S->in_fd;
        if (S->in_fd == -1)
        {
            RaiseError(0, "ConnectHopHTTPSProxy", "failed to connect to proxy at %s:%d", Host, Port);
            return(FALSE);
        }
    }

    ptr=Destination;
    if (strncmp(ptr,"tcp:",4)==0) ptr+=4;
    Tempstr=FormatStr(Tempstr,"CONNECT %s HTTP/1.1\r\n\r\n",ptr);

    STREAMWriteLine(Tempstr,S);
    STREAMFlush(S);

    Tempstr=STREAMReadLine(Tempstr,S);
    if (Tempstr)
    {
        StripTrailingWhitespace(Tempstr);

        ptr=GetToken(Tempstr," ",&Token,0);
        ptr=GetToken(ptr," ",&Token,0);

        if (*Token=='2') result=TRUE;
        else RaiseError(0, "ConnectHopHTTPSProxy", "proxy request to %s:%d failed. %s", Host, Port, Tempstr);

        while (StrLen(Tempstr))
        {
            Tempstr=STREAMReadLine(Tempstr,S);
            StripTrailingWhitespace(Tempstr);
        }
    }
    else RaiseError(0, "ConnectHopHTTPSProxy", "proxy request to %s:%d failed. Server Disconnectd.", Host, Port);

    DestroyString(Tempstr);
    DestroyString(Token);
    DestroyString(Host);
    DestroyString(User);
    DestroyString(Pass);

    return(result);
}
Example #6
0
TFileStore *ConfigFileReadFileStore(STREAM *S, char *Name)
{
char *Tempstr=NULL, *Token=NULL, *ptr;
TFileStore *FS=NULL;
char *Type=NULL, *Host=NULL, *Login=NULL, *Password=NULL, *RefreshToken=NULL;
int Flags=0;


Tempstr=STREAMReadLine(Tempstr,S);
while (Tempstr)
{
StripTrailingWhitespace(Tempstr);
StripTrailingWhitespace(Tempstr);

if (strcmp(Tempstr,"}")==0) break;

ptr=GetToken(Tempstr,":",&Token,0);
while (ptr && isspace(*ptr)) ptr++;

if (strcmp(Token,"Type")==0) Type=CopyStr(Type,ptr);
if (strcmp(Token,"Host")==0) Host=CopyStr(Host,ptr);
if (strcmp(Token,"Login")==0) Login=CopyStr(Login,ptr);
if (strcmp(Token,"Password")==0) Password=CopyStr(Password,ptr);
if (strcmp(Token,"RefreshToken")==0) RefreshToken=CopyStr(RefreshToken,ptr);
if (strcmp(Token,"OAuth")==0) Flags |= FS_OAUTH;

Tempstr=STREAMReadLine(Tempstr,S);
}

if (StrLen(Type) && StrLen(Host))
{
	if (StrLen(Login)) Tempstr=MCopyStr(Tempstr,Type,":",	Login,"@",Host,NULL);
	else Tempstr=MCopyStr(Tempstr,Type,":",	Host,NULL);

	if (StrLen(Password)) Tempstr=MCatStr(Tempstr," -password ",Password,NULL);

	FS=FileStoreCreate(Name,Tempstr);
	if (StrLen(RefreshToken)) SetVar(FS->Vars,"OAuthRefreshToken",RefreshToken);
	FS->Settings |= Flags;

}

DestroyString(Host);
DestroyString(Type);
DestroyString(Login);
DestroyString(Tempstr);
DestroyString(Password);
DestroyString(RefreshToken);

return(FS);
}
Example #7
0
void ParseDirListType(char *Data)
{
char *Token=NULL, *ptr;

Settings.DirListFlags=DIR_REJECT;

ptr=GetToken(Data,",",&Token,0);
while (ptr)
{
	StripLeadingWhitespace(Token);
	StripTrailingWhitespace(Token);

	if (strcasecmp(Token,"None")==0) Settings.DirListFlags=DIR_REJECT;
	if (strcasecmp(Token,"Basic")==0) Settings.DirListFlags=DIR_SHOWFILES;
	if (strcasecmp(Token,"Fancy")==0) Settings.DirListFlags=DIR_SHOWFILES | DIR_FANCY;
	if (strcasecmp(Token,"Interactive")==0) Settings.DirListFlags=DIR_SHOWFILES | DIR_FANCY | DIR_INTERACTIVE;
	if (strcasecmp(Token,"Full")==0) Settings.DirListFlags=DIR_SHOWFILES | DIR_FANCY | DIR_INTERACTIVE | DIR_MEDIA_EXT | DIR_SHOW_VPATHS | DIR_TARBALLS;

	if (strcasecmp(Token,"Media")==0) Settings.DirListFlags |= DIR_MEDIA_EXT;
	if (strcasecmp(Token,"IndexPages")==0) Settings.DirListFlags |= DIR_INDEX_FILES;
	if (strcasecmp(Token,"ShowVPaths")==0) Settings.DirListFlags |= DIR_SHOW_VPATHS;
	if (strcasecmp(Token,"TarDownloads")==0) Settings.DirListFlags |= DIR_TARBALLS;
	if (strcasecmp(Token,"MimeIcons")==0) Settings.DirListFlags |= DIR_MIMEICONS;

ptr=GetToken(ptr,",",&Token,0);
}
DestroyString(Token);
}
Example #8
0
ListNode *ConfigFileLoadFileStores(char *Path)
{
STREAM *S;
TFileStore *FS=NULL;
char *Tempstr=NULL, *Token=NULL, *ptr;

if (! FileStores) FileStores=ListCreate();
S=STREAMOpenFile(Path,O_RDONLY);
if (S)
{
	Tempstr=STREAMReadLine(Tempstr,S);
	while (Tempstr)
	{
		StripTrailingWhitespace(Tempstr);
		ptr=GetToken(Tempstr," ",&Token,0);

		if (strcmp(Token,"FileStore")==0)
		{
			FS=ConfigFileReadFileStore(S, ptr);
			ListAddNamedItem(FileStores,FS->Name,FS);
		}

		Tempstr=STREAMReadLine(Tempstr,S);
	}

STREAMClose(S);
}

DestroyString(Tempstr);
DestroyString(Token);

return(FileStores);
}
Example #9
0
void ParseFunction(STREAM *S, const char *Config)
{
char *Token=NULL, *Tempstr=NULL;
const char *ptr;
ListNode *Select;
TCrayon *Crayon;

if (! Functions) Functions=ListCreate();

ptr=GetToken(Config, "\\S", &Token, GETTOKEN_QUOTES);
Select=ListCreate();

ListAddNamedItem(Functions, Token, Select);

Tempstr=STREAMReadLine(Tempstr, S);
while (Tempstr)
{
	StripTrailingWhitespace(Tempstr);
	StripLeadingWhitespace(Tempstr);
	ptr=GetToken(Tempstr, "\\S", &Token, GETTOKEN_QUOTES);
	if (strcmp(Token,"}")==0) break;
	
	if (MatchActionType(Token) > -1)
	{
		 ParseCrayonization("action", Tempstr, Select);
	}
	else ParseCrayonization(Token, ptr, Select);
	Tempstr=STREAMReadLine(Tempstr, S);
}

Destroy(Tempstr);
Destroy(Token);
}
Example #10
0
static void ParseCrayonList(STREAM *S, TCrayon *Crayon)
{
char *Tempstr=NULL, *Token=NULL;
const char *ptr;
ListNode *Curr;
TCrayon *SubItem;

	if (! Crayon) return;
	Tempstr=STREAMReadLine(Tempstr,S);
	while (Tempstr)
	{
		StripTrailingWhitespace(Tempstr);
		StripLeadingWhitespace(Tempstr);

		ptr=GetToken(Tempstr,"\\S",&Token,0);
		if (StrLen(Token) && (*Token != '#'))
		{
			if (strcmp(Token,"}")==0) break;
		
			if (strcmp(Token,"{")==0) ParseCrayonList(S, SubItem);
			else if (CrayonType(Token) == CRAYON_ACTION) ParseCrayonAction(Crayon, Tempstr);
			else
			{
			SubItem=NewCrayonAction(Crayon, 0);
			ParseCrayonEntry(SubItem, Token,ptr);
			}
		}

	Tempstr=STREAMReadLine(Tempstr,S);
	}

Destroy(Tempstr);
Destroy(Token);
}
Example #11
0
void ListNativeFile(STREAM *Out, char *Path)
{
STREAM *S;
char *Tempstr=NULL, *Token=NULL, *SendStr=NULL, *ptr;

S=STREAMOpenFile(Settings.AuthFile,O_RDONLY);
if (S)
{
  Tempstr=STREAMReadLine(Tempstr,S);
  while (Tempstr)
  {
    StripTrailingWhitespace(Tempstr);
    ptr=GetToken(Tempstr,":",&Token,0);
    SendStr=MCopyStr(SendStr,Token," ",NULL);

    ptr=GetToken(ptr,":",&Token,0); //passtype
    ptr=GetToken(ptr,":",&Token,0); //password
    ptr=GetToken(ptr,":",&Token,0); //realuser
    SendStr=MCatStr(SendStr,"realuser="******" ",NULL);
    ptr=GetToken(ptr,":",&Token,0); //homedir
    SendStr=MCatStr(SendStr,"homedir=",Token," ",NULL);
    SendStr=MCatStr(SendStr,ptr,"\n",NULL);

    STREAMWriteLine(SendStr,Out);
    Tempstr=STREAMReadLine(Tempstr,S);
  }
  STREAMClose(S);
}

STREAMFlush(Out);

DestroyString(Tempstr);
DestroyString(SendStr);
DestroyString(Token);
}
Example #12
0
void SendToProxy(TSession *Session, char *Command, char *Arg)
{
char *Tempstr=NULL;

Tempstr=CopyStr(Tempstr,Command);
if (StrLen(Arg) > 0)
{
  Tempstr=CatStr(Tempstr," ");
  Tempstr=CatStr(Tempstr,Arg);
}
Tempstr=CatStr(Tempstr,"\r\n");
STREAMWriteLine(Tempstr,Session->ProxySock);
STREAMFlush(Session->ProxySock);

do
{
   Tempstr=STREAMReadLine(Tempstr,Session->ProxySock);
	StripTrailingWhitespace(Tempstr);
   SendLoggedLine(Session,Tempstr);
} while ( (Tempstr[3]=='-') || (isspace(Tempstr[0])) );

STREAMFlush(Session->ProxySock);
STREAMFlush(Session->ClientSock);
DestroyString(Tempstr);
}
Example #13
0
int DownloadM3U(char *URL, char *Title, int Flags)
{
char *Tempstr=NULL, *ID=NULL, *Doc=NULL, *ptr;
int Port=0, BytesRead=0, len=0, count=0;
int RetVal=FALSE;
ListNode *Items, *Curr;
int M3UType=M3U_PLAYLIST;
STREAM *Con;

if (Flags & FLAG_DEBUG) fprintf(stderr,"M3U STREAM: %s\n",URL);


Items=ListCreate();
Con=ConnectAndRetryUntilDownload(URL, 0, 0);
if (Con)
{
Tempstr=STREAMReadLine(Tempstr,Con);
while (Tempstr)
{
StripTrailingWhitespace(Tempstr);
StripLeadingWhitespace(Tempstr);

if (Flags & (FLAG_DEBUG2 | FLAG_DEBUG3)) fprintf(stderr,"%s\n",Tempstr);
if (StrLen(Tempstr))
{
	if (strncmp("#EXT-X-STREAM-INF",Tempstr,StrLen("#EXT-X-STREAM-INF"))==0)
	{
			RetVal=M3UStreamInfo(Con,Title,URL,Tempstr,Flags);
			M3UType=M3U_STREAMINFO;
	}
	else if (strncmp("#EXT-X-MEDIA-SEQUENCE",Tempstr,StrLen("#EXT-X-MEDIA-SEQUENCE"))==0) M3UType=M3U_PLAYLIST;
	else if (*Tempstr != '#') 
	{
		if (strncasecmp(Tempstr,"http",4) !=0) 
		{
			Doc=CopyStr(Doc,URL);
			ptr=strrchr(Doc,'/');
			if (ptr) *ptr='\0';
			Doc=MCatStr(Doc,"/",Tempstr,NULL);
		}
		else Doc=CopyStr(Doc,Tempstr);
		ListAddItem(Items,CopyStr(NULL,Doc));
	}
}

Tempstr=STREAMReadLine(Tempstr,Con);
}

STREAMClose(Con);
if (M3UType == M3U_PLAYLIST) RetVal=DownloadStream(URL, Title, Items, Flags);
}

ListDestroy(Items,DestroyString);
DestroyString(Tempstr);
DestroyString(Doc);
DestroyString(ID);


return(RetVal);
}
Example #14
0
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,"/"));
}
Example #15
0
void HTTPReadHeaders(STREAM *S, HTTPInfoStruct *Header)
{
char *Tempstr=NULL;


ListClear(Header->ServerHeaders,DestroyString);
Header->ContentLength=0;
Header->RedirectPath=CopyStr(Header->RedirectPath,"");
Header->Flags &= ~(HTTP_CHUNKED | HTTP_GZIP | HTTP_DEFLATE);
Tempstr=STREAMReadLine(Tempstr,S);
if (Tempstr)
{
HTTPParseResponseLine(S, Header,Tempstr);
Tempstr=STREAMReadLine(Tempstr,S);
}

while (Tempstr)
{
StripTrailingWhitespace(Tempstr);
if (StrLen(Tempstr)==0) break;
HTTPParseHeader(S, Header,Tempstr);
Tempstr=STREAMReadLine(Tempstr,S);
}

S->BytesRead=0;
DestroyString(Tempstr);
}
Example #16
0
void HMACSetup(HashratCtx *Ctx)
{
char *Tempstr=NULL, *ptr;
STREAM *S;

  ptr=GetVar(Ctx->Vars,"EncryptionKey");
  if (StrLen(ptr)==0)
  {
    if (isatty(0))
    {
      write(1, "Enter HMAC Key: ",16);

      S=STREAMFromFD(0);
      Tempstr=STREAMReadLine(Tempstr,S);
      StripTrailingWhitespace(Tempstr);
      SetVar(Ctx->Vars,"EncryptionKey",Tempstr);
      ptr=Tempstr;
      STREAMDisassociateFromFD(S);
    }

    //By now we must have an encryption key!
    if (! StrLen(ptr))
    {
      write(1,"ERROR: No HMAC Key given!\n",27);
      exit(2);
    }
  }
DestroyString(Tempstr);
}
Example #17
0
char *SanitizeQueryString(char *Buffer, char *Data)
{
char *Name=NULL, *Value=NULL, *Token=NULL, *ptr;
char *RetStr=NULL;

RetStr=CopyStr(Buffer,"");
ptr=GetNameValuePair(Data,"&","=",&Name,&Value);
while (ptr)
{
	Token=HTTPUnQuote(Token, Value);
	StripTrailingWhitespace(Token);
	Value=SanitizeStr(Value,Token);

	if (RetStr && (*RetStr != '\0')) RetStr=MCatStr(RetStr,"&",Name,"=",Value,NULL);
	else RetStr=MCatStr(RetStr,Name,"=",Value,NULL);
	ptr=GetNameValuePair(ptr,"&","=",&Name,&Value);
}



DestroyString(Name);
DestroyString(Value);
DestroyString(Token);

return(RetStr);
}
Example #18
0
void HTTPHandleWWWAuthenticate(HTTPInfoStruct *Info, char *Line)
{
char *ptr, *ptr2, *Token=NULL, *Name=NULL, *Value=NULL;
char *AuthTypeStrings[]={"Basic","Digest",NULL};
typedef enum {AUTH_BASIC, AUTH_DIGEST} TAuthTypes;
int result;

if (! Info->Authorization) Info->Authorization=(HTTPAuthStruct *) calloc(1,sizeof(HTTPAuthStruct));
ptr=Line;
while (isspace(*ptr)) ptr++;
ptr=GetToken(ptr," ",&Token,0);

result=MatchTokenFromList(Token,AuthTypeStrings,0);
if (result==AUTH_BASIC) Info->Authorization->Flags |=HTTP_AUTH_BASIC;
if (result==AUTH_DIGEST) Info->Authorization->Flags |=HTTP_AUTH_DIGEST;

while (ptr)
{
ptr=GetToken(ptr,",",&Token,GETTOKEN_QUOTES);
StripLeadingWhitespace(Token);
StripTrailingWhitespace(Token);
ptr2=GetToken(Token,"=",&Name,GETTOKEN_QUOTES);
ptr2=GetToken(ptr2,"=",&Value,GETTOKEN_QUOTES);


if (strcasecmp(Name,"realm")==0) Info->Authorization->AuthRealm=CopyStr(Info->Authorization->AuthRealm,Value);
if (strcasecmp(Name,"qop")==0)  Info->Authorization->AuthQOP=CopyStr(Info->Authorization->AuthQOP,Value);
if (strcasecmp(Name,"nonce")==0) Info->Authorization->AuthNonce=CopyStr(Info->Authorization->AuthNonce,Value);
if (strcasecmp(Name,"opaque")==0) Info->Authorization->AuthOpaque=CopyStr(Info->Authorization->AuthOpaque,Value);
}

DestroyString(Token);
DestroyString(Name);
DestroyString(Value);
}
Example #19
0
void OAuthDeviceRefreshToken(char *TokenURL, char *ClientID, char *ClientSecret, char *RequestRefreshToken, char **AccessToken, char **RefreshToken)
{
char *Tempstr=NULL, *Encode=NULL;
ListNode *Vars=NULL;
STREAM *S;

Vars=ListCreate();

Tempstr=MCopyStr(Tempstr,TokenURL,"?client_id=",ClientID,NULL);
Tempstr=MCatStr(Tempstr,"&client_secret=",ClientSecret,NULL);
Tempstr=MCatStr(Tempstr,"&refresh_token=",RequestRefreshToken,NULL);
Tempstr=MCatStr(Tempstr,"&grant_type=","refresh_token",NULL);

S=HTTPMethod("POST",Tempstr,"","","","",0);
if (S)
{
Tempstr=STREAMReadLine(Tempstr,S);
while (Tempstr)
{
StripTrailingWhitespace(Tempstr);
OAuthParseJSON(Tempstr, Vars);

Tempstr=STREAMReadLine(Tempstr,S);
}
} 

*AccessToken=CopyStr(*AccessToken,GetVar(Vars,"access_token"));
*RefreshToken=CopyStr(*RefreshToken,GetVar(Vars,"refresh_token"));

ListDestroy(Vars,DestroyString);
DestroyString(Tempstr);
DestroyString(Encode);
}
Example #20
0
void OAuthInstalledAppGetAccessToken(char *TokenURL, char *ClientID, char *ClientSecret, char *AuthCode, char *RedirectURL, char **AccessToken, char **RefreshToken)
{
char *Tempstr=NULL, *Encode=NULL;
ListNode *Vars=NULL;
STREAM *S;

Vars=ListCreate();

Tempstr=MCopyStr(Tempstr,TokenURL,"?client_id=",ClientID,NULL);
Tempstr=MCatStr(Tempstr,"&client_secret=",ClientSecret,NULL);
Tempstr=MCatStr(Tempstr,"&code=",AuthCode,NULL);
Tempstr=MCatStr(Tempstr,"&redirect_uri=",RedirectURL,NULL);
Tempstr=MCatStr(Tempstr,"&grant_type=","authorization_code",NULL);

S=HTTPMethod("POST",Tempstr,"","","","",0);
if (S)
{
Tempstr=STREAMReadLine(Tempstr,S);
while (Tempstr)
{
StripTrailingWhitespace(Tempstr);
fprintf(stderr,"OA: %s\n",Tempstr);
OAuthParseJSON(Tempstr, Vars);
Tempstr=STREAMReadLine(Tempstr,S);
}
}

*AccessToken=CopyStr(*AccessToken,GetVar(Vars,"access_token"));
*RefreshToken=CopyStr(*RefreshToken,GetVar(Vars,"refresh_token"));

ListDestroy(Vars,DestroyString);
DestroyString(Tempstr);
DestroyString(Encode);
}
Example #21
0
void OAuthDeviceGetAccessToken(char *TokenURL, char *ClientID, char *ClientSecret, char *DeviceCode, char **AccessToken, char **RefreshToken)
{
char *Tempstr=NULL, *Encode=NULL;
ListNode *Vars=NULL;
STREAM *S;

Vars=ListCreate();

Encode=HTTPQuote(Encode,ClientID);
Tempstr=MCopyStr(Tempstr,TokenURL,"?client_id=",Encode,NULL);
Encode=HTTPQuote(Encode,ClientSecret);
Tempstr=MCatStr(Tempstr,"&client_secret=",Encode,NULL);
Tempstr=MCatStr(Tempstr,"&code=",DeviceCode,NULL);
Tempstr=MCatStr(Tempstr,"&grant_type=","http://oauth.net/grant_type/device/1.0",NULL);

S=HTTPMethod("POST",Tempstr,"","","","",0);
if (S)
{
Tempstr=STREAMReadLine(Tempstr,S);
while (Tempstr)
{
StripTrailingWhitespace(Tempstr);
printf("OA: %s\n",Tempstr);
OAuthParseJSON(Tempstr, Vars);
Tempstr=STREAMReadLine(Tempstr,S);
}
}

*AccessToken=CopyStr(*AccessToken,GetVar(Vars,"access_token"));
*RefreshToken=CopyStr(*RefreshToken,GetVar(Vars,"refresh_token"));

ListDestroy(Vars,DestroyString);
DestroyString(Tempstr);
DestroyString(Encode);
}
Example #22
0
void ListNativeFile(char *Path)
{
STREAM *S;
char *Tempstr=NULL, *Token=NULL, *ptr;

S=STREAMOpenFile(Settings.AuthPath,SF_RDONLY);
if (S)
{
	Tempstr=STREAMReadLine(Tempstr,S);
	while (Tempstr)
	{
		StripTrailingWhitespace(Tempstr);
		ptr=GetToken(Tempstr,":",&Token,0);

		printf("%s ",Token);

		ptr=GetToken(ptr,":",&Token,0); //passtype
		ptr=GetToken(ptr,":",&Token,0); //password
		ptr=GetToken(ptr,":",&Token,0); //realuser
		printf("RealUser=%s ",Token);
		ptr=GetToken(ptr,":",&Token,0); //homedir
		printf("Dir=%s %s\n",Token,ptr);

		Tempstr=STREAMReadLine(Tempstr,S);
	}
	STREAMClose(S);
}

DestroyString(Tempstr);
DestroyString(Token);
}
Example #23
0
void HTTPReadHeaders(STREAM *S, HTTPInfoStruct *Info)
{
char *Tempstr=NULL, *ptr;


ListClear(Info->ServerHeaders,DestroyString);
Info->ContentLength=0;

//Not needed
//Info->RedirectPath=CopyStr(Info->RedirectPath,"");
Info->Flags &= ~(HTTP_CHUNKED | HTTP_GZIP | HTTP_DEFLATE);
Tempstr=STREAMReadLine(Tempstr,S);
if (Tempstr)
{
	if (Info->Flags & HTTP_DEBUG) fprintf(stderr,"RESPONSE: %s\n",Tempstr);
  if (strncmp(Tempstr,"HTTP/",5)==0)
  {
    ptr=strchr(Tempstr,' ');
    ptr++;

    Info->ResponseCode=CopyStrLen(Info->ResponseCode,ptr,3);
    STREAMSetValue(S,"HTTP:ResponseCode",Info->ResponseCode);
  
  }
Tempstr=STREAMReadLine(Tempstr,S);
}

while (Tempstr)
{
StripTrailingWhitespace(Tempstr);
if (StrLen(Tempstr)==0) break;
HTTPParseHeader(S, Info,Tempstr);
Tempstr=STREAMReadLine(Tempstr,S);
}

//Handle Response Codes
if (StrLen(Info->ResponseCode))
{
		if (*Info->ResponseCode=='3') 
		{
			//No longer a flag, HTTP Redirect is now just a response code
			//Info->Flags |= HTTP_REDIRECT;
		}

		if (strcmp(Info->ResponseCode,"401")==0) 
		{
			if (Info->Authorization) Info->Authorization->Flags |= HTTP_AUTH_BASIC;
		}

		if (strcmp(Info->ResponseCode,"407")==0) 
		{
			if (Info->ProxyAuthorization) Info->ProxyAuthorization->Flags |= HTTP_PROXY_AUTH;
		}

}

S->BytesRead=0;
DestroyString(Tempstr);
}
Example #24
0
int UploadReadMultipartHeaders(STREAM *S, char **Field, char **FileName)
{
char *Tempstr=NULL, *Name=NULL, *Value=NULL, *ptr;
int result=FALSE;

Tempstr=STREAMReadLine(Tempstr,S);
while (StrLen(Tempstr))
{
StripTrailingWhitespace(Tempstr);
ptr=GetToken(Tempstr,":",&Name,0);

if (strcasecmp(Name,"Content-Disposition")==0)
{
	ptr=GetNameValuePair(ptr,";","=",&Name,&Value);
	while (ptr)
	{
		StripLeadingWhitespace(Name);
		StripTrailingWhitespace(Name);
		StripLeadingWhitespace(Value);
		StripTrailingWhitespace(Value);
	if (strcasecmp(Name,"name")==0) 
	{
		*Field=CopyStr(*Field,Value);
		result=TRUE;
	}

	if (strcasecmp(Name,"filename")==0) 
	{
		*FileName=CopyStr(*FileName,Value);
		result=TRUE;
	}
	ptr=GetNameValuePair(ptr,";","=",&Name,&Value);
	}
}
Tempstr=STREAMReadLine(Tempstr,S);
StripTrailingWhitespace(Tempstr);
}

DestroyString(Tempstr);
DestroyString(Name);
DestroyString(Value);

return(result);
}
Example #25
0
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);
}
Example #26
0
int M3UStreamInfo(STREAM *S, char *Title, char *URL, char *FirstLine, int Flags)
{
	char *Tempstr=NULL, *Doc=NULL, *Resolution=NULL, *Bandwidth=NULL, *ptr;
	ListNode *Vars=NULL;
	int RetVal=FALSE;

	Vars=ListCreate();
	Tempstr=CopyStr(Tempstr,FirstLine);
	while (Tempstr)
	{
	StripTrailingWhitespace(Tempstr);
  if (Flags & (FLAG_DEBUG2 | FLAG_DEBUG3)) fprintf(stderr,"%s\n",Tempstr);

	if (strncmp("#EXT-X-STREAM-INF",Tempstr,StrLen("#EXT-X-STREAM-INF"))==0) M3UParseStreamInfo(Tempstr, &Resolution, &Bandwidth);
	else if (*Tempstr != '#') 
	{
		if (strncasecmp(Tempstr,"http",4) !=0) 
		{
			Doc=CopyStr(Doc,URL);
			ptr=strrchr(Doc,'/');
			if (ptr) *ptr='\0';
			Doc=MCatStr(Doc,"/",Tempstr,NULL);
		}
		else Doc=CopyStr(Doc,Tempstr);

		ptr=FileTypeFromURL(Doc);
		if (strcmp(ptr,"m3u8")==0) ptr="stream";
		if (StrLen(Resolution)) 
		{
			if (StrLen(ptr)) Tempstr=MCopyStr(Tempstr,"item:",ptr,":",Resolution,NULL);
			else Tempstr=MCopyStr(Tempstr,"item:stream:",Resolution,NULL);
		}
		else if (StrLen(Bandwidth)) 
		{
			if (StrLen(ptr)) Tempstr=MCopyStr(Tempstr,"item:",ptr,":",Bandwidth,NULL);
			else Tempstr=MCopyStr(Tempstr,"item:stream:",Bandwidth,NULL);
		}
		else Tempstr=CopyStr(Tempstr,"ID");
		SetVar(Vars,Tempstr,Doc);
	}
	Tempstr=STREAMReadLine(Tempstr,S);
	}

	ptr=GetVar(Vars,"ID");
	if (! StrLen(ptr)) Type=SelectDownloadFormat(Vars, TYPE_REFERENCE, FALSE);
	ptr=GetVar(Vars,"ID");
	if (StrLen(ptr)) RetVal=DownloadM3U(ptr, Title, Flags);

	ListDestroy(Vars,DestroyString);
	DestroyString(Tempstr);
	DestroyString(Resolution);
	DestroyString(Bandwidth);
	DestroyString(Doc);

	return(RetVal);
}
Example #27
0
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);
}
Example #28
0
void M3UParseStreamInfo(char *Line, char **Resolution, char **Bandwidth)
{
char *Name=NULL, *Value=NULL, *ptr;

ptr=GetToken(Line,":",&Name,0);
ptr=GetNameValuePair(ptr,",","=",&Name,&Value);
while (ptr)
{
	StripLeadingWhitespace(Name);
	StripTrailingWhitespace(Name);
	StripLeadingWhitespace(Value);
	StripTrailingWhitespace(Value);
	if (strcasecmp(Name,"RESOLUTION")==0) *Resolution=CopyStr(*Resolution,Value);
	if (strcasecmp(Name,"BANDWIDTH")==0) *Bandwidth=CopyStr(*Bandwidth,Value);
ptr=GetNameValuePair(ptr,",","=",&Name,&Value);
}

DestroyString(Name);
DestroyString(Value);
}
Example #29
0
int AuthNativeFile(HTTPSession *Session, int HTTPDigest, char **RealUser, char **HomeDir, char **UserSettings)
{
STREAM *S;
char *Tempstr=NULL, *ptr;
char *Name=NULL, *Pass=NULL, *PasswordType=NULL, *Trash=NULL;
int RetVal=USER_UNKNOWN;


S=STREAMOpenFile(Settings.AuthPath,SF_RDONLY);
if (! S) return(USER_UNKNOWN);

Tempstr=STREAMReadLine(Tempstr,S);
while (Tempstr)
{

  StripTrailingWhitespace(Tempstr);
	ptr=GetToken(Tempstr,":",&Name,0);

  if (strcasecmp(Name,Session->UserName)==0)
  {
		ptr=GetToken(ptr,":",&PasswordType,0);
		ptr=GetToken(ptr,":",&Pass,0);
		if (RealUser) ptr=GetToken(ptr,":",RealUser,0);
		else ptr=GetToken(ptr,":",&Trash,0);
		if (HomeDir) ptr=GetToken(ptr,":",HomeDir,0);
		else ptr=GetToken(ptr,":",&Trash,0);
		if (UserSettings) ptr=GetToken(ptr,":",UserSettings,0);
		else ptr=GetToken(ptr,":",&Trash,0);
	
		RetVal=FALSE;

		if (HTTPDigest) RetVal=NativeFileCheckHTTPDigestAuth(Session, PasswordType, Pass, Session->Password);
		else RetVal=NativeFileCheckPassword(Name,PasswordType,Pass,Session->Password);

		break;
  }

  Tempstr=STREAMReadLine(Tempstr,S);
}
STREAMClose(S);


if ((RetVal==TRUE) && (Settings.Flags & FLAG_LOG_VERBOSE)) LogToFile(Settings.LogPath,"AUTH: UserName '%s' Authenticated via %s.",Session->UserName,Settings.AuthPath);

AuthenticationsTried=CatStr(AuthenticationsTried,"native ");

DestroyString(Name);
DestroyString(Pass);
DestroyString(Tempstr);
DestroyString(PasswordType);

return(RetVal);
}
Example #30
0
int STREAMAddConnectionHop(STREAM *S, char *Value)
{
char *Tempstr=NULL;

StripTrailingWhitespace(Value);
StripLeadingWhitespace(Value);

if (! S->Values) S->Values=ListCreate();
Tempstr=FormatStr(Tempstr,"ConnectHop:%d",ListSize(S->Values));
STREAMSetValue(S,Tempstr,Value);

DestroyString(Tempstr);
return(TRUE);
}