File: user.c

package info (click to toggle)
cssc 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,540 kB
  • ctags: 10,602
  • sloc: cpp: 39,776; sh: 16,907; ansic: 13,767; python: 3,923; makefile: 1,698; perl: 342; awk: 15
file content (222 lines) | stat: -rw-r--r-- 4,898 bytes parent folder | download | duplicates (3)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * user.c: Part of GNU CSSC.
 *
 *
 *    Copyright (C) 1997,2007 Free Software Foundation, Inc.
 *
 *    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 License, 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, see <http://www.gnu.org/licenses/>.
 *
 * Program for getting the user's login name.
 */
#include <config.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>

const char usage_str[] = "usage: \"user name\" or \"user group\"\n";


static int duplicate_group(gid_t g, const gid_t *vec, int len)
{
  while (len--)
    {
      if (*vec == g)
        return 1;
    }
  return 0;
}


/* do_groups
 *
 * Emits a list of groups associated with the current
 * process.  The effectve group ID is also returned,
 * and this may be a suplicate entry.
 */
static gid_t *get_group_list(int *ngroups)
{
  int len;
  gid_t *grouplist;

  len = getgroups(0, NULL);

  if (len < 0)
    {
      perror("getgroups");
      return NULL;
    }
  else
    {
      grouplist = malloc((1+len) * sizeof(*grouplist));
      if (grouplist)
        {
          /* We don't know if the effectve group ID is in
           * the list returned by grouplist, so find out
           * and return a list with it included, but only once
           */
          gid_t egid = getegid();
          if (getgroups(len, &grouplist[1]))
            {
              if (duplicate_group(egid, grouplist+1, len))
                {
                  *ngroups = len;
                  return &grouplist[1];
                }
              else
                {
                  grouplist[0] = egid;
                  *ngroups = len+1;
                  return &grouplist[0];
                }
            }
          else
            {
              perror("getgroups");
              return NULL;
            }
        }
      else
        {
              perror("malloc");
              return NULL;
        }
    }
}


static void do_groups()
{
  int ngroups;
  const gid_t *list = get_group_list(&ngroups);

  if (list)
    {
      int i;

      for (i=0; i<ngroups; ++i)
        {
          fprintf(stdout, "%ld\n", (long) list[i]);
        }
    }
  else
    {
      /* fallback. */
      fprintf(stdout, "%ld\n", (long) getegid());
    }
}


static int compare_groups(const void *pv1, const void *pv2)
{
  const gid_t *p1 = (const gid_t*) pv1;
  const gid_t *p2 = (const gid_t*) pv2;

  if (*p1 < *p2)
    return -1;
  else if (*p1 > *p2)
    return 1;
  else
    return 0;
}


/* Find and print the Id of a group of which we are not a member.
 * This group will (most likely) not have a name
 */
static gid_t foreign_group(void)
{
  int ngroups, i;
  gid_t *list = get_group_list(&ngroups);

  qsort(list, ngroups, sizeof(*list), compare_groups);

  for (i=1; i<ngroups; ++i)
    {
      /* nextval is a gid_t value 1 greater than the last gid we saw */
      const gid_t nextval = 1+list[i-1];

      if (nextval < list[i] )
        {
          /* we have a gap. */
          return nextval;
        }
    }

  /* We can't just add 1 to the value of the last entry in the list
   * because of the possibility of overflow.
   */
  if (ngroups)
    {
      if (list[0] > 0)
        return 0;
      else
        return 1+list[ngroups-1];
    }
  else /* not a member of any groups? */
    {
      gid_t last_resort = getegid();
      if (last_resort > 0)
        return 0;
      else
        return 1+last_resort;
    }
}


int main(int argc, char *argv[])
{
  if (2 == argc)
    {
      if (0 == strcmp(argv[1], "name"))
        {
          struct passwd *p;
          const char *pn = "unknown";
          p = getpwuid(getuid());
          if (p)
            pn = p->pw_name;

          fprintf(stdout, "%s\n", pn);
          return 0;
        }
      else if (0 == strcmp(argv[1], "groups"))
        {
          do_groups();
          return 0;
        }
      else if (0 == strcmp(argv[1], "foreigngroup"))
        {
          fprintf(stdout, "%ld\n", (long)foreign_group());
          return 0;
        }
      else if (0 == strcmp(argv[1], "group"))
        {
          fprintf(stdout, "%ld\n", (long)getgid());
          return 0;
        }
      else
        {
          fprintf(stderr, "%s", usage_str);
          return 1;
        }
    }
  else
    {
      fprintf(stderr, "%s", usage_str);
      return 1;
    }
}