config.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* SASL Config file API
  2. * Rob Siemborski
  3. * Tim Martin (originally in Cyrus distribution)
  4. */
  5. /*
  6. * Copyright (c) 1998-2016 Carnegie Mellon University. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The name "Carnegie Mellon University" must not be used to
  21. * endorse or promote products derived from this software without
  22. * prior written permission. For permission or any other legal
  23. * details, please contact
  24. * Carnegie Mellon University
  25. * Center for Technology Transfer and Enterprise Creation
  26. * 4615 Forbes Avenue
  27. * Suite 302
  28. * Pittsburgh, PA 15213
  29. * (412) 268-7393, fax: (412) 268-7395
  30. * innovation@andrew.cmu.edu
  31. *
  32. * 4. Redistributions of any form whatsoever must retain the following
  33. * acknowledgment:
  34. * "This product includes software developed by Computing Services
  35. * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
  36. *
  37. * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
  38. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  39. * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
  40. * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  41. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  42. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  43. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  44. */
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <ctype.h>
  48. #include "sasl.h"
  49. #include "saslint.h"
  50. struct configlist {
  51. char *key;
  52. char *value;
  53. };
  54. static struct configlist *configlist = NULL;
  55. static int nconfiglist = 0;
  56. #define CONFIGLISTGROWSIZE 100
  57. int sasl_config_init(const char *filename)
  58. {
  59. FILE *infile;
  60. int lineno = 0;
  61. int alloced = 0;
  62. char buf[4096];
  63. char *p, *key;
  64. char *tail;
  65. int result;
  66. nconfiglist=0;
  67. infile = fopen(filename, "r");
  68. if (!infile) {
  69. return SASL_CONTINUE;
  70. }
  71. while (fgets(buf, sizeof(buf), infile)) {
  72. lineno++;
  73. if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
  74. for (p = buf; *p && isspace((int) *p); p++);
  75. if (!*p || *p == '#') continue;
  76. key = p;
  77. while (*p && (isalnum((int) *p) || *p == '-' || *p == '_')) {
  78. if (isupper((int) *p)) *p = (char) tolower(*p);
  79. p++;
  80. }
  81. if (*p != ':') {
  82. fclose(infile);
  83. return SASL_CONFIGERR;
  84. }
  85. *p++ = '\0';
  86. while (*p && isspace((int) *p)) p++;
  87. if (!*p) {
  88. fclose(infile);
  89. return SASL_CONFIGERR;
  90. }
  91. /* Now strip trailing spaces, if any */
  92. tail = p + strlen(p) - 1;
  93. while (tail > p && isspace((int) *tail)) {
  94. *tail = '\0';
  95. tail--;
  96. }
  97. if (nconfiglist == alloced) {
  98. alloced += CONFIGLISTGROWSIZE;
  99. configlist=sasl_REALLOC((char *)configlist,
  100. alloced * sizeof(struct configlist));
  101. if (configlist == NULL) {
  102. fclose(infile);
  103. return SASL_NOMEM;
  104. }
  105. }
  106. result = _sasl_strdup(key,
  107. &(configlist[nconfiglist].key),
  108. NULL);
  109. if (result != SASL_OK) {
  110. fclose(infile);
  111. return result;
  112. }
  113. result = _sasl_strdup(p,
  114. &(configlist[nconfiglist].value),
  115. NULL);
  116. if (result != SASL_OK) {
  117. fclose(infile);
  118. return result;
  119. }
  120. nconfiglist++;
  121. }
  122. fclose(infile);
  123. return SASL_OK;
  124. }
  125. const char *sasl_config_getstring(const char *key,const char *def)
  126. {
  127. int opt;
  128. for (opt = 0; opt < nconfiglist; opt++) {
  129. if (*key == configlist[opt].key[0] &&
  130. !strcmp(key, configlist[opt].key))
  131. return configlist[opt].value;
  132. }
  133. return def;
  134. }
  135. void sasl_config_done(void)
  136. {
  137. int opt;
  138. for (opt = 0; opt < nconfiglist; opt++) {
  139. if (configlist[opt].key) sasl_FREE(configlist[opt].key);
  140. if (configlist[opt].value) sasl_FREE(configlist[opt].value);
  141. }
  142. sasl_FREE(configlist);
  143. configlist = NULL;
  144. nconfiglist = 0;
  145. }