parse_ini.c 10 KB

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