int parseConfigFile(AtomPtr filename) { char buf[512]; int lineno; FILE *f; if(!filename || filename->length == 0) return 0; f = fopen(filename->string, "r"); if(f == NULL) { do_log_error(L_ERROR, errno, "Couldn't open config file %s", filename->string); return -1; } lineno = 1; while(1) { char *s; s = fgets(buf, 512, f); if(s == NULL) { fclose(f); return 1; } parseConfigLine(buf, filename->string, lineno, 0); lineno++; } }
bool MaItem::load(const QString& filename) { bool res = false; if (!filename.isEmpty()) mFilename = filename; mRelPath.clear(); mRelPath = relPath(); #ifdef _DEBUG // qDebug() << absPath(); #endif // _DEBUG QFile file(absPath()); if (file.open(QIODevice::ReadOnly)) { QTextStream in(&file); in.setCodec("UTF-8"); parseConfigLine(in.readLine()); if (mIsEncrypted) { QByteArray s = in.readAll().toAscii(); _engine->encrypt(s); QString s2(s); int i = s2.indexOf("\n"); if (i > -1) { mCaption = s2.left(i); mNotes = s2.right(s2.length() - i - 1); } } else { mCaption = in.readLine(); mNotes = in.readAll(); } file.close(); res = true; } // Loading the password from the password file: QFile file2(absPathToFilename(kPathwordFilename)); if (file.exists() && file2.open(QIODevice::ReadOnly)) { QTextStream in(&file2); in.setCodec("UTF-8"); QByteArray ba = in.readAll().toAscii(); QByteArray salt(QString(_engine->passwordEncryptionSalt()).toAscii()); _engine->encrypt(ba, _engine->passwordEncryptionKey().toAscii(), salt); mPassword = ba; mPasswordDirty = false; } return res; }
static void parseConfigFile(int fd, struct s_initconfig *cs) { char line[CONFPARSER_LINEBUF_SIZE+1]; char c; int linepos = 0; int linectr = 0; int waiteol = 0; int rc; int readlen; do { readlen = read(fd,&c,1); if(!(readlen > 0)) { c = '\n'; } if(c == '\n') { linectr++; while(linepos > 0) { if(isWhitespaceChar(line[linepos-1])) { linepos--; } else { break; } } line[linepos] = '\0'; rc = parseConfigLine(line,linepos,cs); if(rc < 0) { printf("error: config file parse error at line %d!\n", linectr); throwError(NULL); } if(rc == 0) break; linepos = 0; waiteol = 0; } else { if((!waiteol) && (!(linepos == 0 && isWhitespaceChar(c)))) { if(parseConfigIsEOLChar(c)) { line[linepos] = '\0'; waiteol = 1; } else { if(linepos < (CONFPARSER_LINEBUF_SIZE)) { line[linepos] = c; linepos++; } else { line[linepos] = '\0'; waiteol = 1; } } } } } while(readlen > 0); }
bool_t loadConfigFile(void) { char buffer[BUFSIZ]; FILE *cfgfile; bool_t retval = TRUE; if ((cfgfile = fopen(CONF_FILENAME, "r")) == NULL) { syslog(LOG_ERR, gettext( "could not open config file \"%s\"\n"), CONF_FILENAME); syslog(LOG_ERR, gettext( "using default options \"%s\"\n"), DEFAULT_CONFIG); retval = parseConfigLine(DEFAULT_CONFIG); } else { (void) memset(buffer, 0, sizeof (buffer)); while ((fgets(buffer, BUFSIZ, cfgfile) != NULL) && (retval == TRUE)) retval = parseConfigLine(buffer); fclose(cfgfile); } return (retval); }
static bool parseConfigFromString(std::string content, std::map<std::string, LoggerInfo> & outInfo) { std::string key; int curLine = 1; std::string line; std::string::size_type curPos = 0; if (content.empty()) { return true; } do { std::string::size_type pos = std::string::npos; for (std::string::size_type i = curPos; i < content.length(); ++i) { //support linux/unix/windows LRCF if (content[i] == '\r' || content[i] == '\n') { pos = i; break; } } line = content.substr(curPos, pos - curPos); parseConfigLine(line, curLine, key, outInfo); curLine++; if (pos == std::string::npos) { break; } else { curPos = pos+1; } } while (1); return true; }
int main(int argc, char **argv) { FdEventHandlerPtr listener; int i; int rc; int expire = 0, printConfig = 0; initAtoms(); CONFIG_VARIABLE(daemonise, CONFIG_BOOLEAN, "Run as a daemon"); CONFIG_VARIABLE(pidFile, CONFIG_ATOM, "File with pid of running daemon."); preinitChunks(); preinitLog(); preinitObject(); preinitIo(); preinitDns(); preinitServer(); preinitHttp(); preinitDiskcache(); preinitLocal(); preinitForbidden(); preinitSocks(); preinitOffline(); i = 1; while(i < argc) { if(argv[i][0] != '-') break; if(strcmp(argv[i], "--") == 0) { i++; break; } else if(strcmp(argv[i], "-h") == 0) { usage(argv[0]); exit(0); } else if(strcmp(argv[i], "-v") == 0) { printConfig = 1; i++; } else if(strcmp(argv[i], "-x") == 0) { expire = 1; i++; } else if(strcmp(argv[i], "-c") == 0) { i++; if(i >= argc) { usage(argv[0]); exit(1); } if(configFile) releaseAtom(configFile); configFile = internAtom(argv[i]); i++; } else { usage(argv[0]); exit(1); } } if(configFile) configFile = expandTilde(configFile); if(configFile == NULL) { configFile = expandTilde(internAtom("~/.polipo")); if(configFile) if(access(configFile->string, F_OK) < 0) { releaseAtom(configFile); configFile = NULL; } } if(configFile == NULL) { if(access("/etc/polipo/config", F_OK) >= 0) configFile = internAtom("/etc/polipo/config"); if(configFile && access(configFile->string, F_OK) < 0) { releaseAtom(configFile); configFile = NULL; } } rc = parseConfigFile(configFile); if(rc < 0) exit(1); while(i < argc) { rc = parseConfigLine(argv[i], "command line", 0, 0); if(rc < 0) exit(1); i++; } initChunks(); initLog(); initObject(); if(!expire && !printConfig) initEvents(); initIo(); initDns(); initHttp(); initServer(); initDiskcache(); initForbidden(); initSocks(); initOffline(); if(printConfig) { printConfigVariables(stdout, 0); exit(0); } if(expire) { expireDiskObjects(); exit(0); } if(daemonise) do_daemonise(logFile == NULL || logFile->length == 0); if(pidFile) writePid(pidFile->string); listener = create_listener(proxyAddress->string, proxyPort, httpAccept, NULL); if(!listener) { if(pidFile) unlink(pidFile->string); exit(1); } eventLoop(); if(pidFile) unlink(pidFile->string); return 0; }
int httpSpecialDoSideFinish(AtomPtr data, HTTPRequestPtr requestor) { ObjectPtr object = requestor->object; if (matchUrl("/s_server/config", object)) { AtomListPtr list = NULL; int i, rc; if (disableConfiguration) { abortObject(object, 403, internAtom("Action not allowed")); goto out; } list = urlDecode(data->string, data->length); if (list == NULL) { abortObject(object, 400, internAtom ("Couldn't parse variable to set")); goto out; } for (i = 0; i < list->length; i++) { rc = parseConfigLine(list->list[i]->string, NULL, 0, 1); if (rc < 0) { abortObject(object, 400, rc == -1 ? internAtom ("Couldn't parse variable to set") : internAtom ("Variable is not settable")); destroyAtomList(list); goto out; } } destroyAtomList(list); object->date = current_time.tv_sec; object->age = current_time.tv_sec; object->headers = internAtom("\r\nLocation: /s_server/config?"); object->code = 303; object->message = internAtom("Done"); object->flags &= ~OBJECT_INITIAL; object->length = 0; } else if (matchUrl("/s_server/status", object)) { AtomListPtr list = NULL; int i; if (disableConfiguration) { abortObject(object, 403, internAtom("Action not allowed")); goto out; } list = urlDecode(data->string, data->length); if (list == NULL) { abortObject(object, 400, internAtom("Couldn't parse action")); goto out; } for (i = 0; i < list->length; i++) { char *equals = memchr(list->list[i]->string, '=', list->list[i]->length); AtomPtr name = equals ? internAtomN(list->list[i]->string, equals - list-> list[i]->string) : retainAtom(list->list[i]); if (name == atomInitForbidden) initForbidden(); else if (name == atomReopenLog) reopenLog(); else if (name == atomDiscardObjects) discardObjects(1, 0); else if (name == atomWriteoutObjects) writeoutObjects(1); else if (name == atomFreeChunkArenas) free_chunk_arenas(); else { abortObject(object, 400, internAtomF("Unknown action %s", name->string)); releaseAtom(name); destroyAtomList(list); goto out; } releaseAtom(name); } destroyAtomList(list); object->date = current_time.tv_sec; object->age = current_time.tv_sec; object->headers = internAtom("\r\nLocation: /s_server/status?"); object->code = 303; object->message = internAtom("Done"); object->flags &= ~OBJECT_INITIAL; object->length = 0; } else { abortObject(object, 405, internAtom("Method not allowed")); } out: releaseAtom(data); notifyObject(object); requestor->connection->flags &= ~CONN_READER; return 1; }
int psiphonMain( int bindAll, int proxyPortParam, int localParentProxyPortParam) { FdEventHandlerPtr listener; initAtoms(); CONFIG_VARIABLE(daemonise, CONFIG_BOOLEAN, "Run as a daemon"); CONFIG_VARIABLE(pidFile, CONFIG_ATOM, "File with pid of running daemon."); preinitChunks(); preinitLog(); preinitObject(); preinitIo(); preinitDns(); preinitServer(); preinitHttp(); preinitDiskcache(); preinitLocal(); preinitForbidden(); preinitSocks(); const int MAX_SIZE = 80; char proxyAddressParamLine[MAX_SIZE]; char proxyPortParamLine[MAX_SIZE]; char localParentProxyPortParamLine[MAX_SIZE]; snprintf( proxyAddressParamLine, MAX_SIZE, (0 == bindAll) ? "proxyAddress=127.0.0.1" : "proxyAddress=0.0.0.0"); snprintf( proxyPortParamLine, MAX_SIZE, "proxyPort=%d", proxyPortParam); snprintf( localParentProxyPortParamLine, MAX_SIZE, "socksParentProxy=127.0.0.1:%d", localParentProxyPortParam); if (0 > parseConfigLine(proxyAddressParamLine, "psiphon", 0, 0) || 0 > parseConfigLine(proxyPortParamLine, "psiphon", 0, 0) || 0 > parseConfigLine(localParentProxyPortParamLine, "psiphon", 0, 0) || 0 > parseConfigLine("disableLocalInterface=true", "psiphon", 0, 0) || 0 > parseConfigLine("logLevel=1", "psiphon", 0, 0) /* Allow HTTPS on all ports */ || 0 > parseConfigLine("tunnelAllowedPorts=1-65535", "psiphon", 0, 0)) { return -1; } initChunks(); initLog(); initObject(); initEvents(); initIo(); initDns(); initHttp(); initServer(); initDiskcache(); initForbidden(); initSocks(); listener = create_listener( proxyAddress->string, proxyPort, httpAccept, NULL); if (!listener) { return -1; } eventLoop(); return 0; }
void ConfigFile::load(SeekableReadStream &stream) { UString comment; ConfigDomain *domain = 0; int lineNumber = 0; int domainLineNumber = 0; while (!stream.eos()) { lineNumber++; // Read a line UString line = readStringLine(stream, kEncodingUTF8); // Parse it UString domainName; UString key, value, lineComment; parseConfigLine(line, domainName, key, value, lineComment, lineNumber); if (!domainName.empty()) { // New domain // Finish up the old domain addDomain(domain, domainLineNumber); // Check that the name is actually valid if (!isValidName(domainName)) throw Exception("\"%s\" isn't a valid domain name (line %d)", domainName.c_str(), lineNumber); // Create the new domain domain = new ConfigDomain(domainName); domain->_prologue = comment; domain->_comment = lineComment; comment.clear(); lineComment.clear(); domainLineNumber = lineNumber; } if (!key.empty()) { // New key if (!domain) throw Exception("Found a key outside a domain (line %d)", lineNumber); if (!isValidName(key)) throw Exception("\"%s\" isn't a valid key name (line %d)", key.c_str(), lineNumber); // Add collected comments to the domain if (!comment.empty()) addDomainKey(*domain, "", "", comment, lineNumber); // Add the key to the domain addDomainKey(*domain, key, value, lineComment, lineNumber); comment.clear(); lineComment.clear(); } // Collect comments, we don't yet know where those belong to. if (!lineComment.empty()) { if (!comment.empty()) comment += '\n'; comment += lineComment; } // Empty line, associate collected comments with the current domain if (domainName.empty() && key.empty() && value.empty() && lineComment.empty()) { if (!comment.empty() && !stream.eos()) { if (!domain) { // We have no domain yet, add it to the file's prologue if (!_prologue.empty()) _prologue += '\n'; _prologue += comment; } else addDomainKey(*domain, "", "", comment, lineNumber); comment.clear(); } } } // Finish up the last domain addDomain(domain, domainLineNumber); // We still have comments, those apparently belong to the bottom of the file if (!comment.empty()) _epilogue = comment; }
int main(int argc, char* argv[]) { int i; FILE* pFile; size_t iLineSize; bool bInitSuccess; int iCurrLine; struct sigaction sSigAct; memset(&sSigAct,0,sizeof(struct sigaction)); sSigAct.sa_handler = inrptHandler; if ( 0 != sigaction(SIGINT, &sSigAct,NULL)) { DEBUG_PRINT("Signal Handler Registration Failed\n"); return 0; } bInitSuccess = false; iCurrLine = 0,i = 0,pFile = NULL; iLineSize = MAX_CONFIG_LINE; memset(m_apArgs,0,sizeof(void*)*MAX_NUMBER_STREAMS); memset(m_apStreamContext,0,sizeof(void*)*MAX_NUMBER_STREAMS); pFile = openFile(CONFIG_FILE_NAME,"r"); if (pFile) { char* pcLine = malloc(iLineSize); bInitSuccess = true; while((iCurrLine<MAX_NUMBER_STREAMS) && (getline((char**)&pcLine,&iLineSize,pFile) != -1)) { m_apArgs[iCurrLine] = (ThreadArgs*)malloc(sizeof(ThreadArgs)); memset(m_apArgs[iCurrLine],0,sizeof(ThreadArgs)); m_apStreamContext[iCurrLine] = (StreamContext*)malloc(sizeof(StreamContext)); memset(m_apStreamContext[iCurrLine],0,sizeof(StreamContext)); parseConfigLine(pcLine,m_apArgs[iCurrLine]); /*assign cmd args to thread context*/ m_apStreamContext[iCurrLine]->psArgs = m_apArgs[iCurrLine]; printArg(m_apArgs[iCurrLine]); /*get MPEG2TS data from file*/ if (!getStreamInfo(m_apArgs[iCurrLine]->acFileName,&(m_apStreamContext[iCurrLine]->iBitrate), &(m_apStreamContext[iCurrLine]->dDuration), &(m_apStreamContext[iCurrLine]->dAvgPeriod))) { DEBUG_PRINT("Failed to retriev MPEG2TS data for %s\n",m_apArgs[iCurrLine]->acFileName); bInitSuccess = false; break; } /*create queue names*/ memset(m_apStreamContext[iCurrLine]->acStreamQueueName,0,MAX_QUEUE_NAME); sprintf(m_apStreamContext[iCurrLine]->acStreamQueueName,"/StreamingQueue%d",iCurrLine); memset(m_apStreamContext[iCurrLine]->acEmptyQueueName,0,MAX_QUEUE_NAME); sprintf(m_apStreamContext[iCurrLine]->acEmptyQueueName,"/EmptyQueue%d",iCurrLine); if (createQueue(m_apStreamContext[iCurrLine]->acStreamQueueName,sizeof(AppMsg),MAX_DATA_MSG + 1,&(m_apStreamContext[iCurrLine]->iStreamQid)) < 0) { DEBUG_PRINT("Failed to create %s queue\n",m_apStreamContext[iCurrLine]->acStreamQueueName); bInitSuccess = false; break; } if (createQueue(m_apStreamContext[iCurrLine]->acEmptyQueueName,sizeof(AppMsg),MAX_DATA_MSG + 1,&(m_apStreamContext[iCurrLine]->iEmptyQid)) < 0) { DEBUG_PRINT("Failed to create %s queue\n",m_apStreamContext[iCurrLine]->acEmptyQueueName); bInitSuccess = false; break; } iCurrLine+=1; memset(pcLine,0,iLineSize); } free(pcLine); } closeFile(pFile); if (!m_apArgs[0]) { DEBUG_PRINT("No configuration found\n"); return 0; } if (!bInitSuccess) { DEBUG_PRINT("The Application will exit\n"); cleanUp(); return 0; } while (m_apStreamContext[i]!=NULL) { populateFileReaderEmptyQueue(m_apStreamContext[i]->iEmptyQid); startFileReader(m_apStreamContext[i]); startStreamer(m_apStreamContext[i]); i+=1; } i=0; /*join threads here until the end*/ while (m_apStreamContext[i]!=NULL) { joinPosixThread(m_apStreamContext[i]->lStreamerId,NULL); joinPosixThread(m_apStreamContext[i]->lReaderId,NULL); i+=1; } cleanUp(); return 0; }
void parseConfigFile(int fd, struct s_initconfig *cs) { char line[CONFPARSER_LINEBUF_SIZE+1]; char c; int linepos = 0; int linectr = 0; int waiteol = 0; int rc; int readlen; strcpy(cs->tapname,""); strcpy(cs->ifconfig4,""); strcpy(cs->ifconfig6,""); strcpy(cs->upcmd,""); strcpy(cs->sourceip,""); strcpy(cs->sourceport,""); strcpy(cs->userstr,""); strcpy(cs->groupstr,""); strcpy(cs->chrootstr,""); strcpy(cs->networkname,"PEERVPN"); strcpy(cs->engines,""); strcpy(cs->pidfile, ""); strcpy(cs->privatekey, "/var/run/peervpn.pem"); cs->password_len = 0; cs->enablepidfile = 0; cs->enableeth = 1; cs->enablendpcache = 0; cs->enablevirtserv = 0; cs->enablerelay = 0; cs->enableindirect = 0; cs->enableconsole = 0; cs->enableseccomp = 0; cs->forceseccomp = 0; cs->daemonize = 0; cs->enableprivdrop = 1; cs->enableipv4 = 1; cs->enableipv6 = 1; cs->enablenat64clat = 0; cs->enablesyslog = 0; cs->sockmark = 0; cs->enablepidfile = 0; cs->initpeerscount = 0; do { readlen = read(fd,&c,1); if(!(readlen > 0)) { c = '\n'; } if(c == '\n') { linectr++; while(linepos > 0) { if(isWhitespaceChar(line[linepos-1])) { linepos--; } else { break; } } line[linepos] = '\0'; rc = parseConfigLine(line,linepos,cs); if(rc < 0) { printf("error: config file parse error at line %d!\n", linectr); throwError(NULL); } if(rc == 0) break; linepos = 0; waiteol = 0; } else { if((!waiteol) && (!(linepos == 0 && isWhitespaceChar(c)))) { if(parseConfigIsEOLChar(c)) { line[linepos] = '\0'; waiteol = 1; } else { if(linepos < (CONFPARSER_LINEBUF_SIZE)) { line[linepos] = c; linepos++; } else { line[linepos] = '\0'; waiteol = 1; } } } } } while(readlen > 0); }