imapd.patch.group 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. --- lib/auth_unix.c.orig Wed Oct 22 20:50:12 2003
  2. +++ lib/auth_unix.c Fri Jan 16 15:24:05 2004
  3. @@ -48,6 +48,7 @@
  4. #include <stdlib.h>
  5. #include <pwd.h>
  6. #include <grp.h>
  7. +#include <stdio.h>
  8. #include <ctype.h>
  9. #include <string.h>
  10. @@ -55,6 +56,126 @@
  11. #include "libcyr_cfg.h"
  12. #include "xmalloc.h"
  13. +#ifdef __FreeBSD__
  14. +/*
  15. + * __getgrent.c - This file is part of the libc-8086/grp package for ELKS,
  16. + * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>.
  17. + *
  18. + * This library is free software; you can redistribute it and/or
  19. + * modify it under the terms of the GNU Library General Public
  20. + * License as published by the Free Software Foundation; either
  21. + * version 2 of the License, or (at your option) any later version.
  22. + *
  23. + * This library is distributed in the hope that it will be useful,
  24. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. + * Library General Public License for more details.
  27. + *
  28. + * You should have received a copy of the GNU Library General Public
  29. + * License along with this library; if not, write to the Free
  30. + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  31. + *
  32. + */
  33. +
  34. +#include <unistd.h>
  35. +#include <string.h>
  36. +#include <errno.h>
  37. +
  38. +static struct group *__getgrent(int grp_fd, char *line_buff, char **members)
  39. +{
  40. + short line_index;
  41. + short buff_size;
  42. + static struct group group;
  43. + register char *ptr;
  44. + char *field_begin;
  45. + short member_num;
  46. + char *endptr;
  47. + int line_len;
  48. +
  49. + /* We use the restart label to handle malformatted lines */
  50. + restart:
  51. + line_index = 0;
  52. + buff_size = 256;
  53. +
  54. + line_buff = realloc(line_buff, buff_size);
  55. + while (1) {
  56. + if ((line_len = read(grp_fd, line_buff + line_index,
  57. + buff_size - line_index)) <= 0) {
  58. + return NULL;
  59. + }
  60. + field_begin = strchr(line_buff, '\n');
  61. + if (field_begin != NULL) {
  62. + lseek(grp_fd,
  63. + (long) (1 + field_begin -
  64. + (line_len + line_index + line_buff)), SEEK_CUR);
  65. + *field_begin = '\0';
  66. + if (*line_buff == '#' || *line_buff == ' '
  67. + || *line_buff == '\n' || *line_buff == '\t')
  68. + goto restart;
  69. + break;
  70. + } else {
  71. + /* Allocate some more space */
  72. + line_index = buff_size;
  73. + buff_size += 256;
  74. + line_buff = realloc(line_buff, buff_size);
  75. + }
  76. + }
  77. +
  78. + /* Now parse the line */
  79. + group.gr_name = line_buff;
  80. + ptr = strchr(line_buff, ':');
  81. + if (ptr == NULL)
  82. + goto restart;
  83. + *ptr++ = '\0';
  84. +
  85. + group.gr_passwd = ptr;
  86. + ptr = strchr(ptr, ':');
  87. + if (ptr == NULL)
  88. + goto restart;
  89. + *ptr++ = '\0';
  90. +
  91. + field_begin = ptr;
  92. + ptr = strchr(ptr, ':');
  93. + if (ptr == NULL)
  94. + goto restart;
  95. + *ptr++ = '\0';
  96. +
  97. + group.gr_gid = (gid_t) strtoul(field_begin, &endptr, 10);
  98. + if (*endptr != '\0')
  99. + goto restart;
  100. +
  101. + member_num = 0;
  102. + field_begin = ptr;
  103. +
  104. + if (members != NULL)
  105. + free(members);
  106. + members = (char **) malloc((member_num + 1) * sizeof(char *));
  107. + for ( ; field_begin && *field_begin != '\0'; field_begin = ptr) {
  108. + if ((ptr = strchr(field_begin, ',')) != NULL)
  109. + *ptr++ = '\0';
  110. + members[member_num++] = field_begin;
  111. + members = (char **) realloc(members,
  112. + (member_num + 1) * sizeof(char *));
  113. + }
  114. + members[member_num] = NULL;
  115. +
  116. + group.gr_mem = members;
  117. + return &group;
  118. +}
  119. +
  120. +static char *line_buff = NULL;
  121. +static char **members = NULL;
  122. +
  123. +struct group *fgetgrent(FILE *file)
  124. +{
  125. + if (file == NULL) {
  126. + errno = EINTR;
  127. + return NULL;
  128. + }
  129. + return __getgrent(fileno(file), line_buff, members);
  130. +}
  131. +#endif /* __FreeBSD__ */
  132. +
  133. const char *auth_method_desc = "unix";
  134. struct auth_state {
  135. @@ -144,6 +265,25 @@
  136. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  137. };
  138. +static struct group* fgetgrnam(const char* name)
  139. +{
  140. + struct group *grp;
  141. + FILE *groupfile;
  142. +
  143. + groupfile = fopen("/etc/imapd.group", "r");
  144. + if (!groupfile) groupfile = fopen("/etc/group", "r");
  145. + if (groupfile) {
  146. + while ((grp = fgetgrent(groupfile))) {
  147. + if (strcmp(grp->gr_name, name) == 0) {
  148. + fclose(groupfile);
  149. + return grp;
  150. + }
  151. + }
  152. + }
  153. + if (groupfile) fclose(groupfile);
  154. + return NULL;
  155. +}
  156. +
  157. /*
  158. * Convert 'identifier' into canonical form.
  159. * Returns a pointer to a static buffer containing the canonical form
  160. @@ -179,7 +319,7 @@
  161. */
  162. if (!strncmp(retbuf, "group:", 6)) {
  163. - grp = getgrnam(retbuf+6);
  164. + grp = fgetgrnam(retbuf+6);
  165. if (!grp) return 0;
  166. strcpy(retbuf+6, grp->gr_name);
  167. return retbuf;
  168. @@ -224,6 +364,7 @@
  169. struct passwd *pwd;
  170. struct group *grp;
  171. char **mem;
  172. + FILE *groupfile;
  173. identifier = auth_canonifyid(identifier, 0);
  174. if (!identifier) return 0;
  175. @@ -240,20 +381,23 @@
  176. pwd = getpwnam(identifier);
  177. - setgrent();
  178. - while ((grp = getgrent())) {
  179. - for (mem = grp->gr_mem; *mem; mem++) {
  180. - if (!strcmp(*mem, identifier)) break;
  181. - }
  182. -
  183. - if (*mem || (pwd && pwd->pw_gid == grp->gr_gid)) {
  184. - newstate->ngroups++;
  185. - newstate->group = (char **)xrealloc((char *)newstate->group,
  186. - newstate->ngroups * sizeof(char *));
  187. - newstate->group[newstate->ngroups-1] = xstrdup(grp->gr_name);
  188. - }
  189. - }
  190. - endgrent();
  191. + groupfile = fopen("/etc/imapd.group", "r");
  192. + if (!groupfile) groupfile = fopen("/etc/group", "r");
  193. + if (groupfile) {
  194. + while ((grp = fgetgrent(groupfile))) {
  195. + for (mem = grp->gr_mem; *mem; mem++) {
  196. + if (!strcmp(*mem, identifier)) break;
  197. + }
  198. +
  199. + if (*mem || (pwd && pwd->pw_gid == grp->gr_gid)) {
  200. + newstate->ngroups++;
  201. + newstate->group = (char **)xrealloc((char *)newstate->group,
  202. + newstate->ngroups * sizeof(char *));
  203. + newstate->group[newstate->ngroups-1] = xstrdup(grp->gr_name);
  204. + }
  205. + }
  206. + fclose(groupfile);
  207. + }
  208. return newstate;
  209. }