__sgetspent_r.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * __sgetspent_r.c - Based on __getpwent_r.c
  4. * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library 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 GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public
  17. * License along with this library; if not, write to the Free
  18. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include "config.h"
  25. int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, size_t buflen)
  26. {
  27. char *field_begin;
  28. char *endptr;
  29. char *lstchg_ptr=NULL;
  30. char *min_ptr=NULL;
  31. char *max_ptr=NULL;
  32. char *warn_ptr=NULL;
  33. char *inact_ptr=NULL;
  34. char *expire_ptr=NULL;
  35. char *flag_ptr=NULL;
  36. int i;
  37. if (buflen<PWD_BUFFER_SIZE)
  38. return ERANGE;
  39. if (string != line_buff) {
  40. if (strlen(string) >= buflen)
  41. return ERANGE;
  42. strcpy(line_buff, string);
  43. }
  44. if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' ||
  45. *line_buff == '\t')
  46. return EINVAL;
  47. field_begin = strchr(line_buff, '\n');
  48. if (field_begin != NULL)
  49. *field_begin = '\0';
  50. /* We've read the line; now parse it. */
  51. field_begin = line_buff;
  52. for (i = 0; i < 9; i++) {
  53. switch (i) {
  54. case 0:
  55. spwd->sp_namp = field_begin;
  56. break;
  57. case 1:
  58. spwd->sp_pwdp = field_begin;
  59. break;
  60. case 2:
  61. lstchg_ptr = field_begin;
  62. break;
  63. case 3:
  64. min_ptr = field_begin;
  65. break;
  66. case 4:
  67. max_ptr = field_begin;
  68. break;
  69. case 5:
  70. warn_ptr = field_begin;
  71. break;
  72. case 6:
  73. inact_ptr = field_begin;
  74. break;
  75. case 7:
  76. expire_ptr = field_begin;
  77. break;
  78. case 8:
  79. flag_ptr = field_begin;
  80. break;
  81. }
  82. if (i < 8) {
  83. field_begin = strchr(field_begin, ':');
  84. if (field_begin == NULL) {
  85. if (i==4 || i==7)
  86. break;
  87. return EINVAL;
  88. }
  89. *field_begin++ = '\0';
  90. }
  91. }
  92. if (*lstchg_ptr == '\0') {
  93. spwd->sp_lstchg = -1;
  94. } else {
  95. spwd->sp_lstchg = (gid_t) strtoul(lstchg_ptr, &endptr, 10);
  96. if (*endptr != '\0')
  97. return EINVAL;
  98. }
  99. if (*min_ptr == '\0') {
  100. spwd->sp_min = -1;
  101. } else {
  102. spwd->sp_min = (gid_t) strtoul(min_ptr, &endptr, 10);
  103. if (*endptr != '\0')
  104. return EINVAL;
  105. }
  106. if (*max_ptr == '\0') {
  107. spwd->sp_max = -1;
  108. } else {
  109. spwd->sp_max = (gid_t) strtoul(max_ptr, &endptr, 10);
  110. if (*endptr != '\0')
  111. return EINVAL;
  112. }
  113. if (warn_ptr == NULL) {
  114. /* Support for old format */
  115. spwd->sp_warn = -1;
  116. spwd->sp_inact = -1;
  117. spwd->sp_expire = -1;
  118. spwd->sp_flag = 0;
  119. }
  120. else {
  121. if (*warn_ptr == '\0') {
  122. spwd->sp_warn = -1;
  123. } else {
  124. spwd->sp_warn = (gid_t) strtoul(warn_ptr, &endptr, 10);
  125. if (*endptr != '\0')
  126. return EINVAL;
  127. }
  128. if (*inact_ptr == '\0') {
  129. spwd->sp_inact = -1;
  130. } else {
  131. spwd->sp_inact = (gid_t) strtoul(inact_ptr, &endptr, 10);
  132. if (*endptr != '\0')
  133. return EINVAL;
  134. }
  135. if (*expire_ptr == '\0') {
  136. spwd->sp_expire = -1;
  137. } else {
  138. spwd->sp_expire = (gid_t) strtoul(expire_ptr, &endptr, 10);
  139. if (*endptr != '\0')
  140. return EINVAL;
  141. }
  142. if (flag_ptr==NULL || *flag_ptr=='\0') {
  143. spwd->sp_flag = ~0ul;
  144. } else {
  145. spwd->sp_flag = (gid_t) strtoul(flag_ptr, &endptr, 10);
  146. if (*endptr != '\0')
  147. return EINVAL;
  148. }
  149. }
  150. return 0;
  151. }