Ejemplo n.º 1
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);
}
Ejemplo n.º 2
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);
}
Ejemplo n.º 3
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);
}
Ejemplo n.º 4
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);
}
Ejemplo n.º 5
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);
}
Ejemplo n.º 6
0
int SMTPSendMail(const char *Sender, const char *Recipient, const char *Subject, const char *Body, int Flags)
{
    char *Tempstr=NULL;
    STREAM *S;
    int result=FALSE;

    S=SMTPConnect(Sender, Recipient, Flags);
    if (S)
    {
        if (! (Flags & SMTP_NOHEADER))
        {
            Tempstr=MCopyStr(Tempstr,"Date: ", GetDateStr("%a, %d %b %Y %H:%M:%S", NULL), "\r\n", NULL);
            Tempstr=MCatStr(Tempstr,"From: ", Sender, "\r\n", NULL);
            Tempstr=MCatStr(Tempstr,"To: ", Recipient, "\r\n", NULL);
            Tempstr=MCatStr(Tempstr,"Subject: ", Subject, "\r\n\r\n", NULL);
            STREAMWriteLine(Tempstr, S);
        }
        STREAMWriteLine(Body, S);
        STREAMWriteLine("\r\n.\r\n", S);
        SMTPInteract("", S);
        SMTPInteract("QUIT\r\n", S);
        result=TRUE;

        STREAMClose(S);
    }

    DestroyString(Tempstr);
    return(result);
}
Ejemplo n.º 7
0
void HTTPServerSendM3U(STREAM *S, HTTPSession *Session, char *Path, int NoOfFiles, TPathItem **Files)
{
char *Tempstr=NULL, *M3U=NULL, *URL=NULL, *Salt=NULL, *AccessToken=NULL, *ptr;
ListNode *Vars;
STREAM *F;
int i;

M3U=CopyStr(M3U,"#EXTM3U\n");

for (i=0; i < NoOfFiles; i++)
{
	if (InFileTypeList(Files[i]->Path,Settings.M3UFileTypes))
	{

		//Examine file for Artist/title information
		Vars=ListCreate();
		F=STREAMOpenFile(Files[i]->Path, SF_RDONLY);
		if (F) 
		{
			MediaReadDetails(F, Vars);
			STREAMClose(F);
		}
		ptr=GetVar(Vars, "Media-title");
		if (StrLen(ptr))
		{
			//#EXTINF - extra info - length (seconds), title
			Tempstr=CopyStr(Tempstr, GetVar(Vars, "Media-artist"));
			if (! StrLen(Tempstr)) Tempstr=CopyStr(Tempstr,"unknown-artist");
			M3U=MCatStr(M3U,"#EXTINF: -1, ", Tempstr, "-", GetVar(Vars,"Media-title"),"\n",NULL);
		}

		//Actually supply the URL
		M3U=CatStr(M3U,Files[i]->URL);

		//if we are supporting access token authentication, supply that
		if (AuthenticateExamineMethods(Settings.AuthMethods, FALSE) & AUTH_ACCESSTOKEN)
		{
			GenerateRandomBytes(&Salt,24,ENCODE_HEX);
			AccessToken=MakeAccessToken(AccessToken, Session->UserName, Salt, Session->ClientIP, Files[i]->URL);
			M3U=MCatStr(M3U,"?AccessToken=",AccessToken,NULL);
		}
		ListDestroy(Vars,DestroyString);
		M3U=CatStr(M3U,"\n");
	}	
}

Tempstr=MCopyStr(Tempstr,Path,".m3u",NULL);
SetVar(Session->Headers,"Content-disposition",Tempstr);
HTTPServerSendResponse(S, Session, "200 OK","audio/x-mpegurl",M3U);

DestroyString(AccessToken);
DestroyString(Tempstr);
DestroyString(Salt);
DestroyString(URL);
DestroyString(M3U);
}
Ejemplo n.º 8
0
char *HTTPHeadersAppendAuth(char *RetStr, char *AuthHeader, HTTPInfoStruct *Info, HTTPAuthStruct *AuthInfo)
{
char *SendStr=NULL, *Tempstr=NULL;
char *HA1=NULL, *HA2=NULL, *ClientNonce=NULL, *Digest=NULL;
int i, AuthCounter;

if (! AuthInfo) return(RetStr);

SendStr=CatStr(RetStr,"");

	//Authentication by an opaque authentication token that is handled 
	//elsewhere, and is set as the 'Password'
  if (AuthInfo->Flags & HTTP_AUTH_TOKEN)
	{
    SendStr=MCatStr(SendStr,AuthHeader,": ",AuthInfo->Password,"\r\n",NULL);
    AuthInfo->Flags |= HTTP_SENT_AUTH;
	}
  else if (AuthInfo->Flags & HTTP_AUTH_DIGEST)
  {
    AuthCounter++;
    Tempstr=FormatStr(Tempstr,"%s:%s:%s",AuthInfo->Logon,AuthInfo->AuthRealm,AuthInfo->Password);
    HashBytes(&HA1,"md5",Tempstr,StrLen(Tempstr),0);
    Tempstr=FormatStr(Tempstr,"%s:%s",Info->Method,Info->Doc);
    HashBytes(&HA2,"md5",Tempstr,StrLen(Tempstr),0);

    for (i=0; i < 10; i++)
    {
			Tempstr=FormatStr(Tempstr,"%x",rand() % 255);
			ClientNonce=CatStr(ClientNonce,Tempstr);
    }

    Tempstr=FormatStr(Tempstr,"%s:%s:%08d:%s:auth:%s",HA1,AuthInfo->AuthNonce,AuthCounter,ClientNonce,HA2);
    HashBytes(&Digest,"md5",Tempstr,StrLen(Tempstr),0);
    Tempstr=FormatStr(Tempstr,"%s: Digest username=\"%s\",realm=\"%s\",nonce=\"%s\",uri=\"%s\",qop=auth,nc=%08d,cnonce=\"%s\",response=\"%s\"\r\n",AuthHeader,AuthInfo->Logon,AuthInfo->AuthRealm,AuthInfo->AuthNonce,Info->Doc,AuthCounter,ClientNonce,Digest);
    SendStr=CatStr(SendStr,Tempstr);
    AuthInfo->Flags |= HTTP_SENT_AUTH;
  }
  else 
  {
    Tempstr=CopyStr(Tempstr,AuthInfo->Logon);
    Tempstr=CatStr(Tempstr,":");
    Tempstr=CatStr(Tempstr,AuthInfo->Password);
    Digest=SetStrLen(Digest,StrLen(Tempstr) *2);
    to64frombits(Digest,Tempstr,strlen(Tempstr));
    SendStr=MCatStr(SendStr,AuthHeader,": Basic ",Digest,"\r\n",NULL);
    AuthInfo->Flags |= HTTP_SENT_AUTH;
  }

DestroyString(HA1);
DestroyString(HA2);
DestroyString(ClientNonce);
DestroyString(Digest);
DestroyString(Tempstr);

return(SendStr);
}
Ejemplo n.º 9
0
void VPathHandleFilePath(STREAM *S,HTTPSession *Session, TPathItem *VPath, int SendData)
{
char *Tempstr=NULL, *ptr;
char *LocalPath=NULL, *ExternalPath=NULL, *DocName=NULL;

//Document name here is whatever part of the Path is *beyond* the VPath component
DocName=HTTPServerSubstituteArgs(DocName, Session->Path+StrLen(VPath->URL), Session);


ptr=GetToken(VPath->Path,":",&Tempstr,0);
while (ptr)
{
	if (*Tempstr=='/') ExternalPath=MCatStr(ExternalPath,Tempstr,":",NULL);
	else 
	{
		if (StrLen(Tempstr)==0) LocalPath=CatStr(LocalPath,"/:");
		else LocalPath=MCatStr(LocalPath,Tempstr,":",NULL);
	}
	ptr=GetToken(ptr,":",&Tempstr,0);
}

Tempstr=CopyStr(Tempstr,"");
if (StrLen(LocalPath)) Tempstr=FindFileInPath(Tempstr,DocName,LocalPath);

if (StrLen(Tempstr)) HTTPServerSendDocument(S, Session, Tempstr, HEADERS_SENDFILE|HEADERS_USECACHE|HEADERS_KEEPALIVE);
else if (StrLen(ExternalPath))
{
	if (strcmp(Session->Method,"POST")==0)
	{
		if (VPath->Flags & PATHITEM_UPLOAD)
		{
		LogToFile(Settings.LogPath,"%s@%s (%s) uploading to %s in VPATH %s", Session->UserName,Session->ClientHost,Session->ClientIP,DocName,ExternalPath);
		ChrootProcessRequest(S, Session, "POST", DocName, ExternalPath);
		}
		else 
		{
			LogToFile(Settings.LogPath,"%s@%s (%s) uploading DENIED to %s in VPATH %s", Session->UserName,Session->ClientHost,Session->ClientIP,DocName,ExternalPath);
			HTTPServerSendHTML(S, Session, "403 Forbidden","Uploads not allowed to this path.");
		}
	}
	else
	{
	LogToFile(Settings.LogPath,"%s@%s (%s) asking for external document %s in Search path %s", Session->UserName,Session->ClientHost,Session->ClientIP,DocName,ExternalPath);
	ChrootProcessRequest(S, Session, "GETF", DocName, ExternalPath);
	}
}
//This will send '404'
else HTTPServerSendDocument(S, Session, DocName, HEADERS_SENDFILE|HEADERS_USECACHE|HEADERS_KEEPALIVE);

DestroyString(DocName);
DestroyString(Tempstr);
DestroyString(LocalPath);
DestroyString(ExternalPath);
}
Ejemplo n.º 10
0
STREAM *IDriveOpenFile(TFileStore *FS, TFileInfo *FI, int Flags)
{
char *URL=NULL, *Tempstr=NULL, *Boundary=NULL, *FullPath=NULL, *ptr;
HTTPInfoStruct *Info;
TFileInfo *tmpFI;

  if (Flags & OPEN_WRITE)
	{
  URL=MCopyStr(URL,"https://",FS->Host,"/evs/uploadFile",NULL);
	Info=HTTPInfoFromURL("POST",URL);
	
	Boundary=FormatStr(Boundary,"%x-%x-%x",getpid(),time(NULL),rand());
	Info->PostContentType=MCopyStr(Info->ContentType,"multipart/form-data; boundary=",Boundary,NULL);

	Tempstr=MCopyStr(Tempstr,"--",Boundary,"\r\n","Content-disposition: form-data; name=uid\r\n\r\n",FS->Logon,"\r\n",NULL);
	Tempstr=MCatStr(Tempstr,"--",Boundary,"\r\n","Content-disposition: form-data; name=pwd\r\n\r\n",FS->Passwd,"\r\n",NULL);
	Tempstr=MCatStr(Tempstr,"--",Boundary,"\r\n","Content-disposition: form-data; name=p\r\n\r\n",FS->CurrDir,"\r\n",NULL);
	//Tempstr=MCatStr(Tempstr,"--",Boundary,"\r\n","Content-disposition: form-data; name=myfiles\r\n\r\n",Path,"\r\n",NULL);
	Tempstr=MCatStr(Tempstr,"--",Boundary,"\r\n","Content-disposition: form-data; charset=UTF-8; name=definition; filename=",FI->Path,"\r\nContent-type: image/jpeg\r\n\r\n",NULL);

	Info->PostContentLength=FI->Size+StrLen(Tempstr)+StrLen(Boundary)+8;
	FS->S=HTTPTransact(Info);
	FS->Extra=Info;

	STREAMWriteLine(Tempstr,FS->S);
	STREAMSetValue(FS->S,"Boundary",Boundary);
	Tempstr=FormatStr(Tempstr,"%d",FI->Size);
	STREAMSetValue(FS->S,"Transfer-Size",Tempstr);
	}
	else
	{
			if (*FI->Path=='/') FullPath=CopyStr(FullPath,FI->Path);
			else FullPath=MCopyStr(FullPath,FS->CurrDir,FI->Path,NULL);

			Tempstr=HTTPQuote(Tempstr,FullPath);

//		if (OpenFI->Version) FI=OpenFI;
//		else FI=FileStoreGetFileInfo(FS, FI->Path);

		  URL=FormatStr(URL,"https://%s/evs/downloadFile?uid=%s&pwd=%s&p=%s&version=%d",FS->Host,FS->Logon,FS->Passwd,Tempstr,FI->Version);
			FS->S=HTTPMethod("POST",URL,"","","","",0);
			FS->Extra=NULL;
//		if (FI != OpenFI) FileInfoDestroy(FI);
	}

	DestroyString(URL);
	DestroyString(FullPath);
	DestroyString(Tempstr);
	DestroyString(Boundary);

   return(FS->S);
}
Ejemplo n.º 11
0
void DisplayTransferStatus(char *Line, unsigned int transferred, unsigned int total, unsigned int *percent, unsigned int secs,int CmdFlags, int Throttle)
{
int result=0, cols=80, bps=0;
char *Tempstr=NULL, *TimeStr=NULL, *ptr=NULL;
static int ThrotCount=0;
struct winsize w;


	if (Settings.Flags & FLAG_QUIET) return;
	if (CmdFlags & FLAG_QUIET) return;
	if (! isatty(1)) return;

	ptr=getenv("COLUMNS");
	if (ptr) cols=atoi(ptr);
	printf("\r");
	if (total==0) Tempstr=FormatStr(Tempstr,"%s bytes sent ",GetHumanReadableDataQty(transferred,0));
	else
	{
	result=(transferred * 100) / total;
//	if (result != *percent)
	{
		Tempstr=FormatStr(Tempstr,"%d%% %s of ",result,GetHumanReadableDataQty(transferred,0));
		Tempstr=CatStr(Tempstr,GetHumanReadableDataQty(total,0));
	}
	*percent=result;
	}

bps=transferred / secs;
TimeStr=FormatStr(TimeStr," in %d secs %s Bps ",secs,GetHumanReadableDataQty(bps,0) );

if ((Throttle > 0) && (bps > Throttle)) 
{
	ThrotCount++;
	usleep(ThrotCount * 250000);
}
else if (ThrotCount > 0) ThrotCount--;

if (ThrotCount > 0) Tempstr=MCatStr(Tempstr,TimeStr, Line, " (throttling)      ",NULL);
else Tempstr=MCatStr(Tempstr,TimeStr, Line, "           ",NULL);


ioctl(0, TIOCGWINSZ, &w);
if (StrLen(Tempstr) > w.ws_col) Tempstr[w.ws_col]='\0';

printf("%s",Tempstr);
fflush(NULL);

DestroyString(TimeStr);
DestroyString(Tempstr);
}
Ejemplo n.º 12
0
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);
}
Ejemplo n.º 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);
}
Ejemplo n.º 14
0
void PostProcessSettings(TSettings *Settings)
{
char *Tempstr=NULL, *Token=NULL, *ptr;

if (StrLen(Settings->DefaultUser)==0) Settings->DefaultUser=CopyStr(Settings->DefaultUser,GetDefaultUser());

Tempstr=CopyStr(Tempstr,"");
ptr=GetToken(Settings->HttpMethods,",",&Token,0);
while (ptr)
{
if (strcmp(Token,"BASE")==0) Tempstr=CatStr(Tempstr,"GET,POST,HEAD,OPTIONS,");
else if (strcmp(Token,"DAV")==0) Tempstr=CatStr(Tempstr,"GET,POST,HEAD,OPTIONS,DELETE,MKCOL,MOVE,COPY,PUT,PROPFIND,PROPPATCH,");
else if (strcmp(Token,"PROXY")==0) Tempstr=CatStr(Tempstr,"CONNECT,RGET,RPOST");
else Tempstr=MCatStr(Tempstr,Token,",",NULL);

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

Settings->HttpMethods=CopyStr(Settings->HttpMethods,Tempstr);

AuthenticateExamineMethods(Settings->AuthMethods, TRUE);

if (Settings->Port < 1)
{
  if (Settings->Flags & FLAG_SSL) Settings->Port=443;
  else Settings->Port=80;
}

DestroyString(Tempstr);
DestroyString(Token);
}
Ejemplo n.º 15
0
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);
}
Ejemplo n.º 16
0
char *FancyHeading(char *Buffer,const char *Name, int SortType, const char *CurrURL, int Flags)
{
char *SortName="";
int CurrSortType=0;

CurrSortType=Flags & SORT_TYPE_MASK;
switch (SortType)
{
case SORT_TYPE:
case SORT_RTYPE:
	if (CurrSortType == SORT_RTYPE) SortName="type";
	else SortName="rtype";
break;

case SORT_SIZE:
case SORT_RSIZE:
	if (CurrSortType == SORT_RSIZE) SortName="size";
	else SortName="rsize";
break;

case SORT_TIME:
case SORT_RTIME:
	if (CurrSortType == SORT_RTIME) SortName="time";
	else SortName="rtime";
break;

case SORT_NAME:
case SORT_RNAME:
	if (CurrSortType == SORT_RNAME) SortName="name";
	else SortName="rname";
break;
}

return(MCatStr(Buffer,"<th><a href=\"",CurrURL,"?sort=",SortName,"\">",Name,"</a></th>",NULL));
}
Ejemplo n.º 17
0
void ParsePermittedCommands(TSettings *Settings, char *Features)
{
char *ptr, *Token=NULL;

Settings->PermittedCommands=CopyStr(Settings->PermittedCommands,"");
ptr=GetToken(Features,",",&Token,GETTOKEN_QUOTES);
while (ptr)
{
	if (strcasecmp(Token,"Basic")==0) Settings->PermittedCommands=MCatStr(Settings->PermittedCommands,BASIC_COMMANDS,",",NULL);
	else Settings->PermittedCommands=MCatStr(Settings->PermittedCommands,Token,",",NULL);

	ptr=GetToken(ptr,",",&Token,GETTOKEN_QUOTES);
}

DestroyString(Token);
}
Ejemplo n.º 18
0
void HandleUserSetup(char *Operation, int argc, char *argv[])
{
 int i, result;
 char *UserName=NULL, *Password=NULL, *PassType=NULL, *HomeDir=NULL, *RealUser=NULL, *Args=NULL;

	if (strcmp(Operation,"del")==0) PassType=CopyStr(PassType,"delete");
	else PassType=CopyStr(PassType,"sha256");
	HomeDir=CopyStr(HomeDir,"/tmp");
	RealUser=CopyStr(RealUser,GetDefaultUser());
	Password=CopyStr(Password,"");
	Args=CopyStr(Args,"");

 for (i=3; i < argc; i++)
 {
	if (strcmp(argv[i],"-e")==0)
	{
		i++;
		if (strcmp(Operation,"del") !=0) PassType=CopyStr(PassType,argv[i]);
	}
	else if (strcmp(argv[i],"-h")==0)
	{
		i++;
		HomeDir=CopyStr(HomeDir,argv[i]);
	}
	else if (strcmp(argv[i],"-u")==0)
	{
		i++;
		RealUser=CopyStr(RealUser,argv[i]);
	}
	else if (strcmp(argv[i],"-a")==0)
	{
		i++;
		Settings.AuthPath=CopyStr(Settings.AuthPath,argv[i]);
	}
	else if (StrLen(UserName)==0) UserName=CopyStr(UserName,argv[i]);
	else if (StrLen(Password)==0) Password=CopyStr(Password,argv[i]);
	else Args=MCatStr(Args,argv[i]," ",NULL);
 }

	if (strcmp(Operation,"list")==0) ListNativeFile(Settings.AuthPath);
	else if (! StrLen(UserName)) printf("ERROR: NO USERNAME GIVEN\n");
	else if ((strcmp(Operation,"add")==0) && (! StrLen(Password))) printf("ERROR: NO PASSWORD GIVEN\n");
	else 
	{
		result=UpdateNativeFile(Settings.AuthPath, UserName, PassType, Password, HomeDir,RealUser, Args);
		if (result==ERR_FILE) printf("ERROR: Cannot open file '%s'\n",Settings.AuthPath);
	}

 DestroyString(UserName);
 DestroyString(Password);
 DestroyString(PassType);
 DestroyString(RealUser);
 DestroyString(HomeDir);
 DestroyString(Args);

	//Always exit when this is done, don't launch webserver
 exit(0);
}
Ejemplo n.º 19
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);
}
Ejemplo n.º 20
0
char *HashAvailableTypes(char *RetStr)
{
int i;

RetStr=CopyStr(RetStr,"");
for (i=0; HashTypes[i] !=NULL; i++) 
{
	RetStr=MCatStr(RetStr, HashTypes[i], ",", NULL);
}
return(RetStr);
}
Ejemplo n.º 21
0
void OAuthInstalledAppURL(char *LoginURL, char *ClientID, char *Scope, char *RedirectURL, char **NextURL)
{
char *Encode=NULL;

Encode=HTTPQuote(Encode,ClientID);
*NextURL=MCopyStr(*NextURL,LoginURL,"?response_type=code&redirect_uri=",RedirectURL,"&client_id=",Encode,NULL);
Encode=HTTPQuote(Encode,Scope);
*NextURL=MCatStr(*NextURL,"&scope=",Encode,NULL);


DestroyString(Encode);
}
Ejemplo n.º 22
0
char *DisplayPackAction(char *HTML, HTTPSession *Session)
{
char *Name=NULL, *Value=NULL, *ptr;

//if (Flags & DIR_TARBALLS) 
if (StrLen(Settings.PackFormats))
{
ptr=GetNameValuePair(Settings.PackFormats,",",":",&Name,&Value);

HTML=CatStr(HTML,"<td align=center colspan='2' bgcolor='skyblue'>Download as <select name=\"PackType\">");
while (ptr)
{
	HTML=MCatStr(HTML,"<option value=\"",Name,"\">",Name,NULL);
	ptr=GetNameValuePair(ptr,",",":",&Name,&Value);
}

HTML=MCatStr(HTML,"</select><input type=submit name='pack:",Session->URL,"' value='Pack'></td>",NULL);
}

return(HTML);
}
Ejemplo n.º 23
0
char *FinalizeDirListHTML(char *Buffer, HTTPSession *Session, const char *Path, const char *DirItemsHtml, const char *MimeIconsURL, int Flags)
{
char *HTML=NULL;

	HTML=FormatStr(Buffer,"<html>\r\n<head><title>/%s%s</title></head>\r\n<body>\r\n",Session->Host, Session->URL);

	if ((Flags & DIR_FANCY))
	{
		if (Flags & DIR_INTERACTIVE) HTML=CatStr(HTML,"<form>\r\n");

		HTML=CatStr(HTML,"<table align=center border=0><tr>\n");
		if (Settings.Flags & FLAG_SSL) HTML=MCatStr(HTML,"<td><font color=green size=-1>SECURE<br/>",Session->Cipher,"</font></td>\n",NULL);
		else HTML=MCatStr(HTML,"<td><font color=red size=-1>Unencrypted<br/>Connection</font></td>\n",NULL);

		HTML=MCatStr(HTML,"<td><b>",Session->URL,"</b> at ",Session->Host, " <i>",GetDateStrFromSecs("%Y/%m/%d %H:%M:%S",Now,NULL),"</i><br/>",NULL);

		HTML=DisplayDirActions(HTML,Session,Flags);
		HTML=CatStr(HTML,"</td>\n");


		HTML=MCatStr(HTML,"<td>User: "******"<br/>",NULL);
		if (Settings.Flags & FLAG_LOGOUT_AVAILABLE) HTML=MCatStr(HTML,"<a href=\"",GetLogoutPath(),"\">( Logout )</a>",NULL);
		HTML=CatStr(HTML,"</td></tr></table>\n");
	}
	HTML=MCatStr(HTML,DirItemsHtml,"<br />&nbsp;<br />",NULL);
	if (Flags & DIR_INTERACTIVE) HTML=CatStr(HTML,"</form>\r\n");
	HTML=CatStr(HTML,"</body></html>\r\n");

return(HTML);
}
Ejemplo n.º 24
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);
}
Ejemplo n.º 25
0
char *OAuthFormatLoginURL(char *RetStr, const char *LoginURL, const char *ClientID, const char *Scope, const char *RedirectURL)
{
char *Encode=NULL;

if (RedirectURL==NULL) RedirectURL="urn:ietf:wg:oauth:2.0:oob";
Encode=HTTPQuote(Encode,ClientID);
RetStr=MCopyStr(RetStr,LoginURL,"?response_type=code&redirect_uri=",RedirectURL,"&client_id=",Encode,NULL);
Encode=HTTPQuote(Encode,Scope);
RetStr=MCatStr(RetStr,"&scope=",Encode,NULL);

DestroyString(Encode);

return(RetStr);
}
Ejemplo n.º 26
0
void UploadSelectPage(STREAM *S,HTTPSession *Session,char *Path)
{
char *HTML=NULL, *Tempstr=NULL;
int i;

  HTML=MCopyStr(HTML,"<html>\r\n<head><title>Upload files to: ",Session->URL,"</title></head>\r\n<body><form method=\"post\" enctype=\"multipart/form-data\" action=\"",Session->URL,"\">\r\n",NULL);

  HTML=MCatStr(HTML,"<p align=center>Upload files to: ",Session->URL,"</p>\r\n",NULL);
	HTML=CatStr(HTML,"<table align=center border=0><tr><th bgcolor=#AAAAFF>Select files for upload</th></tr>\r\n");
	for (i=0; i < 10; i++)
	{
  Tempstr=FormatStr(Tempstr,"<tr><td><input type=file name=uploadfile:%d></td></tr>\r\n",i);
  HTML=CatStr(HTML,Tempstr);
	}
	HTML=MCatStr(HTML,"<tr><td><input type=submit value=Upload></td></tr></table>\r\n",NULL);

  HTML=MCatStr(HTML,"</form></body></html>\r\n",NULL);

	HTTPServerSendResponse(S, Session, "200 OK","text/html",HTML);

DestroyString(HTML);
DestroyString(Tempstr);
}
Ejemplo n.º 27
0
//Sanitize is more than 'Clean', it clears out any HTML string
char *SanitizeStr(char *Buffer, char *Data)
{
char *TagNamespace=NULL, *TagType=NULL, *TagData=NULL, *ptr;
char *RetStr=NULL, *Tempstr=NULL;

ptr=XMLGetTag(Data, &TagNamespace, &TagType, &TagData);
while (ptr)
{
	if (StrLen(TagType)==0) Tempstr=CatStr(Tempstr,TagData);
	else
	{
		if (
				ListFindNamedItem(Settings.SanitizeArgumentsAllowedTags,TagType) ||
				((*TagType=='/') && ListFindNamedItem(Settings.SanitizeArgumentsAllowedTags,TagType+1))
			)
		{
			if (StrLen(TagNamespace)) Tempstr=MCatStr(Tempstr,"<",TagNamespace,":",TagType,NULL);
			else Tempstr=MCatStr(Tempstr,"<",TagType,NULL);
			if (StrLen(TagData)) Tempstr=MCatStr(Tempstr," ",TagData,NULL);
			Tempstr=CatStr(Tempstr,">");
		}
	}


ptr=XMLGetTag(ptr, &TagNamespace, &TagType, &TagData);
}

RetStr=HTTPQuote(Buffer,Tempstr);

DestroyString(TagNamespace);
DestroyString(Tempstr);
DestroyString(TagType);
DestroyString(TagData);


return(RetStr);
}
Ejemplo n.º 28
0
char *DisplayDirActions(char *Buffer, HTTPSession *Session, int Flags)
{
char *HTML=NULL;

if (Flags & DIR_INTERACTIVE)
{
HTML=MCatStr(HTML, "<script>function setCheckboxes(OnOrOff){ var i; var checkboxes=document.getElementsByName('selected'); for (i=0; i < checkboxes.length; i++) { if (OnOrOff==1) checkboxes[i].checked=true; else checkboxes[i].checked=false; } return(false); }</script>",NULL);
HTML=CatStr(Buffer,"<table align=center>\r\n");

	HTML=CatStr(HTML,"<tr>");
	if (Flags & DIR_TARBALLS) HTML=DisplayPackAction(HTML, Session);
	if ((Flags & DIR_HASMEDIA) && (Flags & DIR_MEDIA_EXT))
	{
		HTML=MCatStr(HTML,"<td bgcolor=\"#FFAAFF\"><input type=submit name='m3u:",Session->URL,"' value='M3U Playlist'></td>",NULL);

		/* One day, but not yet
		HTML=MCatStr(HTML,"<audio id=\"audioplayer\" controls autoplay></audio>",NULL);
		HTML=MCatStr(HTML,"<script>function addaudio(url){document.getElementById('audioplayer').src=url;}</script></td>",NULL);
		*/

	}
	HTML=CatStr(HTML,"</tr>");

	if (Session->Flags & SESSION_UPLOAD) 
	{
	HTML=CatStr(HTML,"<tr>");
	HTML=MCatStr(HTML,"<td align=center bgcolor='pink'><input type=submit name='upload:",Session->URL,"' value='Upload Files'></td>",NULL);
	HTML=MCatStr(HTML,"<td align=center bgcolor='yellow'><input type=submit name='mkdir:",Session->URL,"' value='MkDir'><input type=text name=mkdir></td>",NULL);
	HTML=MCatStr(HTML,"<td align=center bgcolor='red'><input type=submit name='delete-selected:",Session->URL,"' value='Delete Selected'></td>",NULL);
	HTML=CatStr(HTML,"</tr>");
	}

	HTML=CatStr(HTML,"</table>");
}

return(HTML);
}
Ejemplo n.º 29
0
void ConfigFileSaveFileStores()
{
STREAM *S;
char *Tempstr=NULL, *PortStr=NULL, *ptr;
ListNode *Curr;
TFileStore *FS;


Tempstr=MCopyStr(Tempstr,GetCurrUserHomeDir(),"/.fileferry.conf",NULL);
S=STREAMOpenFile(Tempstr,O_CREAT | O_TRUNC | O_WRONLY);
if (S)
{
	Curr=ListGetNext(FileStores);
	while (Curr)
	{	
		FS=(TFileStore *) Curr->Item;	
		PortStr=FormatStr(PortStr,"%d",FS->Port);
		if (! StrLen(FS->Name)) Tempstr=MCopyStr(Tempstr,"FileStore ",FS->Type,":",FS->Logon,"@",FS->Host,"\n{\n",NULL);
		else Tempstr=MCopyStr(Tempstr,"FileStore ",FS->Name,"\n{\n",NULL);
		Tempstr=MCatStr(Tempstr, "Type: ",FS->Type,"\n","Host: ",FS->Host,"\n","Port: ",PortStr,"\n","Login: "******"\n","Password: "******"\n",NULL);
		if (FS->Settings & FS_OAUTH) 
		{
			ptr=GetVar(FS->Vars,"OAuthRefreshToken");
			if (StrLen(ptr)) Tempstr=MCatStr(Tempstr, "OAuth\nRefreshToken: ", ptr,"\n",NULL);
		}
		Tempstr=MCatStr(Tempstr, "}\n\n",NULL);
		STREAMWriteLine(Tempstr,S);
		Curr=ListGetNext(Curr);
	}
	STREAMClose(S);
}
else printf("ERROR: Failed to open config file\n");

DestroyString(Tempstr);
DestroyString(PortStr);
}
Ejemplo n.º 30
0
//Parse a list of packing formats and their associated commands so we can offer the user
//'download as zip' in the directory webpage
char *ParsePackFormats(char *RetStr, const char *Config)
{
char *Name=NULL, *Value=NULL, *ptr, *tptr;
char *Path=NULL;

RetStr=CopyStr(RetStr,"");
ptr=GetNameValuePair(Config, ",",":",&Name,&Value);
while (ptr)
{
if (StrLen(Name) && StrLen(Value)) 
{
	if (strcasecmp(Value,"internal")==0) RetStr=MCatStr(RetStr,Name,":",Value,",",NULL);
	else
	{
		tptr=strchr(Value,' ');
		if (tptr)
		{
			*tptr='\0';
			tptr++;
		}
		//we don't want this to be null if strchr returns a null, otherwise it will
		//shorten our sting when we use MCatStr below.
		else tptr="";

		Path=FindFileInPath(Path,Value,getenv("PATH"));
		if (StrLen(Path)) RetStr=MCatStr(RetStr,Name,":",Path," ", tptr, ",",NULL);
	}
}
ptr=GetNameValuePair(ptr, ",",":",&Name,&Value);
}

DestroyString(Name);
DestroyString(Value);

return(RetStr);
}