int HashratCheckSignedFile(char *Path, HashratCtx *Ctx) { STREAM *S; char *Tempstr=NULL, *HashStr=NULL; THash *Hash, *tmpHash; int LineCount=0; S=STREAMOpenFile(Path, SF_RDWR); if (! S) return(FALSE); Hash=HashInit(Ctx->HashType); Tempstr=STREAMReadLine(Tempstr, S); while (Tempstr) { LineCount++; //hashrat-integrity-mark: 2014/10/29 21:05:19 md5:nTnlHmvVowFowmxXtm0uNw== if (strncmp(Tempstr, "hashrat-integrity-mark: ",24)==0) { tmpHash=Hash->Clone(Hash); HashFinish(tmpHash,ENCODE_BASE64,&HashStr); HashratOutputSigningCheck(Ctx, HashStr, Tempstr, LineCount); } Hash->Update(Hash ,Tempstr, StrLen(Tempstr)); Tempstr=STREAMReadLine(Tempstr, S); } DestroyString(Tempstr); DestroyString(HashStr); return(TRUE); }
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); }
int HashFile(char **Return, char *Type, char *Path, int Encoding) { THash *Hash; STREAM *S; char *Tempstr=NULL; int result; S=STREAMOpenFile(Path,O_RDONLY); if (! S) return(FALSE); Hash=HashInit(Type); if (! Hash) { STREAMClose(S); return(FALSE); } Tempstr=SetStrLen(Tempstr,4096); result=STREAMReadBytes(S,Tempstr,4096); while (result !=EOF) { Hash->Update(Hash, Tempstr, result); result=STREAMReadBytes(S,Tempstr,4096); } DestroyString(Tempstr); STREAMClose(S); result=Hash->Finish(Hash, Encoding, Return); HashDestroy(Hash); return(result); }
void HashratSignFile(char *Path, HashratCtx *Ctx) { STREAM *S; char *Tempstr=NULL, *HashStr=NULL; double pos; THash *Hash; S=STREAMOpenFile(Path, SF_RDWR); if (! S) return; Hash=HashInit(Ctx->HashType); HashratFinishHash(&HashStr, Ctx, Hash); pos=STREAMSeek(S,0,SEEK_END); Tempstr=MCopyStr(Tempstr,"hashrat-placeholder---: ",GetDateStr("%Y/%m/%d %H:%M:%S",NULL)," ",Ctx->HashType,":", HashStr,"\n",NULL); STREAMWriteLine(Tempstr,S); STREAMFlush(S); STREAMSeek(S,0,SEEK_SET); Hash=HashInit(Ctx->HashType); HashratHashFile(Ctx, Hash, FT_FILE, Path, (off_t) pos); HashratFinishHash(&HashStr, Ctx, Hash); Tempstr=MCopyStr(Tempstr,"hashrat-integrity-mark: ",GetDateStr("%Y/%m/%d %H:%M:%S",NULL)," ",Ctx->HashType,":", HashStr,"\n",NULL); STREAMSeek(S,pos,SEEK_SET); STREAMWriteLine(Tempstr,S); STREAMFlush(S); DestroyString(Tempstr); DestroyString(HashStr); }
void GetClientHardwareAddress(TSession *Session) { STREAM *S; char *Tempstr=NULL, *Token=NULL, *ptr; S=STREAMOpenFile("/proc/net/arp",O_RDONLY); if (S) { Tempstr=STREAMReadLine(Tempstr,S); Tempstr=STREAMReadLine(Tempstr,S); while (Tempstr) { ptr=GetToken(Tempstr,"\\S",&Token,0); if (strcmp(Token,Session->ClientIP)==0) { //HW Type ptr=GetToken(ptr,"\\S",&Token,0); //Flags ptr=GetToken(ptr,"\\S",&Token,0); //MAC ptr=GetToken(ptr,"\\S",&Session->ClientMAC,0); } Tempstr=STREAMReadLine(Tempstr,S); } STREAMClose(S); } DestroyString(Tempstr); DestroyString(Token); }
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); }
int LogFileAppendTempLog(const char *LogPath, const char *TmpLogPath) { TLogFile *LogFile; char *Tempstr=NULL; STREAM *S; int result=FALSE; LogFile=LogFileGetEntry(LogPath); LogFileClose(TmpLogPath); S=STREAMOpenFile(TmpLogPath,SF_RDONLY); if (LogFile && S) { STREAMLock(LogFile->S,LOCK_EX); Tempstr=STREAMReadLine(Tempstr,S); while(Tempstr) { STREAMWriteLine(Tempstr,LogFile->S); Tempstr=STREAMReadLine(Tempstr,S); } if (LogFile->Flags & LOGFILE_FLUSH) STREAMFlush(LogFile->S); STREAMLock(LogFile->S,LOCK_UN); unlink(TmpLogPath); result=TRUE; } if (S) STREAMClose(S); DestroyString(Tempstr); return(result); }
void HTTPServerRecieveURL(STREAM *S,HTTPSession *Heads) { STREAM *Doc; struct stat FileStat; char *Buffer=NULL, *Tempstr=NULL; int BuffSize=4096; Doc=STREAMOpenFile(Heads->Path, SF_CREAT | SF_TRUNC | SF_WRONLY); if (! Doc) HTTPServerSendHTML(S, Heads, "403 Forbidden","Can't open document for write."); else { fchmod(Doc->in_fd,0660); Buffer=SetStrLen(Buffer,BuffSize); STREAMSendFile(S,Doc,Heads->ContentSize, SENDFILE_KERNEL | SENDFILE_LOOP); STREAMClose(Doc); stat(Heads->Path,&FileStat); LogToFile(Settings.LogPath,"%s@%s (%s) uploaded %s (%d bytes)",Heads->UserName,Heads->ClientHost,Heads->ClientIP,Heads->Path,FileStat.st_size); HTTPServerSendHTML(S, Heads, "201 Created",""); } DestroyString(Tempstr); DestroyString(Buffer); }
void HTTPServerHandleStream(STREAM *Output, HTTPSession *Session, char *SearchPath, int SendData) { char *Tempstr=NULL; HTTPSession *Response; ListNode *Vars; STREAM *S; glob_t Glob; int i; Vars=ListCreate(); SetVar(Vars,"ContentType","audio/mpeg"); Response=FileSendCreateSession("", Session, Vars, 0); HTTPServerSendHeaders(Output, Response, FALSE); STREAMFlush(Output); Tempstr=MCopyStr(Tempstr,SearchPath,"/*",NULL); glob(Tempstr,0,0,&Glob); LogToFile(Settings.LogPath,"Stream from Dir: %s, %d files",SearchPath,Glob.gl_pathc); for (i=0; i < Glob.gl_pathc; i++) { S=STREAMOpenFile(Glob.gl_pathv[i],SF_RDONLY); if (S) { IcecastSendData(S, Output, 4096000); STREAMClose(S); } } globfree(&Glob); DestroyString(Tempstr); ListDestroy(Vars,DestroyString); }
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); }
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); }
int CopyLocalItem(char *From, char *To) { glob_t Glob; struct stat FStat; char *Tempstr=NULL, *ptr; int i, RetVal=EFAULT; STREAM *In=NULL, *Out=NULL; stat(From,&FStat); if (S_ISDIR(FStat.st_mode)) { mkdir(To,FStat.st_mode); Tempstr=MCopyStr(Tempstr, From, "/*", NULL); glob(Tempstr, 0, 0, &Glob); for (i=0; i < Glob.gl_pathc; i++) { ptr=strrchr(Glob.gl_pathv[i],'/'); if (! ptr) ptr=Glob.gl_pathv[i]; Tempstr=MCopyStr(Tempstr, To, ptr, NULL); CopyLocalItem(Glob.gl_pathv[i],Tempstr); } RetVal=0; globfree(&Glob); } else { In=STREAMOpenFile(From,SF_RDONLY); if (In) { Out=STREAMOpenFile(To, SF_CREAT| SF_WRONLY | SF_TRUNC); if (Out) RetVal=STREAMSendFile(In, Out, 0, SENDFILE_KERNEL | SENDFILE_LOOP); } } //as In and Out are NULL if not opened, it's safe to close them //here as STREAMClose will ignore a NULL argument STREAMClose(In); STREAMClose(Out); DestroyString(Tempstr); return(RetVal); }
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); }
void HTTPServerSendFile(STREAM *S, HTTPSession *Session, char *Path, ListNode *Vars, int Flags) { STREAM *Doc; HTTPSession *Response; char *Buffer=NULL, *Tempstr=NULL; int ICYInterval=4096000; Doc=STREAMOpenFile(Path, SF_RDONLY); if (! Doc) HTTPServerSendHTML(S, Session, "403 Forbidden","You don't have permission for that."); else { if (Session) { LogToFile(Settings.LogPath,"%s@%s (%s) downloading %s (%s bytes)",Session->UserName,Session->ClientHost,Session->ClientIP,Path,GetVar(Vars,"FileSize")); } Response=FileSendCreateSession(Path, Session, Vars, ICYInterval); MediaReadDetails(Doc,Vars); HTTPServerFormatExtraHeaders(Response,Vars); HTTPServerSendHeaders(S, Response, Flags); if (Response->Flags & SESSION_ENCODE_GZIP) { STREAMAddStandardDataProcessor(S,"compression","gzip","CompressionLevel=1"); } if (Flags & HEADERS_SENDFILE) { if (Session->Flags & SESSION_ICECAST) IcecastSendData(Doc, S, ICYInterval); else STREAMSendFile(Doc, S, 0, SENDFILE_KERNEL | SENDFILE_LOOP); } /* If HTTPServerSendHeaders set SESSION_REUSE then set that in the Session object if (Response->Flags & SESSION_REUSE) Session->Flags |= SESSION_REUSE; else Session->Flags &= ~SESSION_REUSE; */ STREAMClose(Doc); HTTPSessionDestroy(Response); } DestroyString(Buffer); DestroyString(Tempstr); }
main(int argc, char *argv[]) { STREAM *S, *Out; ListNode *Vars; char *Path=NULL, *Tempstr=NULL, *ptr; int bytes_read, bytes_total, val, result; S=STREAMOpenFile(argv[1],O_RDONLY); Out=STREAMFromFD(1); Vars=ListCreate(); while (TarReadHeader(S, Vars)) { Path=CopyStr(Path,GetVar(Vars,"Path")); printf("PATH: %s %s %s\n",Path,GetVar(Vars,"Size"),GetVar(Vars,"Type")); if (StrLen(Path)) { ptr=GetVar(Vars,"Type"); if (ptr && (strcmp(ptr,"file")==0)) { bytes_read=0; bytes_total=atoi(GetVar(Vars,"Size")); Tempstr=SetStrLen(Tempstr,BUFSIZ); while (bytes_read < bytes_total) { val=bytes_total - bytes_read; if (val > BUFSIZ) val=BUFSIZ; if ((val % 512)==0) result=val; else result=((val / 512) + 1) * 512; result=STREAMReadBytes(S,Tempstr,result); if (result > val) result=val; printf("READ: %d\n",val); fflush(NULL); if (Out) STREAMWriteBytes(Out,Tempstr,result); bytes_read+=result; } } } ListClear(Vars,DestroyString); } ListDestroy(Vars,DestroyString); DestroyString(Tempstr); DestroyString(Path); }
int MultipartReadFile(STREAM *S,char *FName,char *Boundary, int BoundaryLen) { char *Tempstr=NULL, *ptr; int result, RetVal=FALSE; STREAM *FOut=NULL; off_t fsize; FOut=STREAMOpenFile(FName,SF_CREAT | SF_TRUNC | SF_WRONLY); if (FOut) { Tempstr=SetStrLen(Tempstr,4096); result=STREAMReadBytesToTerm(S, Tempstr, 4096, '\n'); while (result > -1) { if ( (result < (BoundaryLen + 6)) && (strncmp(Tempstr,Boundary,BoundaryLen)==0)) { //As we read to a '\n' we may have left its '\r' partner attached to //the end of the data ptr=Tempstr+result-2; if (strcmp(ptr,"\r\n")==0) result-=2; if ((result >= BoundaryLen) && (strncmp(Tempstr+BoundaryLen,"--\r\n",4)==0)) { //must remove '\r\n' from end of file (it's the start of the boundary) RetVal=UPLOAD_DONE; } break; } else if (FOut) STREAMWriteBytes(FOut,Tempstr,result); result=STREAMReadBytesToTerm(S, Tempstr, 4096, '\n'); } //If we read to a boundary then there will always be a \r\n on the end of the file, fsize=(off_t) STREAMTell(FOut); if (fsize > 0) ftruncate(FOut->out_fd,fsize-2); STREAMClose(FOut); } if (result==-1) RetVal=UPLOAD_DONE; DestroyString(Tempstr); return(RetVal); }
char *ReadBannerFile(char *RetStr, char *Path) { char *Tempstr=NULL; STREAM *S; int result; S=STREAMOpenFile(Path, SF_RDONLY); if (! S) return(RetStr); Tempstr=SetStrLen(Tempstr,8196); result=STREAMReadBytes(S, Tempstr, 8196); RetStr=CopyStr(RetStr,Tempstr); Tempstr[result]='\0'; DestroyString(Tempstr); STREAMClose(S); return(RetStr); }
int ReadConfigFile(char *ConfigPath) { STREAM *S; char *Tempstr=NULL; S=STREAMOpenFile(ConfigPath,O_RDONLY); if (! S) return(FALSE); Tempstr=STREAMReadLine(Tempstr,S); while (Tempstr) { StripTrailingWhitespace(Tempstr); ParseConfigItem(Tempstr); Tempstr=STREAMReadLine(Tempstr,S); } STREAMClose(S); return(TRUE); }
void ReadConfigFile(TSettings *Settings) { STREAM *S; char *Tempstr=NULL; S=STREAMOpenFile(Settings->ConfigPath,SF_RDONLY); if (S) { Tempstr=STREAMReadLine(Tempstr,S); while (Tempstr) { StripLeadingWhitespace(Tempstr); StripTrailingWhitespace(Tempstr); if (StrLen(Tempstr)) ParseConfigItem(Tempstr); Tempstr=STREAMReadLine(Tempstr,S); } STREAMClose(S); } DestroyString(Tempstr); }
TLogFile *LogFileGetEntry(const char *FileName) { ListNode *Node; TLogFile *LogFile=NULL; STREAM *S=NULL; if (! StrLen(FileName)) return(NULL); if (! LogFiles) LogFiles=ListCreate(); if (! LogFileDefaults) LogFileSetupDefaults(); Node=ListFindNamedItem(LogFiles,FileName); if (Node) LogFile=(TLogFile *) Node->Item; else { if (strcmp(FileName,"STDOUT")==0) S=STREAMFromFD(1); else if (strcmp(FileName,"STDERR")==0) S=STREAMFromFD(2); else if (strcmp(FileName,"SYSLOG")==0) S=STREAMCreate(); else { S=STREAMOpenFile(FileName,SF_CREAT | SF_APPEND | SF_WRONLY); } if (S) { LogFile=(TLogFile *) calloc(1,sizeof(TLogFile)); LogFile->Path=CopyStr(LogFile->Path,FileName); LogFile->LogFacility=LogFileDefaults->LogFacility; LogFile->Flags=LogFileDefaults->Flags; LogFile->MaxSize=LogFileDefaults->MaxSize; LogFile->S=S; if (strcmp(FileName,"SYSLOG")==0) LogFile->Flags |= LOGFILE_SYSLOG; ListAddNamedItem(LogFiles,FileName,LogFile); STREAMSetItem(S,"TLogFile",LogFile); } } return(LogFile); }
void CommandLineHandleUpdate(int argc, char *argv[], int i, HashratCtx *Ctx) { char *Token=NULL, *ptr; Flags |= FLAG_UPDATE; strcpy(argv[i],""); i++; ptr=GetToken(argv[i],",",&Token,0); while (ptr) { if (strcasecmp(Token, "stderr")==0) Ctx->Aux=STREAMFromFD(2); else if (strcasecmp(Token, "xattr")==0) Ctx->Flags |= CTX_STORE_XATTR; else if (strcasecmp(Token, "txattr")==0) Ctx->Flags |= CTX_STORE_XATTR | CTX_XATTR_ROOT; else if (strcasecmp(Token, "memcached")==0) Ctx->Flags |= CTX_STORE_MEMCACHED; else if (strcasecmp(Token, "mcd")==0) Ctx->Flags |= CTX_STORE_MEMCACHED; else if (! Ctx->Aux) Ctx->Aux=STREAMOpenFile(Token,SF_WRONLY | SF_CREAT | SF_TRUNC); ptr=GetToken(ptr,",",&Token,0); } strcpy(argv[i],""); DestroyString(Token); }
main(int argc, char *argv[]) { int val, len; STREAM *S; char *Tempstr=NULL; clock_t start, mid, end; S=STREAMOpenFile(argv[1],SF_RDONLY); start=clock(); Tempstr=STREAMReadLine(Tempstr,S); while (Tempstr) { len=StrLen(Tempstr); val=mmx_hash_bucket_data(Tempstr, len, 9999); Tempstr=STREAMReadLine(Tempstr,S); } end=clock(); printf("MMX: %d\n",end-start,val,Tempstr); STREAMSeek(S,0,SEEK_SET); start=clock(); Tempstr=STREAMReadLine(Tempstr,S); while (Tempstr) { len=StrLen(Tempstr); val=fnv_string2bucket(Tempstr,len, 9999); Tempstr=STREAMReadLine(Tempstr,S); } end=clock(); printf("FNV: %d\n",end-start,val,Tempstr); STREAMClose(S); }
STREAM *LogFileInternalDoRotate(TLogFile *LogFile) { struct stat FStat; char *Tempstr=NULL, *Path=NULL, *PrevPath=NULL; int i; if (! LogFile) return(NULL); if (strcmp(LogFile->Path,"SYSLOG")==0) return(LogFile->S); if (strcmp(LogFile->Path,"STDOUT")==0) return(LogFile->S); if (strcmp(LogFile->Path,"STDERR")==0) return(LogFile->S); if (getpid() != ParentPID) return(LogFile->S); if (LogFile->MaxSize > 0) { fstat(LogFile->S->out_fd,&FStat); if (FStat.st_size > LogFile->MaxSize) { Tempstr=LogFileInternalGetRotateDestinationPath(Tempstr, LogFile); for (i=LogFile->MaxRotate; i > 0; i--) { Path=FormatStr(Path,"%s.%d",Tempstr,i); if (i==LogFile->MaxRotate) unlink(Path); else rename(Path,PrevPath); PrevPath=CopyStr(PrevPath,Path); } STREAMClose(LogFile->S); if (PrevPath) rename(LogFile->Path,PrevPath); LogFile->S=STREAMOpenFile(LogFile->Path,SF_CREAT | SF_APPEND | SF_WRONLY); } } DestroyString(PrevPath); DestroyString(Tempstr); DestroyString(Path); return(LogFile->S); }
int SendPublicKeyToRemote(STREAM *S, char *KeyFile, char *LocalPath) { char *Tempstr=NULL, *Line=NULL; STREAM *LocalFile; Tempstr=FormatStr(Tempstr,"rm -f %s ; touch %s; chmod 0600 %s\n",KeyFile,KeyFile,KeyFile); STREAMWriteLine(Tempstr,S); LocalFile=STREAMOpenFile(LocalPath,SF_RDONLY); if (LocalFile) { Line=STREAMReadLine(Line,LocalFile); while (Line) { StripTrailingWhitespace(Line); Tempstr=FormatStr(Tempstr,"echo '%s' >> %s\n",Line,KeyFile); STREAMWriteLine(Tempstr,S); Line=STREAMReadLine(Line,LocalFile); } STREAMClose(LocalFile); } return(TRUE); }
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); }
int CopyURL(HTTPSession *Session, char *From, char *To) { char *Tempstr=NULL, *Host=NULL, *PortStr=NULL, *FromPath=NULL, *ToPath=NULL, *User=NULL, *Password=NULL; int RetVal=EFAULT; STREAM *In=NULL, *Out=NULL; ParseURL(To, &Tempstr, &Host, &Tempstr, NULL, NULL, &ToPath, NULL); if ((access(ToPath,F_OK)==0) && (! (Session->Flags & SESSION_OVERWRITE))) RetVal=EEXIST; //If the TO Host is local if (IsLocalHost(Session,Host)) { ParseURL(From, &Tempstr, &Host, &PortStr, &User, &Password, &FromPath, NULL); if (! IsLocalHost(Session,Host)) { In=HTTPGet(From,User,Password); if (In) Out=STREAMOpenFile(ToPath,SF_CREAT|SF_WRONLY|SF_TRUNC); if (Out) RetVal=STREAMSendFile(In, Out, 0, SENDFILE_KERNEL | SENDFILE_LOOP); STREAMClose(In); STREAMClose(Out); } else RetVal=CopyLocalItem(FromPath, ToPath); } DestroyString(User); DestroyString(Password); DestroyString(Tempstr); DestroyString(Host); DestroyString(PortStr); DestroyString(FromPath); DestroyString(ToPath); return(RetVal); }
int CheckScriptIntegrity(char *ScriptPath) { STREAM *S; char *Tempstr=NULL, *Token=NULL, *Hash=NULL, *FileHash=NULL, *ptr; int result, len; if (! ScriptPath) return(FALSE); if (! (Settings.Flags & FLAG_CHECK_SCRIPTS)) return(TRUE); S=STREAMOpenFile(Settings.ScriptHashFile,SF_RDONLY); if (S) { Tempstr=STREAMReadLine(Tempstr,S); while (Tempstr) { StripTrailingWhitespace(Tempstr); ptr=GetToken(Tempstr," ",&Token,0); while (ptr && isspace(*ptr)) ptr++; if (ptr && (strcmp(ptr,ScriptPath)==0)) { Hash=CopyStr(Hash,Token); len=StrLen(Hash); switch (len) { case 32: //MD5 HashFile(&FileHash, "md5", ScriptPath, ENCODE_HEX); break; case 40: //SHA1 HashFile(&FileHash, "sha1", ScriptPath, ENCODE_HEX); break; case 64: //SHA256 HashFile(&FileHash, "sha256", ScriptPath, ENCODE_HEX); break; case 128: //SHA512 HashFile(&FileHash, "sha512", ScriptPath, ENCODE_HEX); break; } } Tempstr=STREAMReadLine(Tempstr,S); } STREAMClose(S); } if (StrLen(FileHash) && StrLen(Hash) && (strcmp(FileHash,Hash)==0) ) result=TRUE; else { LogToFile(Settings.LogPath,"ERROR: Not running script '%s'. Script failed integrity check.",ScriptPath); result=FALSE; } DestroyString(FileHash); DestroyString(Tempstr); DestroyString(Token); DestroyString(Hash); return(result); }
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); }
int OSSPlaySoundFile(char *FilePath, int Vol) { STREAM *S; char *Tempstr=NULL; int result, fd=-1, mixfd=-1; int Flags, val, oldvol; char FourCharacter[5]; TAudioInfo *AudioInfo=NULL; S=STREAMOpenFile(FilePath,O_RDONLY); if (! S) return(FALSE); STREAMReadBytes(S,FourCharacter,4); FourCharacter[4]='\0'; if (strcmp(FourCharacter,".snd")==0) AudioInfo=SoundReadAU(S); else if (strcmp(FourCharacter,"RIFF")==0) AudioInfo=SoundReadWAV(S); if (AudioInfo==NULL) { STREAMClose(S); return(FALSE); } //Must do all the above before we do open oss! fd=OpenOSSOutput("/dev/dsp", AudioInfo); if (fd==-1) { STREAMClose(S); return(FALSE); } if (Vol != VOLUME_LEAVEALONE) { mixfd=open("/dev/dsp", O_RDWR); if (mixfd > -1) { ioctl(mixfd,SOUND_MIXER_READ_PCM,&oldvol); if (Vol > 255) result=255; else result=Vol; val=(result) | (result <<8); ioctl(mixfd,SOUND_MIXER_WRITE_PCM,&val); } } Tempstr=SetStrLen(Tempstr,1024); result=STREAMReadBytes(S,Tempstr,1024); val=0; while ((result > 0) && (val < AudioInfo->DataSize)) { write(fd,Tempstr,result); val+=result; result=STREAMReadBytes(S,Tempstr,1024); } close(fd); if (mixfd > -1) { ioctl(mixfd,SOUND_MIXER_WRITE_PCM,&oldvol); close(mixfd); } STREAMClose(S); return(TRUE); }
int AuthNativeFile(TSession *Session) { STREAM *S; char *Tempstr=NULL,*ptr; char *Name=NULL, *Pass=NULL, *Salt=NULL, *RealUser=NULL, *HomeDir=NULL, *PasswordType=NULL; int RetVal=USER_UNKNOWN; struct passwd *pass_struct; AuthenticationsTried=CatStr(AuthenticationsTried,"native "); if (! StrLen(Settings.AuthFile)) return(FALSE); S=STREAMOpenFile(Settings.AuthFile,O_RDONLY); if (! S) { if (Settings.Flags & FLAG_LOG_VERBOSE) LogToFile(Settings.ServerLogPath,"Cannot open %s",Settings.AuthFile); return(USER_UNKNOWN); } Tempstr=STREAMReadLine(Tempstr,S); while (Tempstr) { StripTrailingWhitespace(Tempstr); ptr=GetToken(Tempstr,":",&Name,0); ptr=GetToken(ptr,":",&PasswordType,0); ptr=GetToken(ptr,":",&Salt,0); ptr=GetToken(ptr,":",&Pass,0); ptr=GetToken(ptr,":",&RealUser,0); ptr=GetToken(ptr,":",&HomeDir,0); if (strcasecmp(Name,Session->User)==0) { RetVal=FALSE; if (CheckNativeFilePassword(PasswordType,Name,Salt,Pass,Session->Passwd,Session)) { RetVal=TRUE; Session->RealUser=CopyStr(Session->RealUser,RealUser); if (StrLen(HomeDir)) Session->HomeDir=CopyStr(Session->HomeDir,HomeDir); Session->UserSettings=CopyStr(Session->UserSettings,ptr); LogToFile(Settings.ServerLogPath,"AUTH OK %s [%s] [%s]",Name,Session->HomeDir,Session->RealUser); if (StrLen(Settings.UpdatePasswordType) && (strcasecmp(Settings.UpdatePasswordType, PasswordType) !=0) && (strcasecmp(PasswordType, "challenge") !=0)) { UpdateNativeFile(S->Path, Name, Settings.UpdatePasswordType, Session->Passwd, HomeDir, RealUser, ptr); } } break; } Tempstr=STREAMReadLine(Tempstr,S); } STREAMClose(S); DestroyString(Tempstr); DestroyString(Name); DestroyString(Pass); DestroyString(Salt); DestroyString(RealUser); DestroyString(HomeDir); DestroyString(PasswordType); return(RetVal); }