summaryrefslogtreecommitdiff
path: root/src/fgetpwent.c
blob: 3a2d77c8fa05443eb67121ff48f75bef6aed2b33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright (C) 2013 Canonical Limited
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the licence, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Ryan Lortie <desrt@desrt.ca>
 */

static gchar *
fgetpwent_getline (FILE *fp)
{
  static GString str;

  /* We could be "more clever" here and avoid the strlen() but this is a
   * clear case of dumb is better.
   */
  str.len = 0;
  while (!str.len || str.str[str.len - 1] != '\n')
    {
      if (str.allocated_len < str.len + 32)
        {
          str.allocated_len = str.len + 32;
          str.str = g_realloc (str.str, str.allocated_len);
        }

      if (fgets (str.str + str.len, str.allocated_len - str.len, fp) == NULL)
        return NULL;

      str.len = strlen (str.str + str.len) + str.len;
    }

  /* chomp the '\n' */
  str.str[str.len - 1] = '\0';

  return str.str;
}

static struct passwd *
fgetpwent (FILE *fp)
{
  static struct passwd pw;
  guint64 intval;
  gchar *line;

  /* In case of failure, we ignore the line and start again */
again:
  line = fgetpwent_getline (fp);
  if (!line)
    return NULL;

  while (g_ascii_isspace (*line))
    line++;

  /* comments, empty lines */
  if (line[0] == '#' || line[0] == '\0')
    goto again;

  /* username */
  pw.pw_name = line;
  if (!(line = strchr (line, ':')))
    goto again;
  *line++ = '\0';

  /* password */
  pw.pw_passwd = line;
  if (!(line = strchr (line, ':')))
    goto again;
  *line++ = '\0';

  /* uid */
  pw.pw_uid = intval = g_ascii_strtoull (line, &line, 10);
  if (pw.pw_uid != intval || *line++ != ':')
    goto again;

  /* gid */
  pw.pw_gid = intval = g_ascii_strtoull (line, &line, 10);
  if (pw.pw_gid != intval || *line++ != ':')
    goto again;

  /* gecos */
  pw.pw_gecos = line;
  if (!(line = strchr (line, ':')))
    goto again;
  *line++ = '\0';

  /* home directory */
  pw.pw_dir = line;
  if (!(line = strchr (line, ':')))
    goto again;
  *line++ = '\0';

  /* shell */
  pw.pw_shell = line;

  return &pw;
}