File: xispid.c

package info (click to toggle)
xisp 2.5p4-1
  • links: PTS
  • area: contrib
  • in suites: slink
  • size: 1,328 kB
  • ctags: 1,908
  • sloc: ansic: 16,648; asm: 214; sh: 210; makefile: 202; perl: 55
file content (498 lines) | stat: -rw-r--r-- 10,663 bytes parent folder | download
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
  Copyright (C) 1997,1998  Dimitrios P. Bouras

   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   For author contact information, look in the README file.

   GNU id, written by Arnold Robbins, arnold@audiofax.com,
   with major rewrite by David MacKenzie, djm@gnu.ai.mit.edu,
   is (C) 89, 90, 91, 92, 93, 1994 Free Software Foundation, Inc.
   Parts of id and miscellaneous GNU library functions assembled
   for xispid by Dimitrios P. Bouras <dbouras@hol.gr>
*/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <string.h>
#include <varargs.h>
#include <sys/param.h>
#include <sys/stat.h>
#include "common.h"
#include "version.h"

#ifdef SUNOS5x
#include <limits.h>
#endif

struct passwd *getpwuid();
struct group *getgrgid(), *getgrent();
uid_t getuid(), geteuid();
gid_t getgid(), getegid();
void setgrent(), endgrent(), *malloc(), *realloc(), free();
int fflush(), fputs(), _flsbuf(), fprintf(), vfprintf(), printf(), getopt();

#if !defined(NGROUPS_MAX) && defined(NGROUPS)
#define NGROUPS_MAX NGROUPS
#endif

#ifndef SUNOS5x
#ifdef GLIBC2
static const char *private_strerror (errnum)
#else
static char *private_strerror (errnum)
#endif
     int errnum;
{
#ifdef GLIBC2
  extern const char *const sys_errlist[];
#else
  extern char *sys_errlist[];
#endif
  extern int sys_nerr;

  if (errnum > 0 && errnum <= sys_nerr)
    return sys_errlist[errnum];
  return "Unknown system error";
}
#define strerror private_strerror
#endif

void error (va_alist) va_dcl
{
  int status, errnum;
  char *message;
  extern char *program_name;
  va_list args;

  fflush (stdout);
  fprintf (stderr, "%s: ", program_name);

  va_start (args);
  status = va_arg(args, int);
  errnum = va_arg(args, int);
  message = va_arg(args, char*);
  vfprintf (stderr, message, args);
  va_end (args);

  if (errnum)
    fprintf (stderr, ": %s", strerror (errnum));
  putc ('\n', stderr);
  fflush (stderr);
  if (status)
    exit (status);
}

static void *fixup_null_alloc (n)
     size_t n;
{
  void *p;

  p = 0;
  if (n == 0)
    p = malloc ((size_t) 1);
  if (p == 0)
    error (1, 0, "memory exhausted");
  return p;
}

void *xmalloc (n)
     size_t n;
{
  void *p;

  p = malloc (n);
  if (p == 0)
    p = fixup_null_alloc (n);
  return p;
}

void *xrealloc (p, n)
     void *p;
     size_t n;
{
  if (p == 0)
    return xmalloc (n);
  p = realloc (p, n);
  if (p == 0)
    p = fixup_null_alloc (n);
  return p;
}

int getugroups (maxcount, grouplist, username)
     int maxcount;
     int *grouplist;
     char *username;
{
  struct group *grp;
  register char **cp;
  register int count = 0;

  setgrent ();
  while ((grp = getgrent ()) != 0)
    for (cp = grp->gr_mem; *cp; ++cp)
      if (!strcmp (username, *cp))
	{
	  if (maxcount != 0)
	    {
	      if (count >= maxcount)
		{
		  endgrent ();
		  return count;
		}
	      grouplist[count] = grp->gr_gid;
	    }
	  count++;
	}
  endgrent ();
  return count;
}

static void print_user ();
static void print_group ();
static void print_group_list ();
static void print_full_info ();
static void usage ();

/* The name this program was run with. */
char *program_name;

/* If nonzero, output only the group ID(s). -g */
static int just_group = 0;

/* If nonzero, output user/group name instead of ID number. -n */
static int use_name = 0;

/* If nonzero, output real UID/GID instead of default effective UID/GID. -r */
static int use_real = 0;

/* If nonzero, output only the user ID(s). -u */
static int just_user = 0;

/* If nonzero, output only the supplementary groups. -G */
static int just_group_list = 0;

/* The real and effective IDs of the user to print. */
static uid_t ruid, euid;
static gid_t rgid, egid;

/* The number of errors encountered so far. */
static int problems = 0;

/* If non-zero, display usage information and exit.  */
static int show_help;

/* If non-zero, print the version on standard output and exit.  */
static int show_version;

/*
 If non-zero, check the file name argument for the following:
 - name starts with ".xisppipe",
 - mode is 0010600 (i.e. named pipe read-write only by its owner), and
 - owner GID is equal to the caller's GID
*/
static int check_pipe;
static char *pipe_fname;

int main (argc, argv)
     int argc;
     char **argv;
{
  char *p;
  int optc;
  extern int optind;
  extern char *optarg;

  p = RCSid;
  program_name = argv[0];

  while ((optc = getopt (argc, argv, "gnruGhvc:"))
	 != EOF)
    {
      switch (optc)
	{
	case 0:
	  break;
	case 'g':
	  just_group = 1;
	  break;
	case 'n':
	  use_name = 1;
	  break;
	case 'r':
	  use_real = 1;
	  break;
	case 'u':
	  just_user = 1;
	  break;
	case 'G':
	  just_group_list = 1;
	  break;
	case 'v':
	  show_version = 1;
	  break;
	case 'h':
	  usage (0);
	  break;
	case 'c':
	  check_pipe = 1;
	  pipe_fname = optarg;
	  break;
	default:
	  usage (1);
	}
    }

  if (show_version)
    {
      printf ("xispid (X-ISP V.%s%s)\n", Version, PatchLevel);
      exit (0);
    }

  if (show_help)
    usage (0);

  if (check_pipe) {
    struct stat st;
    char *bnp = strstr(pipe_fname, PIPEFNAME);
    int tlen = strlen(PIPEFNAME);

    if (bnp == NULL || strlen(bnp) < tlen) return 1;
    if (strncmp(bnp, PIPEFNAME, tlen)) return 1;
    if (stat(pipe_fname, &st) < 0) return 1;
    if (st.st_mode != 010600 || st.st_gid != getgid()) return 1;
    return 0;
  }

  if (just_user + just_group + just_group_list > 1)
    error (1, 0, "cannot print only user and only group");

  if (just_user + just_group + just_group_list == 0 && (use_real || use_name))
    error (1, 0, "cannot print only names or real IDs in default format");

  if (argc - optind > 1)
    usage (1);

  if (argc - optind == 1)
    {
      struct passwd *pwd = getpwnam (argv[optind]);
      if (pwd == NULL)
	error (1, 0, "%s: No such user", argv[optind]);
      ruid = euid = pwd->pw_uid;
      rgid = egid = pwd->pw_gid;
    }
  else
    {
      euid = geteuid ();
      ruid = getuid ();
      egid = getegid ();
      rgid = getgid ();
    }

  if (just_user)
    print_user (use_real ? ruid : euid);
  else if (just_group)
    print_group (use_real ? rgid : egid);
  else if (just_group_list)
    print_group_list (argv[optind]);
  else
    print_full_info (argv[optind]);
  putchar ('\n');

  return (problems != 0);
}

static void print_user (uid)
     int uid;
{
  struct passwd *pwd = NULL;

  if (use_name)
    {
      pwd = getpwuid (uid);
      if (pwd == NULL)
	problems++;
    }

  if (pwd == NULL)
    printf ("%u", (unsigned) uid);
  else
    printf ("%s", pwd->pw_name);
}

static void print_group (gid)
     int gid;
{
  struct group *grp = NULL;

  if (use_name)
    {
      grp = getgrgid (gid);
      if (grp == NULL)
	problems++;
    }

  if (grp == NULL)
    printf ("%u", (unsigned) gid);
  else
    printf ("%s", grp->gr_name);
}

static void print_group_list (username)
     char *username;
{
  print_group (rgid);
  if (egid != rgid)
    {
      putchar (' ');
      print_group (egid);
    }

  {
    int ngroups;
    gid_t *groups;
    register int i;

    groups = (gid_t *) xmalloc (NGROUPS_MAX * sizeof (gid_t));
    if (username == 0)
      ngroups = getgroups (NGROUPS_MAX, groups);
    else
      ngroups = getugroups (NGROUPS_MAX, groups, username);
    if (ngroups < 0)
      {
	error (0, errno, "cannot get supplemental group list");
	problems++;
	free (groups);
	return;
      }

    for (i = 0; i < ngroups; i++)
      if (groups[i] != rgid && groups[i] != egid)
	{
	  putchar (' ');
	  print_group (groups[i]);
	}
    free (groups);
  }
}

static void print_full_info (username)
     char *username;
{
  struct passwd *pwd;
  struct group *grp;

  printf ("uid=%u", (unsigned) ruid);
  pwd = getpwuid (ruid);
  if (pwd == NULL)
    problems++;
  else
    printf ("(%s)", pwd->pw_name);
  
  printf (" gid=%u", (unsigned) rgid);
  grp = getgrgid (rgid);
  if (grp == NULL)
    problems++;
  else
    printf ("(%s)", grp->gr_name);
  
  if (euid != ruid)
    {
      printf (" euid=%u", (unsigned) euid);
      pwd = getpwuid (euid);
      if (pwd == NULL)
	problems++;
      else
	printf ("(%s)", pwd->pw_name);
    }
  
  if (egid != rgid)
    {
      printf (" egid=%u", (unsigned) egid);
      grp = getgrgid (egid);
      if (grp == NULL)
	problems++;
      else
	printf ("(%s)", grp->gr_name);
    }

  {
    int ngroups;
    gid_t *groups;
    register int i;

    groups = (gid_t *) xmalloc (NGROUPS_MAX * sizeof (gid_t));
    if (username == 0)
      ngroups = getgroups (NGROUPS_MAX, groups);
    else
      ngroups = getugroups (NGROUPS_MAX, groups, username);
    if (ngroups < 0)
      {
	error (0, errno, "cannot get supplemental group list");
	problems++;
	free (groups);
	return;
      }

    if (ngroups > 0)
      fputs (" groups=", stdout);
    for (i = 0; i < ngroups; i++)
      {
	if (i > 0)
	  putchar (',');
	printf ("%u", (unsigned) groups[i]);
	grp = getgrgid (groups[i]);
	if (grp == NULL)
	  problems++;
	else
	  printf ("(%s)", grp->gr_name);
      }
    free (groups);
  }
}

static void usage (status)
     int status;
{
  if (status != 0)
    fprintf (stderr, "Try `%s -h' for more information.\n",
	     program_name);
  else
    {
      printf ("Usage: %s [-gGnruhv] [user name]\n", program_name);
      printf ("   or: %s -c <file name>\n", program_name);
      printf ("\
\n\
  -g,             print only the group ID\n\
  -G,             print only the supplementary groups\n\
  -n,             print a name instead of a number, for -ugG\n\
  -r,             print the real ID instead of effective ID, for -ugG\n\
  -u,             print only the user ID\n\
  -h,             display this help and exit\n\
  -v,             output version information and exit\n\
  -c <file name>  check if the file name starts with %s, it has\n\
                  mode 0010600 and its group is same as caller's group ID\n\
\n\
Without any OPTION, print some useful set of identified information.\n\
", PIPEFNAME);
    }
  exit (status);
}