File: nslistusrmap.c

package info (click to toggle)
lfc-postgres 1.7.4.7-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 13,676 kB
  • ctags: 10,779
  • sloc: ansic: 146,136; sh: 13,176; perl: 11,142; python: 5,529; cpp: 5,113; sql: 1,790; makefile: 861; fortran: 113
file content (96 lines) | stat: -rw-r--r-- 2,334 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (C) 2007-2008 by CERN/IT/GD/ITR
 * All rights reserved
 */

#ifndef lint
static char sccsid[] = "@(#)$RCSfile: nslistusrmap.c,v $ $Revision: 1.2 $ $Date: 2008/01/28 15:28:06 $ CERN IT-GD/ITR Jean-Philippe Baud";
#endif /* not lint */

/*	nslistusrmap - query about a given user or list all existing users in virtual uid table */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "Cgetopt.h"
#include "Cns_api.h"
#include "serrno.h"
extern	char	*optarg;
extern	int	optind;
main(argc, argv)
int argc;
char **argv;
{
	int c;
	char *dp;
	int errflg = 0;
	int i;
	static struct Coptions longopts[] = {
		{"uid", REQUIRED_ARGUMENT, 0, OPT_IDMAP_UID},
		{"user", REQUIRED_ARGUMENT, 0, OPT_IDMAP_USER},
		{0, 0, 0, 0}
	};
	int nbentries;
	uid_t uid = -1;
	char *username = NULL;
	struct Cns_userinfo *usr_entries;
	char vidstr[256];

	Copterr = 1;
	Coptind = 1;
	while ((c = Cgetopt_long (argc, argv, "", longopts, NULL)) != EOF) {
		switch (c) {
		case OPT_IDMAP_UID:
			if ((uid = strtol (Coptarg, &dp, 10)) < 0 || *dp != '\0') {
				fprintf (stderr, "invalid uid: %s\n", Coptarg);
				errflg++;
			}
			break;
		case OPT_IDMAP_USER:
			if (strlen (Coptarg) > 255) {
				fprintf (stderr,
				    "user name too long: %s\n", Coptarg);
				errflg++;
			} else
				username = Coptarg;
			break;
		case '?':
			errflg++;
			break;
		default:
			break;
		}
	}
	if (Coptind < argc)
		errflg++;
	if (errflg) {
		fprintf (stderr, "usage: %s %s", argv[0],
		    "[--uid uid] [--user username]\n");
		exit (USERR);
	}

	if (uid != -1) {
		if (Cns_getusrbyuid (uid, vidstr) < 0) {
			fprintf (stderr, "nslistusrmap %d: %s\n", uid,
			    (serrno == ENOENT) ? "No such user" : sstrerror(serrno));
			exit (USERR);
		}
		printf ("%8d %s\n", uid, vidstr);
	} else if (username) {
		if (Cns_getusrbynam (username, &uid) < 0) {
			fprintf (stderr, "nslistusrmap %s: %s\n", username,
			    (serrno == ENOENT) ? "No such user" : sstrerror(serrno));
			exit (USERR);
		}
		printf ("%8d %s\n", uid, username);
	} else {
		if (Cns_getusrmap (&nbentries, &usr_entries) < 0) {
			fprintf (stderr, "nslistusrmap: %s\n", sstrerror(serrno));
			exit (USERR);
		}
		for (i = 0; i < nbentries; i++)
			printf ("%8d %s\n", (usr_entries+i)->userid, (usr_entries+i)->username);
	}
	exit (0);
}