xref: /illumos-gate/usr/src/cmd/getent/dogetnetgr.c (revision 74e12c43fe52f2c30f36e65a4d0fb0e8dfd7068a)
1*74e12c43SGordon Ross /*
2*74e12c43SGordon Ross  * This file and its contents are supplied under the terms of the
3*74e12c43SGordon Ross  * Common Development and Distribution License ("CDDL"), version 1.0.
4*74e12c43SGordon Ross  * You may only use this file in accordance with the terms of version
5*74e12c43SGordon Ross  * 1.0 of the CDDL.
6*74e12c43SGordon Ross  *
7*74e12c43SGordon Ross  * A full copy of the text of the CDDL should have accompanied this
8*74e12c43SGordon Ross  * source.  A copy of the CDDL is also available via the Internet at
9*74e12c43SGordon Ross  * http://www.illumos.org/license/CDDL.
10*74e12c43SGordon Ross  */
11*74e12c43SGordon Ross 
12*74e12c43SGordon Ross /*
13*74e12c43SGordon Ross  * Copyright 2012 Nexenta Systems, Inc.  All rights reserved.
14*74e12c43SGordon Ross  * Copyright 2021 Planets Communications B.V.
15*74e12c43SGordon Ross  */
16*74e12c43SGordon Ross 
17*74e12c43SGordon Ross #include <stdio.h>
18*74e12c43SGordon Ross #include <stdlib.h>
19*74e12c43SGordon Ross #include <errno.h>
20*74e12c43SGordon Ross #include <netdb.h>
21*74e12c43SGordon Ross #include <string.h>
22*74e12c43SGordon Ross #include "getent.h"
23*74e12c43SGordon Ross 
24*74e12c43SGordon Ross int
dogetnetgr(const char ** list)25*74e12c43SGordon Ross dogetnetgr(const char **list)
26*74e12c43SGordon Ross {
27*74e12c43SGordon Ross 	uint_t cnt;
28*74e12c43SGordon Ross 	char *host, *user, *dom;
29*74e12c43SGordon Ross 	const char *host_filter, *user_filter, *dom_filter;
30*74e12c43SGordon Ross 	int rc = EXC_SUCCESS;
31*74e12c43SGordon Ross 
32*74e12c43SGordon Ross 	if (list == NULL || *list == NULL)
33*74e12c43SGordon Ross 		return (EXC_ENUM_NOT_SUPPORTED);
34*74e12c43SGordon Ross 
35*74e12c43SGordon Ross 	/*
36*74e12c43SGordon Ross 	 * Count the arguments given.
37*74e12c43SGordon Ross 	 */
38*74e12c43SGordon Ross 	cnt = 0;
39*74e12c43SGordon Ross 	while (list[cnt] != NULL)
40*74e12c43SGordon Ross 		cnt++;
41*74e12c43SGordon Ross 
42*74e12c43SGordon Ross 	switch (cnt) {
43*74e12c43SGordon Ross 	case 1:
44*74e12c43SGordon Ross 		if (setnetgrent(list[0]) != 0)
45*74e12c43SGordon Ross 			return (EXC_ERROR);
46*74e12c43SGordon Ross 		printf("%s", list[0]);
47*74e12c43SGordon Ross 		while (getnetgrent(&host, &user, &dom) != 0) {
48*74e12c43SGordon Ross 			printf(" (%s,%s,%s)",
49*74e12c43SGordon Ross 			    (host) ? host : "",
50*74e12c43SGordon Ross 			    (user) ? user : "",
51*74e12c43SGordon Ross 			    (dom)  ? dom  : "");
52*74e12c43SGordon Ross 		}
53*74e12c43SGordon Ross 		printf("\n");
54*74e12c43SGordon Ross 		break;
55*74e12c43SGordon Ross 	case 4:
56*74e12c43SGordon Ross 		host_filter = (strcmp(list[1], "*") == 0) ? NULL : list[1];
57*74e12c43SGordon Ross 		user_filter = (strcmp(list[2], "*") == 0) ? NULL : list[2];
58*74e12c43SGordon Ross 		dom_filter = (strcmp(list[3], "*") == 0) ? NULL : list[3];
59*74e12c43SGordon Ross 		printf("%-21s (%s,%s,%s) = %d\n", list[0],
60*74e12c43SGordon Ross 		    (host_filter) ? host_filter : "",
61*74e12c43SGordon Ross 		    (user_filter) ? user_filter : "",
62*74e12c43SGordon Ross 		    (dom_filter)  ? dom_filter  : "",
63*74e12c43SGordon Ross 		    innetgr(list[0], host_filter, user_filter, dom_filter));
64*74e12c43SGordon Ross 		break;
65*74e12c43SGordon Ross 	default:
66*74e12c43SGordon Ross 		rc = EXC_SYNTAX;
67*74e12c43SGordon Ross 		break;
68*74e12c43SGordon Ross 	}
69*74e12c43SGordon Ross 
70*74e12c43SGordon Ross 	return (rc);
71*74e12c43SGordon Ross }
72