parse_ini.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "common.h"
  2. #include "parse_ini.h"
  3. #include "utils_base.h"
  4. #include <ctype.h>
  5. /* np_ini_info contains the result of parsing a "locator" in the format
  6. * [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example)
  7. */
  8. typedef struct {
  9. char *file;
  10. char *stanza;
  11. } np_ini_info;
  12. /* eat all characters from a FILE pointer until n is encountered */
  13. #define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n))
  14. /* internal function that returns the constructed defaults options */
  15. static char* read_defaults(FILE *f, const char *stanza);
  16. /* internal function that converts a single line into options format */
  17. static int add_option(FILE *f, char **optbuf, size_t *bufsize);
  18. /* parse_locator decomposes a string of the form
  19. * [stanza][@filename]
  20. * into its seperate parts
  21. */
  22. static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i){
  23. size_t locator_len, stanza_len;
  24. locator_len=strlen(locator);
  25. stanza_len=strcspn(locator, "@");
  26. /* if a non-default stanza is provided */
  27. if(stanza_len>0){
  28. i->stanza=(char*)malloc(sizeof(char)*(stanza_len+1));
  29. strncpy(i->stanza, locator, stanza_len);
  30. i->stanza[stanza_len]='\0';
  31. } else { /* otherwise we use the default stanza */
  32. i->stanza=strdup(def_stanza);
  33. }
  34. /* if there is no @file part */
  35. if(stanza_len==locator_len){
  36. i->file=strdup(NP_DEFAULT_INI_PATH);
  37. } else {
  38. i->file=strdup(&(locator[stanza_len+1]));
  39. }
  40. if(i->file==NULL || i->stanza==NULL){
  41. die(STATE_UNKNOWN, _("malloc() failed!\n"));
  42. }
  43. }
  44. /* this is the externally visible function used by plugins */
  45. char* np_get_defaults(const char *locator, const char *default_section){
  46. FILE *inifile=NULL;
  47. char *defaults=NULL;
  48. np_ini_info i;
  49. parse_locator(locator, default_section, &i);
  50. /* if a file was specified or if we're using the default file */
  51. if(i.file != NULL && strlen(i.file) > 0){
  52. if(strcmp(i.file, "-")==0){
  53. inifile=stdout;
  54. } else {
  55. inifile=fopen(i.file, "r");
  56. }
  57. if(inifile==NULL) die(STATE_UNKNOWN, _("Config file error"));
  58. defaults=read_defaults(inifile, i.stanza);
  59. free(i.file);
  60. if(inifile!=stdout) fclose(inifile);
  61. }
  62. free(i.stanza);
  63. return defaults;
  64. }
  65. /* read_defaults is where the meat of the parsing takes place.
  66. *
  67. * note that this may be called by a setuid binary, so we need to
  68. * be extra careful about user-supplied input (i.e. avoiding possible
  69. * format string vulnerabilities, etc)
  70. */
  71. static char* read_defaults(FILE *f, const char *stanza){
  72. int c;
  73. char *opts=NULL;
  74. size_t i, stanza_len, opts_buf_size=0;
  75. enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate=NOSTANZA;
  76. stanza_len=strlen(stanza);
  77. /* our little stanza-parsing state machine. */
  78. while((c=fgetc(f))!=EOF){
  79. /* gobble up leading whitespace */
  80. if(isspace(c)) continue;
  81. switch(c){
  82. /* globble up coment lines */
  83. case '#':
  84. GOBBLE_TO(f, c, '\n');
  85. break;
  86. /* start of a stanza. check to see if it matches */
  87. case '[':
  88. stanzastate=WRONGSTANZA;
  89. for(i=0; i<stanza_len; i++){
  90. c=fgetc(f);
  91. /* nope, read to the end of the stanza header */
  92. if(c!=stanza[i]) {
  93. GOBBLE_TO(f, c, ']');
  94. break;
  95. }
  96. }
  97. /* if it matched up to here and the next char is ']'... */
  98. if(i==stanza_len){
  99. c=fgetc(f);
  100. if(c==']') stanzastate=RIGHTSTANZA;
  101. }
  102. break;
  103. /* otherwise, we're in the body of a stanza or a parse error */
  104. default:
  105. switch(stanzastate){
  106. /* we never found the start of the first stanza, so
  107. * we're dealing with a config error
  108. */
  109. case NOSTANZA:
  110. die(STATE_UNKNOWN, _("Config file error"));
  111. break;
  112. /* we're in a stanza, but for a different plugin */
  113. case WRONGSTANZA:
  114. GOBBLE_TO(f, c, '\n');
  115. break;
  116. /* okay, this is where we start taking the config */
  117. case RIGHTSTANZA:
  118. ungetc(c, f);
  119. if(add_option(f, &opts, &opts_buf_size)){
  120. die(STATE_UNKNOWN, _("Config file error"));
  121. }
  122. break;
  123. }
  124. break;
  125. }
  126. }
  127. return opts;
  128. }
  129. /*
  130. * read one line of input in the format
  131. * ^option[[:space:]]*(=[[:space:]]*value)?
  132. * and creates it as a cmdline argument
  133. * --option[=value]
  134. * appending it to the string pointed to by optbuf (which will
  135. * be dynamically grown if needed)
  136. */
  137. static int add_option(FILE *f, char **optbuf, size_t *bufsize){
  138. char *newbuf=*optbuf;
  139. char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL;
  140. char *eqptr=NULL, *valptr=NULL, *spaceptr=NULL, *valend=NULL;
  141. short done_reading=0, equals=0, value=0;
  142. size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0, bs=*bufsize;
  143. size_t opt_len=0, val_len=0;
  144. /* read one line from the file */
  145. while(!done_reading){
  146. /* grow if necessary */
  147. if(linebuf==NULL || read_pos+read_sz >= linebuf_sz){
  148. linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz;
  149. linebuf=realloc(linebuf, linebuf_sz);
  150. if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
  151. }
  152. if(fgets(&linebuf[read_pos], read_sz, f)==NULL) done_reading=1;
  153. else {
  154. read_pos=strlen(linebuf);
  155. if(linebuf[read_pos-1]=='\n') {
  156. linebuf[--read_pos]='\0';
  157. done_reading=1;
  158. }
  159. }
  160. }
  161. lineend=&linebuf[read_pos];
  162. /* all that to read one line. isn't C fun? :) now comes the parsing :/ */
  163. /* skip leading whitespace */
  164. for(optptr=linebuf; optptr<lineend && isspace(*optptr); optptr++);
  165. /* continue to '=' or EOL, watching for spaces that might precede it */
  166. for(eqptr=optptr; eqptr<lineend && *eqptr!='='; eqptr++){
  167. if(isspace(*eqptr) && optend==NULL) optend=eqptr;
  168. else optend=NULL;
  169. }
  170. if(optend==NULL) optend=eqptr;
  171. --optend;
  172. /* ^[[:space:]]*=foo is a syntax error */
  173. if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n"));
  174. /* continue from '=' to start of value or EOL */
  175. for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
  176. /* continue to the end of value, watching for trailing space/comments */
  177. for(valend=valptr; valend<lineend; valend++){
  178. if(isspace(*valend) && spaceptr==NULL) spaceptr=valend;
  179. else if(*valend=='#') break;
  180. else spaceptr=NULL;
  181. }
  182. if(spaceptr!=NULL) valend=spaceptr;
  183. --valend;
  184. /* calculate the length of "--foo" */
  185. opt_len=1+optend-optptr;
  186. cfg_len=2+(opt_len);
  187. /* if valptr<lineend then we have to also allocate space for "=bar" */
  188. if(valptr<lineend) {
  189. equals=value=1;
  190. val_len=1+valend-valptr;
  191. cfg_len+=1+val_len;
  192. }
  193. /* if valptr==valend then we have "=" but no "bar" */
  194. else if (valptr==lineend) {
  195. equals=1;
  196. cfg_len+=1;
  197. }
  198. /* okay, now we have all the info we need, so we grow the default opts
  199. * buffer if it's necessary, and put everything together.
  200. * (+2 is for a potential space and a null byte)
  201. */
  202. read_pos=(newbuf==NULL)?0:strlen(newbuf);
  203. if(newbuf==NULL || read_pos+cfg_len+2 >= bs){
  204. bs=(bs>0)?(bs+cfg_len+2)<<1:cfg_len+1;
  205. newbuf=realloc(newbuf, bs);
  206. if(newbuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
  207. }
  208. if(read_pos>0) newbuf[read_pos++]=' ';
  209. strncpy(&newbuf[read_pos], "--", 2); read_pos+=2;
  210. strncpy(&newbuf[read_pos], optptr, opt_len); read_pos+=opt_len;
  211. if(equals) newbuf[read_pos++]='=';
  212. if(value) {
  213. strncpy(&newbuf[read_pos], valptr, val_len); read_pos+=val_len;
  214. }
  215. newbuf[read_pos]='\0';
  216. *optbuf=newbuf;
  217. *bufsize=bs;
  218. free(linebuf);
  219. return 0;
  220. }