Coverage Report

Created: 2026-08-22 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/proftpd/lib/pwgrent.c
Line
Count
Source
1
/* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
2
3
This library is free software; you can redistribute it and/or
4
modify it under the terms of the GNU Library General Public License as
5
published by the Free Software Foundation; either version 2 of the
6
License, or (at your option) any later version.
7
8
This library is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
Library General Public License for more details.
12
13
You should have received a copy of the GNU Library General Public
14
License along with this library; see the file COPYING.LIB.  If
15
not, see <https://www.gnu.org/licenses/>.  */
16
17
/* Required to tell conf.h not to include the standard ProFTPD
18
 * header files
19
 */
20
21
#define __PROFTPD_SUPPORT_LIBRARY
22
23
#include "conf.h"
24
#include "base.h"
25
26
/* From log.c/log.h */
27
0
#define PR_LOG_ERR LOG_ERR
28
extern void pr_log_pri(int, char *, ...);
29
30
/* From support.c */
31
extern int sstrncpy(char *dst, const char *src, size_t n);
32
33
0
#define NPWDFIELDS  7
34
0
#define NGRPFIELDS  4
35
36
#ifndef BUFSIZ
37
#define BUFSIZ    PR_TUNABLE_BUFFER_SIZE
38
#endif
39
40
/* Provides fgetpwent()/fgetgrent() functions.  Note that the format of the
41
 * files is probably NOT platform dependent, so use of these functions will
42
 * require a strict format:
43
 *
44
 *   "username:password:uid:gid:gecos:home:shell"
45
 */
46
47
#ifndef HAVE_FGETPWENT
48
49
static char pwdbuf[BUFSIZ];
50
static char *pwdfields[NPWDFIELDS];
51
static struct passwd pwent;
52
53
0
static struct passwd *supp_getpwent(const char *buf) {
54
0
  register unsigned int i;
55
0
  register char *cp;
56
0
  char *ep = NULL, *buffer = NULL;
57
0
  char **fields = NULL;
58
0
  struct passwd *pwd = NULL;
59
60
0
  fields = pwdfields;
61
0
  buffer = pwdbuf;
62
0
  pwd = &pwent;
63
64
0
  sstrncpy(buffer, buf, BUFSIZ-1);
65
0
  buffer[BUFSIZ-1] = '\0';
66
67
0
  for(cp = buffer, i = 0; i < NPWDFIELDS && cp; i++) {
68
0
    fields[i] = cp;
69
0
    while (*cp && *cp != ':')
70
0
      ++cp;
71
72
0
    if (*cp)
73
0
      *cp++ = '\0';
74
0
    else
75
0
      cp = 0;
76
0
  }
77
78
0
  if (i != NPWDFIELDS || *fields[2] == '\0' || *fields[3] == '\0')
79
0
    return 0;
80
81
0
  pwd->pw_name = fields[0];
82
0
  pwd->pw_passwd = fields[1];
83
84
0
  if (fields[2][0] == '\0' ||
85
0
     ((pwd->pw_uid = strtol(fields[2], &ep, 10)) == 0 && *ep))
86
0
       return 0;
87
88
0
  if (fields[3][0] == '\0' ||
89
0
     ((pwd->pw_gid = strtol(fields[3], &ep, 10)) == 0 && *ep))
90
0
       return 0;
91
92
0
  pwd->pw_gecos = fields[4];
93
0
  pwd->pw_dir = fields[5];
94
0
  pwd->pw_shell = fields[6];
95
96
0
  return pwd;
97
0
}
98
99
0
struct passwd *fgetpwent(FILE *fp) {
100
0
  char buf[BUFSIZ] = {'\0'};
101
102
0
  while (fgets(buf, sizeof(buf), fp) != (char*) 0) {
103
104
    /* ignore empty and comment lines */
105
0
    if (buf[0] == '\0' || buf[0] == '#')
106
0
      continue;
107
108
0
    buf[strlen(buf)-1] = '\0';
109
0
    return supp_getpwent(buf);
110
0
  }
111
112
0
  return NULL;
113
0
}
114
#endif /* HAVE_FGETPWENT */
115
116
#ifndef HAVE_FGETGRENT
117
0
#define MAXMEMBERS 4096
118
119
static char *grpbuf = NULL;
120
static struct group grent;
121
static char *grpfields[NGRPFIELDS];
122
static char *members[MAXMEMBERS+1];
123
124
0
static char *fgetbufline(char **buf, int *buflen, FILE *fp) {
125
0
  char *cp = *buf;
126
127
0
  while (fgets(cp, (*buflen) - (cp - *buf), fp) != NULL) {
128
129
    /* Is this a full line? */
130
0
    if (strchr(cp, '\n'))
131
0
      return *buf;
132
133
    /* No -- allocate a larger buffer, doubling buflen. */
134
0
    *buflen += *buflen;
135
136
0
    {
137
0
      char *new_buf;
138
139
0
      new_buf = realloc(*buf, *buflen);
140
0
      if (new_buf == NULL)
141
0
        break;
142
143
0
      *buf = new_buf;
144
0
    }
145
146
0
    cp = *buf + (cp - *buf);
147
0
    cp = strchr(cp, '\0');
148
0
  }
149
150
0
  free(*buf);
151
0
  *buf = NULL;
152
0
  *buflen = 0;
153
154
0
  return NULL;
155
0
}
156
157
0
static char **supp_grplist(char *s) {
158
0
  int nmembers = 0;
159
160
0
  while (s && *s && nmembers < MAXMEMBERS) {
161
0
    members[nmembers++] = s;
162
0
    while (*s && *s != ',')
163
0
      s++;
164
165
0
    if (*s)
166
0
      *s++ = '\0';
167
0
  }
168
169
0
  members[nmembers] = NULL;
170
0
  return members;
171
0
}
172
173
0
static struct group *supp_getgrent(const char *buf) {
174
0
  int i;
175
0
  char *cp;
176
177
0
  i = strlen(buf) + 1;
178
179
0
  if (!grpbuf) {
180
0
    grpbuf = malloc(i);
181
182
0
  } else {
183
0
    char *new_buf;
184
185
0
    new_buf = realloc(grpbuf, i);
186
0
    if (new_buf == NULL)
187
0
      return NULL;
188
189
0
    grpbuf = new_buf;
190
0
  }
191
192
0
  if (!grpbuf)
193
0
    return NULL;
194
195
0
  sstrncpy(grpbuf, buf, i);
196
197
0
  if ((cp = strrchr(grpbuf, '\n')))
198
0
    *cp = '\0';
199
200
0
  for (cp = grpbuf, i = 0; i < NGRPFIELDS && cp; i++) {
201
0
    grpfields[i] = cp;
202
203
0
    if ((cp = strchr(cp, ':')))
204
0
      *cp++ = 0;
205
0
  }
206
207
0
  if (i < (NGRPFIELDS - 1)) {
208
0
    pr_log_pri(PR_LOG_ERR, "Malformed entry in group file: %s", buf);
209
0
    return NULL;
210
0
  }
211
212
0
  if (*grpfields[2] == '\0')
213
0
    return NULL;
214
215
0
  grent.gr_name = grpfields[0];
216
0
  grent.gr_passwd = grpfields[1];
217
0
  grent.gr_gid = atoi(grpfields[2]);
218
0
  grent.gr_mem = supp_grplist(grpfields[3]);
219
220
0
  return &grent;
221
0
}
222
223
0
struct group *fgetgrent(FILE *fp) {
224
0
  char *cp = NULL, *buf = malloc(BUFSIZ);
225
0
  int buflen = BUFSIZ;
226
0
  struct group *grp = NULL;
227
228
0
  if (!buf)
229
0
    return NULL;
230
231
0
  while (fgetbufline(&buf, &buflen, fp) != NULL) {
232
233
    /* ignore comment and empty lines */
234
0
    if (buf[0] == '\0' || buf[0] == '#')
235
0
      continue;
236
237
0
    if ((cp = strchr(buf, '\n')) != NULL)
238
0
      *cp = '\0';
239
240
0
    grp = supp_getgrent(buf);
241
0
    free(buf);
242
243
0
    return grp;
244
0
  }
245
246
0
  return NULL;
247
0
}
248
249
#endif /* HAVE_FGETGRENT */