PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/sbin/mount_portal/conf.c

https://bitbucket.org/kmv/aeriebsd-src
C | 316 lines | 190 code | 33 blank | 93 comment | 43 complexity | 3f4af59a4fdac814a794063173ce7992 MD5 | raw file
  1. /*
  2. * Copyright (c) 1992, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. * All rights reserved.
  5. *
  6. * This code is derived from software donated to Berkeley by
  7. * Jan-Simon Pendry.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * @(#)conf.c 8.2 (Berkeley) 3/27/94
  34. */
  35. #ifndef lint
  36. static const char rcsid[] = "$ABSD: conf.c,v 1.1.1.1 2008/08/26 14:40:26 root Exp $";
  37. #endif
  38. #include <sys/param.h>
  39. #include <sys/syslog.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <unistd.h>
  43. #include <string.h>
  44. #include <errno.h>
  45. #include <limits.h>
  46. #include <regex.h>
  47. #include "portald.h"
  48. #define ALLOC(ty) (xmalloc(sizeof(ty)))
  49. typedef struct path path;
  50. struct path {
  51. qelem p_q; /* 2-way linked list */
  52. int p_lno; /* Line number of this record */
  53. char *p_args; /* copy of arg string (malloc) */
  54. char *p_key; /* Pathname to match (also p_argv[0]) */
  55. regex_t p_re; /* RE to match against pathname */
  56. int p_argc; /* number of elements in arg string */
  57. char **p_argv; /* argv[] pointers into arg string (malloc) */
  58. };
  59. static char *conf_file; /* XXX for regerror */
  60. /*
  61. * Add an element to a 2-way list,
  62. * just after (pred)
  63. */
  64. static void
  65. ins_que(qelem *elem, qelem *pred)
  66. {
  67. qelem *p = pred->q_forw;
  68. elem->q_back = pred;
  69. elem->q_forw = p;
  70. pred->q_forw = elem;
  71. p->q_back = elem;
  72. }
  73. /*
  74. * Remove an element from a 2-way list
  75. */
  76. static void
  77. rem_que(qelem *elem)
  78. {
  79. qelem *p = elem->q_forw;
  80. qelem *p2 = elem->q_back;
  81. p2->q_forw = p;
  82. p->q_back = p2;
  83. }
  84. /*
  85. * Error checking malloc
  86. */
  87. static void *
  88. xmalloc(size_t siz)
  89. {
  90. void *p = malloc(siz);
  91. if (p)
  92. return (p);
  93. syslog(LOG_ALERT, "malloc: failed to get %ld bytes", siz);
  94. exit(1);
  95. }
  96. /*
  97. * Insert the path in the list.
  98. * If there is already an element with the same key then
  99. * the *second* one is ignored (return 0). If the key is
  100. * not found then the path is added to the end of the list
  101. * and 1 is returned.
  102. */
  103. static int
  104. pinsert(path *p0, qelem *q0)
  105. {
  106. qelem *q;
  107. if (p0->p_argc == 0)
  108. return (0);
  109. for (q = q0->q_forw; q != q0; q = q->q_forw) {
  110. path *p = (path *)q;
  111. if (strcmp(p->p_key, p0->p_key) == 0)
  112. return (0);
  113. }
  114. ins_que(&p0->p_q, q0->q_back);
  115. return (1);
  116. }
  117. static path *
  118. palloc(char *cline, int lno)
  119. {
  120. int c;
  121. char *s;
  122. char *key;
  123. path *p;
  124. char **ap;
  125. /*
  126. * Implement comment chars
  127. */
  128. s = strchr(cline, '#');
  129. if (s)
  130. *s = 0;
  131. /*
  132. * Do a pass through the string to count the number
  133. * of arguments
  134. */
  135. c = 0;
  136. key = strdup(cline);
  137. if (key == NULL) {
  138. syslog(LOG_ALERT, "malloc: failed to get %ld bytes",
  139. strlen(cline));
  140. exit(1);
  141. }
  142. for (s = key; s != NULL; ) {
  143. char *val;
  144. while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
  145. ;
  146. if (val)
  147. c++;
  148. }
  149. c++;
  150. free(key);
  151. if (c <= 1)
  152. return (0);
  153. /*
  154. * Now do another pass and generate a new path structure
  155. */
  156. p = ALLOC(path);
  157. p->p_argc = 0;
  158. p->p_argv = xmalloc(c * sizeof(char *));
  159. p->p_args = strdup(cline);
  160. if (p->p_args == NULL) {
  161. syslog(LOG_ALERT, "malloc: failed to get %ld bytes",
  162. strlen(cline));
  163. exit(1);
  164. }
  165. ap = p->p_argv;
  166. for (s = p->p_args; s != NULL; ) {
  167. char *val;
  168. while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
  169. ;
  170. if (val) {
  171. *ap++ = val;
  172. p->p_argc++;
  173. }
  174. }
  175. *ap = 0;
  176. #ifdef DEBUG
  177. for (c = 0; c < p->p_argc; c++)
  178. (void)printf("%sv[%d] = %s\n", c?"\t":"", c, p->p_argv[c]);
  179. #endif
  180. p->p_key = p->p_argv[0];
  181. if ((c = regcomp(&(p->p_re), p->p_key, REG_EXTENDED))) {
  182. char errbuf[BUFSIZ];
  183. (void)regerror(c, &(p->p_re), errbuf, sizeof(errbuf));
  184. syslog(LOG_ERR, "%s:%d: regcomp %s: %s",
  185. conf_file, p->p_lno, p->p_key, errbuf);
  186. }
  187. p->p_lno = lno;
  188. return (p);
  189. }
  190. /*
  191. * Free a path structure
  192. */
  193. static void
  194. pfree(path *p)
  195. {
  196. free(p->p_args);
  197. regfree(&(p->p_re));
  198. free((void *)p->p_argv);
  199. free((void *)p);
  200. }
  201. /*
  202. * Discard all currently held path structures on q0.
  203. * and add all the ones on xq.
  204. */
  205. static void
  206. preplace(qelem *q0, qelem *xq)
  207. {
  208. /*
  209. * While the list is not empty,
  210. * take the first element off the list
  211. * and free it.
  212. */
  213. while (q0->q_forw != q0) {
  214. qelem *q = q0->q_forw;
  215. rem_que(q);
  216. pfree((path *)q);
  217. }
  218. while (xq->q_forw != xq) {
  219. qelem *q = xq->q_forw;
  220. rem_que(q);
  221. ins_que(q, q0);
  222. }
  223. }
  224. /*
  225. * Read the lines from the configuration file and
  226. * add them to the list of paths.
  227. */
  228. static void
  229. readfp(qelem *q0, FILE *fp)
  230. {
  231. char cline[LINE_MAX];
  232. int nread = 0;
  233. qelem q;
  234. /*
  235. * Make a new empty list.
  236. */
  237. q.q_forw = q.q_back = &q;
  238. /*
  239. * Read the lines from the configuration file.
  240. */
  241. while (fgets(cline, sizeof(cline), fp)) {
  242. path *p = palloc(cline, nread+1);
  243. if (p && !pinsert(p, &q))
  244. pfree(p);
  245. nread++;
  246. }
  247. /*
  248. * If some records were read, then throw
  249. * away the old list and replace with the
  250. * new one.
  251. */
  252. if (nread)
  253. preplace(q0, &q);
  254. }
  255. /*
  256. * Read the configuration file (conf) and replace
  257. * the existing path list with the new version.
  258. * If the file is not readable, then no changes take place
  259. */
  260. void
  261. conf_read(qelem *q, char *conf)
  262. {
  263. FILE *fp = fopen(conf, "r");
  264. if (fp) {
  265. conf_file = conf; /* XXX */
  266. readfp(q, fp);
  267. conf_file = NULL; /* XXX */
  268. (void)fclose(fp);
  269. } else {
  270. syslog(LOG_ERR, "open config file \"%s\": %m", conf);
  271. }
  272. }
  273. char **
  274. conf_match(qelem *q0, char *key)
  275. {
  276. qelem *q;
  277. for (q = q0->q_forw; q != q0; q = q->q_forw) {
  278. path *p = (path *)q;
  279. if (regexec(&(p->p_re), key, 0, NULL, 0) == 0)
  280. return (p->p_argv+1);
  281. }
  282. return (0);
  283. }