/** * Clear * * Erases the contents of the log. */ void CLog::Clear(void) { FILE *LogFile; if (m_File != NULL) { fclose(m_File); } if (m_Filename != NULL && (LogFile = fopen(m_Filename, "w")) != NULL) { SetPermissions(m_Filename, S_IRUSR | S_IWUSR); if (!m_KeepOpen) { fclose(LogFile); } else { m_File = LogFile; } } }
XsiSemaphoreSet(int numberOfSemaphores, int flags) : fInitOK(false), fLastSemctlTime((time_t)real_time_clock()), fLastSemopTime(0), fNumberOfSemaphores(numberOfSemaphores), fSemaphores(0) { mutex_init(&fLock, "XsiSemaphoreSet private mutex"); SetIpcKey((key_t)-1); SetPermissions(flags); fSemaphores = new(std::nothrow) XsiSemaphore[numberOfSemaphores]; if (fSemaphores == NULL) { TRACE_ERROR(("XsiSemaphoreSet::XsiSemaphore(): failed to allocate " "XsiSemaphore object\n")); } else fInitOK = true; }
void FileUtil::MakeDirectory (const char* directory) { csString dirAppended(directory); dirAppended.Append("/"); csRef<iDataBuffer> realPath = vfs->GetRealPath(dirAppended); while(!vfs->Exists(dirAppended)) { #ifdef CS_PLATFORM_WIN32 int rc = mkdir(realPath->GetData()); #else int rc = mkdir(realPath->GetData(), S_IRUSR | S_IWUSR); #endif csString dir = directory; csRef<iDataBuffer> real = realPath; while(rc < 0) { dir.Truncate(dir.FindLast('/')); if(vfs->Exists(directory)) { printf("Couldn't create directory '%s'.", directory); return; } real = vfs->GetRealPath(dir + "/"); #ifdef CS_PLATFORM_WIN32 rc = mkdir(real->GetData()); #else rc = mkdir(real->GetData(), S_IRUSR | S_IWUSR); #endif } #ifdef CS_PLATFORM_UNIX dir.Truncate(dir.FindLast('/')); csRef<iDataBuffer> parent = vfs->GetRealPath(dir + "/"); csRef<FileStat> dirStat = StatFile(parent->GetData()); SetPermissions(real->GetData(), dirStat); #endif } }
/** * WriteUnformattedLine * * Writes a new log entry. * * @param Line the log entry */ void CLog::WriteUnformattedLine(const char *Line) { char *Out = NULL, *dupLine; size_t StringLength; unsigned int a; tm Now; char strNow[100]; FILE *LogFile; #ifndef _WIN32 struct stat StatBuf; #endif int rc; if (Line == NULL) { return; } LogFile = m_File; #ifndef _WIN32 if (m_Filename != NULL) { rc = lstat(m_Filename, &StatBuf); if (m_File != NULL && (rc < 0 || StatBuf.st_ino != m_Inode || StatBuf.st_dev != m_Dev)) { fclose(m_File); m_File = NULL; } if (rc == 0) { m_Inode = StatBuf.st_ino; m_Dev = StatBuf.st_dev; } } #endif if (m_Filename == NULL || (m_File == NULL && (LogFile = fopen(m_Filename, "a")) == NULL)) { return; } SetPermissions(m_Filename, S_IRUSR | S_IWUSR); Now = *localtime(&g_CurrentTime); #ifdef _WIN32 strftime(strNow, sizeof(strNow), "%#c" , &Now); #else strftime(strNow, sizeof(strNow), "%a %B %d %Y %H:%M:%S" , &Now); #endif dupLine = strdup(Line); if (AllocFailed(dupLine)) { return; } StringLength = strlen(dupLine); a = 0; for (unsigned int i = 0; i <= StringLength; i++) { if (dupLine[i] == '\r' || dupLine[i] == '\n') { continue; } dupLine[a] = dupLine[i]; a++; } rc = asprintf(&Out, "[%s]: %s\n", strNow, dupLine); free(dupLine); if (rc < 0) { perror("asprintf() failed"); return; } fputs(Out, LogFile); printf("%s", Out); free(Out); if (!m_KeepOpen) { fclose(LogFile); } else { m_File = LogFile; fflush(m_File); } }
/** * SetIdent * * Sets the default ident for the bouncer. * * @param Ident the ident */ void CIdentSupport::SetIdent(const char *Ident) { char *NewIdent; #ifndef _WIN32 passwd *pwd; uid_t uid; char *FilenameTemp, *Filename; int rc; uid = getuid(); pwd = getpwuid(uid); if (pwd == NULL) { g_Bouncer->Log("Could not figure out the UNIX username. Not setting ident."); return; } rc = asprintf(&Filename, "%s/.oidentd.conf", pwd->pw_dir); if (RcFailed(rc)) { return; } rc = asprintf(&FilenameTemp, "%s.tmp", Filename); if (RcFailed(rc)) { free(Filename); return; } FILE *identConfig = fopen(FilenameTemp, "w"); SetPermissions(FilenameTemp, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (identConfig != NULL) { char *Buf = (char *)malloc(strlen(Ident) + 50); snprintf(Buf, strlen(Ident) + 50, "global { reply \"%s\" }", Ident); fputs(Buf, identConfig); free(Buf); fclose(identConfig); } rc = rename(FilenameTemp, Filename); free(Filename); free(FilenameTemp); if (RcFailed(rc)) {} #endif NewIdent = strdup(Ident); if (AllocFailed(NewIdent)) { return; } free(m_Ident); m_Ident = NewIdent; }
bool FileUtil::CopyFile(csString from, csString to, bool vfsPath, bool executable, bool silent) { csString n1; csString n2; csString file = to; csRef<iDataBuffer> buff = vfs->GetRealPath(to); if(vfsPath) { // Make parent dir if needed. csString parent = to; MakeDirectory(parent.Truncate(parent.FindLast('/'))); if(!buff) { if(!silent) printf("Couldn't get the real filename for %s!\n",to.GetData()); return false; } file = buff->GetData(); } // Get current permissions for later. csRef<iDataBuffer> fromBuff = vfs->GetRealPath(from); csRef<FileStat> fromStat = StatFile(fromBuff->GetData()); csRef<FileStat> stat = StatFile(file); if(stat && stat->readonly) { if(!silent) printf("Won't write to %s, because it's readonly\n",file.GetData()); return true; // Return true to bypass DLL checks and stuff } if (!vfsPath) { n1 = "/this/" + from; n2 = "/this/" + to; } else { n1= from; n2= to; } csRef<iDataBuffer> buffer = vfs->ReadFile(n1.GetData(),true); if (!buffer) { if(!silent) printf("Couldn't read file %s!\n",n1.GetData()); return false; } if (!vfs->WriteFile(n2.GetData(), buffer->GetData(), buffer->GetSize() ) ) { if(!silent) printf("Couldn't write to %s!\n", n2.GetData()); return false; } #ifdef CS_PLATFORM_UNIX /** * On unix type systems we might need to set permissions after copy. * If the 'from' stat is null, the file is probably in a zip. * So we use the permissions of the parent folder of the 'to' location. */ if(!fromStat.IsValid()) { n2.Truncate(n2.FindLast('/')+1); csRef<iDataBuffer> db = vfs->GetRealPath(n2); fromStat = StatFile(db->GetData()); } SetPermissions(buff->GetData(), fromStat); if(executable) { if(chmod(buff->GetData(), fromStat->mode | S_IXUSR | S_IXGRP) == -1) printf("Failed to set permissions on file %s.\n", to.GetData()); } #endif return true; }