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