PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/libcli/dns/resolvconf.c

https://gitlab.com/miztake/samba
C | 123 lines | 86 code | 18 blank | 19 comment | 25 complexity | 3d417fd54f4dde0f0e56db3eb587022f MD5 | raw file
  1. /*
  2. * Unix SMB/CIFS implementation.
  3. * Internal DNS query structures
  4. * Copyright (C) Volker Lendecke 2018
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "replace.h"
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include "libcli/dns/resolvconf.h"
  23. #include "lib/util/memory.h"
  24. int parse_resolvconf_fp(
  25. FILE *fp,
  26. TALLOC_CTX *mem_ctx,
  27. char ***pnameservers,
  28. size_t *pnum_nameservers)
  29. {
  30. char *line = NULL;
  31. size_t len = 0;
  32. char **nameservers = NULL;
  33. size_t num_nameservers = 0;
  34. int ret = 0;
  35. while (true) {
  36. char *saveptr = NULL, *option = NULL, *ns = NULL;
  37. char **tmp = NULL;
  38. ssize_t n = 0;
  39. n = getline(&line, &len, fp);
  40. if (n < 0) {
  41. if (!feof(fp)) {
  42. /* real error */
  43. ret = errno;
  44. }
  45. break;
  46. }
  47. if ((n > 0) && (line[n-1] == '\n')) {
  48. line[n-1] = '\0';
  49. }
  50. if ((line[0] == '#') || (line[0] == ';')) {
  51. continue;
  52. }
  53. option = strtok_r(line, " \t", &saveptr);
  54. if (option == NULL) {
  55. continue;
  56. }
  57. if (strcmp(option, "nameserver") != 0) {
  58. continue;
  59. }
  60. ns = strtok_r(NULL, " \t", &saveptr);
  61. if (ns == NULL) {
  62. continue;
  63. }
  64. tmp = talloc_realloc(
  65. mem_ctx,
  66. nameservers,
  67. char *,
  68. num_nameservers+1);
  69. if (tmp == NULL) {
  70. ret = ENOMEM;
  71. break;
  72. }
  73. nameservers = tmp;
  74. nameservers[num_nameservers] = talloc_strdup(nameservers, ns);
  75. if (nameservers[num_nameservers] == NULL) {
  76. ret = ENOMEM;
  77. break;
  78. }
  79. num_nameservers += 1;
  80. }
  81. SAFE_FREE(line);
  82. if (ret == 0) {
  83. *pnameservers = nameservers;
  84. *pnum_nameservers = num_nameservers;
  85. } else {
  86. TALLOC_FREE(nameservers);
  87. }
  88. return ret;
  89. }
  90. int parse_resolvconf(
  91. const char *resolvconf,
  92. TALLOC_CTX *mem_ctx,
  93. char ***pnameservers,
  94. size_t *pnum_nameservers)
  95. {
  96. FILE *fp;
  97. int ret;
  98. fp = fopen(resolvconf ? resolvconf : "/etc/resolv.conf", "r");
  99. if (fp == NULL) {
  100. return errno;
  101. }
  102. ret = parse_resolvconf_fp(fp, mem_ctx, pnameservers, pnum_nameservers);
  103. fclose(fp);
  104. return ret;
  105. }