parse_ini.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*****************************************************************************
  2. *
  3. * Monitoring Plugins parse_ini library
  4. *
  5. * License: GPL
  6. * Copyright (c) 2007 Monitoring Plugins Development Team
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. *
  22. *****************************************************************************/
  23. #include "common.h"
  24. #include "utils_base.h"
  25. #include "parse_ini.h"
  26. #include <ctype.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <unistd.h>
  30. /* np_ini_info contains the result of parsing a "locator" in the format
  31. * [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example)
  32. */
  33. typedef struct {
  34. char *file;
  35. char *stanza;
  36. } np_ini_info;
  37. static char *default_ini_file_names[] = {
  38. "monitoring-plugins.ini",
  39. "plugins.ini",
  40. "nagios-plugins.ini",
  41. NULL
  42. };
  43. static char *default_ini_path_names[] = {
  44. "/usr/local/etc/monitoring-plugins.ini",
  45. "/etc/monitoring-plugins.ini",
  46. /* Deprecated path names (for backward compatibility): */
  47. "/etc/nagios/plugins.ini",
  48. "/usr/local/nagios/etc/plugins.ini",
  49. "/usr/local/etc/nagios/plugins.ini",
  50. "/etc/opt/nagios/plugins.ini",
  51. "/etc/nagios-plugins.ini",
  52. "/usr/local/etc/nagios-plugins.ini",
  53. "/etc/opt/nagios-plugins.ini",
  54. NULL
  55. };
  56. /* eat all characters from a FILE pointer until n is encountered */
  57. #define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n))
  58. /* internal function that returns the constructed defaults options */
  59. static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts);
  60. /* internal function that converts a single line into options format */
  61. static int add_option(FILE *f, np_arg_list **optlst);
  62. /* internal function to find default file */
  63. static char* default_file(void);
  64. /* parse_locator decomposes a string of the form
  65. * [stanza][@filename]
  66. * into its seperate parts
  67. */
  68. static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i){
  69. size_t locator_len=0, stanza_len=0;
  70. /* if locator is NULL we'll use default values */
  71. if(locator){
  72. locator_len=strlen(locator);
  73. stanza_len=strcspn(locator, "@");
  74. }
  75. /* if a non-default stanza is provided */
  76. if(stanza_len>0){
  77. i->stanza=(char*)malloc(sizeof(char)*(stanza_len+1));
  78. strncpy(i->stanza, locator, stanza_len);
  79. i->stanza[stanza_len]='\0';
  80. } else { /* otherwise we use the default stanza */
  81. i->stanza=strdup(def_stanza);
  82. }
  83. if(i->stanza==NULL){
  84. die(STATE_UNKNOWN, _("malloc() failed!\n"));
  85. }
  86. /* if there is no @file part */
  87. if(stanza_len==locator_len){
  88. i->file=default_file();
  89. } else {
  90. i->file=strdup(&(locator[stanza_len+1]));
  91. }
  92. if(i->file==NULL || i->file[0]=='\0'){
  93. die(STATE_UNKNOWN, _("Cannot find config file in any standard location.\n"));
  94. }
  95. }
  96. /* this is the externally visible function used by extra_opts */
  97. np_arg_list* np_get_defaults(const char *locator, const char *default_section){
  98. FILE *inifile=NULL;
  99. np_arg_list *defaults=NULL;
  100. np_ini_info i;
  101. parse_locator(locator, default_section, &i);
  102. if(strcmp(i.file, "-")==0){
  103. inifile=stdin;
  104. } else {
  105. inifile=fopen(i.file, "r");
  106. }
  107. if(inifile==NULL) die(STATE_UNKNOWN, "%s\n", _("Can't read config file"));
  108. if(read_defaults(inifile, i.stanza, &defaults)==FALSE)
  109. die(STATE_UNKNOWN, _("Invalid section '%s' in config file '%s'\n"), i.stanza, i.file);
  110. free(i.file);
  111. if(inifile!=stdin) fclose(inifile);
  112. free(i.stanza);
  113. return defaults;
  114. }
  115. /* read_defaults is where the meat of the parsing takes place.
  116. *
  117. * note that this may be called by a setuid binary, so we need to
  118. * be extra careful about user-supplied input (i.e. avoiding possible
  119. * format string vulnerabilities, etc)
  120. */
  121. static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts){
  122. int c, status=FALSE;
  123. size_t i, stanza_len;
  124. enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate=NOSTANZA;
  125. stanza_len=strlen(stanza);
  126. /* our little stanza-parsing state machine. */
  127. while((c=fgetc(f))!=EOF){
  128. /* gobble up leading whitespace */
  129. if(isspace(c)) continue;
  130. switch(c){
  131. /* globble up coment lines */
  132. case ';':
  133. case '#':
  134. GOBBLE_TO(f, c, '\n');
  135. break;
  136. /* start of a stanza. check to see if it matches */
  137. case '[':
  138. stanzastate=WRONGSTANZA;
  139. for(i=0; i<stanza_len; i++){
  140. c=fgetc(f);
  141. /* Strip leading whitespace */
  142. if(i==0) for(c; isspace(c); c=fgetc(f));
  143. /* nope, read to the end of the line */
  144. if(c!=stanza[i]) {
  145. GOBBLE_TO(f, c, '\n');
  146. break;
  147. }
  148. }
  149. /* if it matched up to here and the next char is ']'... */
  150. if(i==stanza_len){
  151. c=fgetc(f);
  152. /* Strip trailing whitespace */
  153. for(c; isspace(c); c=fgetc(f));
  154. if(c==']') stanzastate=RIGHTSTANZA;
  155. }
  156. break;
  157. /* otherwise, we're in the body of a stanza or a parse error */
  158. default:
  159. switch(stanzastate){
  160. /* we never found the start of the first stanza, so
  161. * we're dealing with a config error
  162. */
  163. case NOSTANZA:
  164. die(STATE_UNKNOWN, "%s\n", _("Config file error"));
  165. break;
  166. /* we're in a stanza, but for a different plugin */
  167. case WRONGSTANZA:
  168. GOBBLE_TO(f, c, '\n');
  169. break;
  170. /* okay, this is where we start taking the config */
  171. case RIGHTSTANZA:
  172. ungetc(c, f);
  173. if(add_option(f, opts)){
  174. die(STATE_UNKNOWN, "%s\n", _("Config file error"));
  175. }
  176. status=TRUE;
  177. break;
  178. }
  179. break;
  180. }
  181. }
  182. return status;
  183. }
  184. /*
  185. * read one line of input in the format
  186. * ^option[[:space:]]*(=[[:space:]]*value)?
  187. * and creates it as a cmdline argument
  188. * --option[=value]
  189. * appending it to the linked list optbuf.
  190. */
  191. static int add_option(FILE *f, np_arg_list **optlst){
  192. np_arg_list *opttmp=*optlst, *optnew;
  193. char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL;
  194. char *eqptr=NULL, *valptr=NULL, *spaceptr=NULL, *valend=NULL;
  195. short done_reading=0, equals=0, value=0;
  196. size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0;
  197. size_t opt_len=0, val_len=0;
  198. /* read one line from the file */
  199. while(!done_reading){
  200. /* grow if necessary */
  201. if(linebuf==NULL || read_pos+read_sz >= linebuf_sz){
  202. linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz;
  203. linebuf=realloc(linebuf, linebuf_sz);
  204. if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
  205. }
  206. if(fgets(&linebuf[read_pos], read_sz, f)==NULL) done_reading=1;
  207. else {
  208. read_pos=strlen(linebuf);
  209. if(linebuf[read_pos-1]=='\n') {
  210. linebuf[--read_pos]='\0';
  211. done_reading=1;
  212. }
  213. }
  214. }
  215. lineend=&linebuf[read_pos];
  216. /* all that to read one line. isn't C fun? :) now comes the parsing :/ */
  217. /* skip leading whitespace */
  218. for(optptr=linebuf; optptr<lineend && isspace(*optptr); optptr++);
  219. /* continue to '=' or EOL, watching for spaces that might precede it */
  220. for(eqptr=optptr; eqptr<lineend && *eqptr!='='; eqptr++){
  221. if(isspace(*eqptr) && optend==NULL) optend=eqptr;
  222. else optend=NULL;
  223. }
  224. if(optend==NULL) optend=eqptr;
  225. --optend;
  226. /* ^[[:space:]]*=foo is a syntax error */
  227. if(optptr==eqptr) die(STATE_UNKNOWN, "%s\n", _("Config file error"));
  228. /* continue from '=' to start of value or EOL */
  229. for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
  230. /* continue to the end of value */
  231. for(valend=valptr; valend<lineend; valend++);
  232. --valend;
  233. /* Finally trim off trailing spaces */
  234. for(valend; isspace(*valend); valend--);
  235. /* calculate the length of "--foo" */
  236. opt_len=1+optend-optptr;
  237. /* 1-character params needs only one dash */
  238. if(opt_len==1)
  239. cfg_len=1+(opt_len);
  240. else
  241. cfg_len=2+(opt_len);
  242. /* if valptr<lineend then we have to also allocate space for "=bar" */
  243. if(valptr<lineend) {
  244. equals=value=1;
  245. val_len=1+valend-valptr;
  246. cfg_len+=1+val_len;
  247. }
  248. /* if valptr==valend then we have "=" but no "bar" */
  249. else if(valptr==lineend) {
  250. equals=1;
  251. cfg_len+=1;
  252. }
  253. /* A line with no equal sign isn't valid */
  254. if(equals==0) die(STATE_UNKNOWN, "%s\n", _("Config file error"));
  255. /* okay, now we have all the info we need, so we create a new np_arg_list
  256. * element and set the argument...
  257. */
  258. optnew=malloc(sizeof(np_arg_list));
  259. optnew->next=NULL;
  260. read_pos=0;
  261. optnew->arg=malloc(cfg_len+1);
  262. /* 1-character params needs only one dash */
  263. if(opt_len==1) {
  264. strncpy(&optnew->arg[read_pos], "-", 1);
  265. read_pos+=1;
  266. } else {
  267. strncpy(&optnew->arg[read_pos], "--", 2);
  268. read_pos+=2;
  269. }
  270. strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len;
  271. if(value) {
  272. optnew->arg[read_pos++]='=';
  273. strncpy(&optnew->arg[read_pos], valptr, val_len); read_pos+=val_len;
  274. }
  275. optnew->arg[read_pos]='\0';
  276. /* ...and put that to the end of the list */
  277. if(*optlst==NULL) {
  278. *optlst=optnew;
  279. } else {
  280. while(opttmp->next!=NULL) {
  281. opttmp=opttmp->next;
  282. }
  283. opttmp->next = optnew;
  284. }
  285. free(linebuf);
  286. return 0;
  287. }
  288. static char *default_file_in_path(void){
  289. char *config_path, **file;
  290. char *dir, *ini_file, *tokens;
  291. if((config_path=getenv("NAGIOS_CONFIG_PATH"))==NULL)
  292. return NULL;
  293. if((tokens=strdup(config_path))==NULL)
  294. die(STATE_UNKNOWN, _("Insufficient Memory"));
  295. for(dir=strtok(tokens, ":"); dir!=NULL; dir=strtok(NULL, ":")){
  296. for(file=default_ini_file_names; *file!=NULL; file++){
  297. if((asprintf(&ini_file, "%s/%s", dir, *file))<0)
  298. die(STATE_UNKNOWN, _("Insufficient Memory"));
  299. if(access(ini_file, F_OK)==0){
  300. free(tokens);
  301. return ini_file;
  302. }
  303. }
  304. }
  305. free(tokens);
  306. return NULL;
  307. }
  308. static char *default_file(void){
  309. char **p, *ini_file;
  310. if((ini_file=getenv("MP_CONFIG_FILE"))!=NULL ||
  311. (ini_file=default_file_in_path())!=NULL)
  312. return ini_file;
  313. for(p=default_ini_path_names; *p!=NULL; p++)
  314. if (access(*p, F_OK)==0)
  315. return *p;
  316. return NULL;
  317. }