Example #1
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);
}
Example #2
0
void OpenSSLReseedRandom()
{
int len=32;
char *Tempstr=NULL;


len=GenerateRandomBytes(&Tempstr, len, ENCODE_NONE);
RAND_seed(Tempstr,len);
memset(Tempstr,0,len); //extra paranoid step, don't keep those bytes in memory!

DestroyString(Tempstr);
}
Example #3
0
void InitSettings()
{
time_t Now;
struct utsname UnameData;

//Initialise timezone information, this is so that
//we don't get erratic times in log files from forked
//chrooted processes
time(&Now);
localtime(&Now);
srand(Now+getpid());
SetTimezoneEnv();


uname(&UnameData);
memset(&Settings,0,sizeof(TSettings));
Settings.MaxLogSize=999999;
Settings.MaxLogRotate=5;
Settings.LogPath=CopyStr(Settings.LogPath,"SYSLOG");
Settings.ConfigPath=CopyStr(Settings.ConfigPath,"/etc/alaya.conf");
Settings.DefaultDir=CopyStr(Settings.DefaultDir,"./");
Settings.BindAddress=CopyStr(Settings.BindAddress,"");
Settings.Flags |= FLAG_KEEPALIVES;
Settings.DirListFlags=DIR_SHOWFILES | DIR_FANCY;
Settings.AuthFlags=FLAG_AUTH_REQUIRED | FLAG_AUTH_COOKIE;
Settings.AuthPath=CopyStr(Settings.AuthPath,"/etc/alaya.auth");
Settings.AuthMethods=CopyStr(Settings.AuthMethods,"accesstoken,cookie,native");
Settings.AuthRealm=CopyStr(Settings.AuthRealm,UnameData.nodename);
Settings.IndexFiles=CopyStr(Settings.IndexFiles,"index.html,dir.html");
Settings.M3UFileTypes=CopyStr(Settings.M3UFileTypes,".mp3,.ogg,.mp4,.flv,.webm,.m4v,.m4a,.aac,.wma,.wmv");
Settings.ForbiddenURLStrings=CopyStr(Settings.ForbiddenURLStrings,"..,%00,%2e%2e");
Settings.VPaths=ListCreate();
Settings.HostConnections=ListCreate();
Settings.ScriptHandlers=ListCreate();
Settings.LoginEntries=ListCreate();
Settings.DocumentCacheTime=10;
Settings.AddressSpace=CopyStr(Settings.AddressSpace, "250M");
Settings.StackSize=CopyStr(Settings.StackSize, "1M");
Settings.ActivityTimeout=10000;
Settings.PackFormats=CopyStr(Settings.PackFormats,"tar:internal,zip:zip -");

GenerateRandomBytes(&Settings.AccessTokenKey,32,ENCODE_BASE64);
//this will be set to 80 or 443 in 'PostProcessSettings'
Settings.Port=0;

}
Example #4
0
nsresult
GenerateGUID(nsCString& _guid)
{
  _guid.Truncate();

  // Request raw random bytes and base64url encode them.  For each set of three
  // bytes, we get one character.
  const PRUint32 kRequiredBytesLength =
    static_cast<PRUint32>(GUID_LENGTH / 4 * 3);

  PRUint8 buffer[kRequiredBytesLength];
  nsresult rv = GenerateRandomBytes(kRequiredBytesLength, buffer);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = Base64urlEncode(buffer, kRequiredBytesLength, _guid);
  NS_ENSURE_SUCCESS(rv, rv);

  NS_ASSERTION(_guid.Length() == GUID_LENGTH, "GUID is not the right size!");
  return NS_OK;
}
Example #5
0
void RandomGenerator::getBytes(void* p, size_t size)
{
	while (size > 0)
	{
		const size_t size2 = MIN(size, BUFFER_SIZE);

		if (bufferPos + size2 > BUFFER_SIZE)
		{
			if (bufferPos < BUFFER_SIZE)
				memmove(buffer, buffer + bufferPos, BUFFER_SIZE - bufferPos);
			GenerateRandomBytes(buffer + (BUFFER_SIZE - bufferPos), bufferPos);
			bufferPos = 0;
		}

		memcpy(p, buffer + bufferPos, size2);
		p = ((char*) p) + size2;
		bufferPos += size2;
		size -= size2;
	}
}
Example #6
0
void GenerateGuid(Guid* guid)
{
	GenerateRandomBytes(guid, sizeof(Guid));
	guid->data3 = (4 << 12) | (guid->data3 & 0xFFF);	// version 4
	guid->data4[0] = 0x80 | (guid->data4[0] & 0x3F);	// variant
}
Example #7
0
int UpdateNativeFile(const char *Path, const char *Name, const char *iPassType, const char *Pass, const char *iHomeDir, const char *iRealUser, const char *iArgs)
{
STREAM *S;
ListNode *Entries;
char *Tempstr=NULL, *Token=NULL, *ptr;
char *PassType=NULL, *HomeDir=NULL, *RealUser=NULL, *Args=NULL, *Salt=NULL;
ListNode *Curr;
int RetVal=ERR_FILE;

Entries=ListCreate();
S=STREAMOpenFile(Path,O_RDONLY);

if (S)
{
	Tempstr=STREAMReadLine(Tempstr,S);
	while (Tempstr)
	{
		ptr=GetToken(Tempstr,":",&Token,0);

		if (strcmp(Token,Name) !=0) ListAddItem(Entries,CopyStr(NULL,Tempstr));	
		else 
		{
			StripTrailingWhitespace(Tempstr);
			ptr=GetToken(ptr,":",&PassType,0);
			ptr=GetToken(ptr,":",&Salt,0);
			ptr=GetToken(ptr,":",&Token,0);
			ptr=GetToken(ptr,":",&RealUser,0);
			ptr=GetToken(ptr,":",&HomeDir,0);
			ptr=GetToken(ptr,":",&Args,0);
		}
	
		Tempstr=STREAMReadLine(Tempstr,S);
	}
	STREAMClose(S);
}

if (iPassType) PassType=CopyStr(PassType,iPassType);
if (iHomeDir) HomeDir=CopyStr(HomeDir,iHomeDir);
if (iRealUser) RealUser=CopyStr(RealUser,iRealUser);
if (iArgs) Args=CopyStr(Args,iArgs);


S=STREAMOpenFile(Path,O_WRONLY| O_CREAT | O_TRUNC);
if (S)
{
	//First copy all other entries
	Curr=ListGetNext(Entries);
	while (Curr)
	{
		STREAMWriteLine((char *) Curr->Item,S);
		Curr=ListGetNext(Curr);
	}
	STREAMFlush(S);


	if (strcmp(PassType,"delete")==0)
	{
		//Don't bother to write new entry, effectively deleting user
	}
	else //WriteNew Entry
	{
		//Do this or else HashBytes appends
		Token=CopyStr(Token,"");
		if (strcmp(PassType,"plain") == 0) Token=CopyStr(Token,Pass);
		else if (strcmp(PassType,"challenge") == 0) Token=CopyStr(Token,Pass);
		else 
		{
		  //Generate a new salt
			GenerateRandomBytes(&Salt,20,ENCODE_HEX);
			Tempstr=MCopyStr(Tempstr,Name,Salt,Pass,NULL);
			HashBytes(&Token, PassType, Tempstr, StrLen(Tempstr), ENCODE_HEX);
		}
		Tempstr=MCopyStr(Tempstr,Name,":",PassType,":",Salt,":",Token,":",RealUser,":",HomeDir,":",Args,"\n",NULL);
	
		STREAMWriteLine(Tempstr,S);
	}

	STREAMClose(S);
	RetVal=ERR_OKAY;
}

DestroyString(Args);
DestroyString(Salt);
DestroyString(HomeDir);
DestroyString(RealUser);
DestroyString(PassType);
DestroyString(Tempstr);
DestroyString(Token);
ListDestroy(Entries,DestroyString);

return(RetVal);
}
Example #8
0
int UpdateNativeFile(char *Path, char *Name, char *PassType, char *Pass, char *HomeDir, char *RealUser, char *Args)
{
STREAM *S;
ListNode *Entries;
char *Tempstr=NULL, *Token=NULL, *Salt=NULL;
ListNode *Curr;
int RetVal=ERR_FILE;


Entries=ListCreate();
S=STREAMOpenFile(Settings.AuthPath,SF_RDONLY);

if (S)
{
	Tempstr=STREAMReadLine(Tempstr,S);
	while (Tempstr)
	{
		GetToken(Tempstr,":",&Token,0);

		if (strcmp(Token,Name) !=0) ListAddItem(Entries,CopyStr(NULL,Tempstr));	
	
		Tempstr=STREAMReadLine(Tempstr,S);
	}
	STREAMClose(S);
}


S=STREAMOpenFile(Settings.AuthPath,SF_WRONLY| SF_CREAT | SF_TRUNC);
if (S)
{
	//First copy all other entries
	Curr=ListGetNext(Entries);
	while (Curr)
	{
		STREAMWriteLine((char *) Curr->Item,S);
		Curr=ListGetNext(Curr);
	}
	STREAMFlush(S);


	if (strcmp(PassType,"delete")==0)
	{
		//Don't bother to write new entry, effectively deleting user
	}
	else
	{
		//WriteNew Entry
		if (strcmp(PassType,"plain")==0)
		{
			Token=CopyStr(Token,Pass);
			Tempstr=MCopyStr(Tempstr,Name,":",PassType,":",Token,":",RealUser,":",HomeDir,":",Args,"\n",NULL);
		}
		else if (strcmp(PassType,"htdigest-md5")==0)
		{
			//Calc 'HA1'
			Tempstr=MCopyStr(Tempstr,Name,":",Settings.AuthRealm,":",Pass,NULL);
			HashBytes(&Token,"md5",Tempstr,StrLen(Tempstr),ENCODE_HEX);
			Tempstr=MCopyStr(Tempstr,Name,":",PassType,":",Token,":",RealUser,":",HomeDir,":",Args,"\n",NULL);
		}
		else
		{
			GenerateRandomBytes(&Salt,24, ENCODE_BASE64);
			Tempstr=MCopyStr(Tempstr,Name,":",Pass,":",Salt,NULL);
			HashBytes(&Token, PassType, Tempstr, StrLen(Tempstr), ENCODE_BASE64);
			Tempstr=MCopyStr(Tempstr,Name,":",PassType,":",Salt,"$",Token,":",RealUser,":",HomeDir,":",Args,"\n",NULL);
		}
	
		STREAMWriteLine(Tempstr,S);

		SwitchUser(RealUser);
		mkdir(HomeDir,0770);
	}

	STREAMClose(S);
	RetVal=ERR_OKAY;
}
else RetVal=ERR_FILE;

DestroyString(Tempstr);
DestroyString(Token);
DestroyString(Salt);

ListDestroy(Entries,DestroyString);

return(RetVal);
}