📄 read_cfgd.c
字号:
/*************************************************************************** read_cfg.c - description ------------------- begin : Tue May 15 10:30:03 EET 2001 copyright : (C) 2001-2002 by Petri Turunen email : petri.turunen@pete.fi.eu.org ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <stdio.h>#include <string.h>#include <stdlib.h>#include "respmd.h"//Private functionschar *parseword(char *ptr2,cfgKeywordValue word_type);//Global variablesint processstat = 0;int one_time=0;extern unsigned char CRYPTIV[];extern char CRYPTKEY[]; //defined in linux_mon.cextern char SERVER_IP[]; //defined in servpd.cextern char SERVER_PORT[]; //defined in servpd.cextern char LOCALHOSTN[]; //defined in servpd.cextern char PIDDIR[];int read_confd(char *filename, struct smtps_t *serverpointer){FILE *fp;struct smtps_t *spointer;char *ptr="";char *word="";char SECTION[53]="\0";char PARAMETER[53]="\0";char buf[128];int len,tmp=0;int process_total=0;fp=fopen(filename,"r");if(fp==NULL){slog(2,"read_cfg.c: Error while opening config file: %s",filename);return -1;}while(fgets(buf,128,fp)!=NULL){word = rm_first_spaces(buf);if(is_comment(word)){if(*word=='<'){if((ptr = parseword(word,CFG_SECTION)) == NULL){ slog(2,"Error: NULL section value.\n"); free(ptr); fclose(fp); return -1;}strncpy(SECTION,ptr,50);if(ptr!=NULL) free(ptr);}//We have found some section//Lets see if we can use itif(processstat==2 && *SECTION!='<') //are we realy in section{ if(strcmp(SECTION,"GENERAL")==0)//lets parse GENERAL section { word = rm_first_spaces(word); if((ptr = parseword(word,CFG_PARAMETER)) == NULL) { slog(2,"Error: NULL parameter value.\n"); slog(2,"Error: Check config file.\n"); free(ptr); fclose(fp); return(-1); } strncpy(PARAMETER,ptr,51); free(ptr); word = rm_first_spaces(word); if((ptr = parseword(word,CFG_VALUE)) == NULL) { slog(2,"Error: NULL parameter value.\n"); slog(2,"Error: Check config file.\n"); fclose(fp); return(-1); } //printf("VALUE: %s\n",ptr); //DEBUG if(strcmp(PARAMETER,"SERVERIP")==0) { len=strlen(ptr); *(ptr+len)='\0'; if(len>29) len=29; strncpy(SERVER_IP,ptr,len); } if(strcmp(PARAMETER,"LOCALHOST")==0) { len=strlen(ptr); *(ptr+len)='\0'; if(len>20) len=20; strncpy(LOCALHOSTN,ptr,len); } if(strcmp(PARAMETER,"SERVERPORT")==0) { if(ptr!=NULL) { len=strlen(ptr); if(len>6) len=6; strncpy(SERVER_PORT,ptr,len); } else { strcpy(SERVER_PORT,"8881"); } } if(strcmp(PARAMETER,"PIDDIR")==0) { if(ptr!=NULL) { len=strlen(ptr); if(len>20) len=20; strncpy(PIDDIR,ptr,len); } else { strcpy(PIDDIR,"/var/run"); } } if(strcmp(PARAMETER,"CRYPTKEY")==0) { if(ptr!=NULL) { len=strlen(ptr); if(len>16) len=16; strncpy(CRYPTKEY,ptr,len); } else { slog(1,"Error no CRYPTKEY defined!"); strcpy(CRYPTKEY,"NO KEY PROVIDED"); } } if(strcmp(PARAMETER,"CRYPTIV")==0) { if(ptr!=NULL) { len=strlen(ptr); if(len>8) len=8; strncpy(CRYPTIV,ptr,len); } else { slog(1,"Error no CRYPTIV defined!"); strcpy(CRYPTIV,"NO IV"); } } }//Ends GENERAL section reading if(strcmp(SECTION,"SERVPOLL")==0)//lets parse SMTPSERV section { word = rm_first_spaces(word); if((ptr = parseword(word,CFG_PARAMETER)) == NULL) { slog(2,"Error: NULL parameter value.\n"); slog(2,"Error: Check config file.\n"); free(ptr); fclose(fp); return(-1); } strncpy(PARAMETER,ptr,51); free(ptr); word = rm_first_spaces(word); if((ptr = parseword(word,CFG_VALUE)) == NULL) { slog(2,"Error: NULL parameter value.\n"); slog(2,"Error: Check config file.\n"); fclose(fp); return(-1); } //printf("VALUE: %s\n",ptr); //DEBUG if(one_time==0) { spointer=(struct smtps_t *) calloc(1,sizeof(struct smtps_t)); if(spointer == NULL) { slog(2,"read_cfgd.c: Error Out of memory."); return(-1); } else { spointer->next = serverpointer->next; serverpointer->next = spointer; spointer->next=NULL; serverpointer = spointer; } one_time=1; } if(strcmp(PARAMETER,"SERVER")==0) { len=strlen(ptr); *(ptr+len)='\0'; if(len>60) len=60; strncpy(serverpointer->name,ptr,len); serverpointer->up=1; serverpointer->down=0; serverpointer->url[0]='\0'; serverpointer->port[0]='\0'; serverpointer->proxy[0]='\0'; serverpointer->proxy_port[0]='\0'; } if(strcmp(PARAMETER,"SERVICE")==0) { len=strlen(ptr); *(ptr+len)='\0'; if(len>20) len=20; strncpy(serverpointer->serv,ptr,len); } if(strcmp(PARAMETER,"POLLTIME")==0) { if(ptr!=NULL) { tmp=atoi(ptr); if (tmp!=0) serverpointer->polltime=tmp; } else { serverpointer->polltime=300; //default is 300 sec(5 mins) } } if(strcmp(PARAMETER,"URL")==0) { len=strlen(ptr); *(ptr+len)='\0'; if(len>50) len=50; strncpy(serverpointer->url,ptr,len); } if(strcmp(PARAMETER,"PORT")==0) { len=strlen(ptr); *(ptr+len)='\0'; if(len>6) len=6; strncpy(serverpointer->port,ptr,len); } if(strcmp(PARAMETER,"PROXY")==0) { len=strlen(ptr); *(ptr+len)='\0'; if(len>30) len=30; strncpy(serverpointer->proxy,ptr,len); } if(strcmp(PARAMETER,"PROXYPORT")==0) { len=strlen(ptr); *(ptr+len)='\0'; if(len>6) len=6; strncpy(serverpointer->proxy_port,ptr,len); } } //Ends SMTPSERV section reading}//Section processing ends}//is_comment endsif(processstat==1) //go to section processing processstat++;}//while endsfclose(fp);return process_total;}//main endschar *parseword(char *ptr2,cfgKeywordValue word_type){int len=0;char *word2; if(*(ptr2+len)=='<') { ptr2++; if (*(ptr2+len)=='/') { processstat = 0; //section close one_time=0; } else { processstat = 1; //section open } } for(;;) { if (*(ptr2+len)==' ' || *(ptr2+len)=='\t' || *(ptr2+len)=='\0' || *(ptr2+len)=='#' || (*(ptr2+len)=='=' && word_type==CFG_PARAMETER) || (*(ptr2+len)=='>' && word_type==CFG_SECTION) || (*(ptr2+len)=='=' && word_type==CFG_VALUE)) { break; } len++; }//for ends ptr2=rm_first_spaces(ptr2); if((word2=malloc(len+1))==NULL){ slog(2,"read_cfg.c: Error out of memory.\n"); return NULL; } strncpy(word2,ptr2,len); *(word2+len)='\0'; ptr2+=len; ptr2=rm_first_spaces(ptr2); switch(word_type){ case CFG_PARAMETER: ptr2=rm_first_spaces(ptr2); if(*ptr2=='='){ return word2; } break; case CFG_VALUE: ptr2=rm_first_spaces(ptr2); if(*ptr2!='\0' && *ptr2!='#'){ if(*ptr2=='='){ ptr2++; ptr2=rm_first_spaces(ptr2); len = strlen(ptr2); *(ptr2+len-1)='\0'; ptr2=rm_first_spaces(ptr2); free(word2); return ptr2; } } break; case CFG_SECTION: ptr2=rm_first_spaces(ptr2); //printf("PARA: %s\n", ptr2); //DEBUG if(*ptr2=='>') { if (word2 != NULL) { return word2; } } break; default: free(word2); return NULL; } free(word2); return NULL;}//parseword ends
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -