xref: /original-bsd/usr.sbin/amd/amd/info_nis.c (revision 331bfa8d)
1 /*
2  * $Id: info_nis.c,v 5.2 90/06/23 22:19:32 jsp Rel $
3  *
4  * Copyright (c) 1989 Jan-Simon Pendry
5  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
6  * Copyright (c) 1989 The Regents of the University of California.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Jan-Simon Pendry at Imperial College, London.
11  *
12  * %sccs.include.redist.c%
13  *
14  *	@(#)info_nis.c	5.1 (Berkeley) 06/29/90
15  */
16 
17 /*
18  * Get info from NIS map
19  */
20 
21 #include "am.h"
22 
23 #ifdef HAS_NIS_MAPS
24 #include <rpcsvc/yp_prot.h>
25 #include <rpcsvc/ypclnt.h>
26 
27 /*
28  * Figure out the nis domain name
29  */
30 static int determine_nis_domain(P_void)
31 {
32 static	 char default_domain[YPMAXDOMAIN];
33 
34 	if (getdomainname(default_domain, sizeof(default_domain)) < 0) {
35 		plog(XLOG_ERROR, "getdomainname: %m");
36 		return EIO;
37 	}
38 
39 	domain = default_domain;
40 	if (!*domain) {
41 		plog(XLOG_ERROR, "YP domain name is not set");
42 		return ENOENT;
43 	}
44 
45 	return 0;
46 }
47 
48 /*
49  * Try to locate a key using NIS.
50  * Modify time is ignored in NIS - XXX
51  */
52 int nis_search P((mnt_map *m, char *map, char *key, char **val, time_t *tp));
53 int nis_search(m, map, key, val, tp)
54 mnt_map *m;
55 char *map;
56 char *key;
57 char **val;
58 time_t *tp;
59 {
60 	int outlen;
61 	int res;
62 
63 	if (!domain) {
64 		int error = determine_nis_domain();
65 		if (error)
66 			return error;
67 	}
68 
69 	res = yp_match(domain, map, key, strlen(key), val, &outlen);
70 
71 	/*
72 	 * Do something interesting with the return code
73 	 */
74 	switch (res) {
75 	case 0:
76 		return 0;
77 
78 	case YPERR_KEY:
79 		return ENOENT;
80 
81 	default:
82 		plog(XLOG_ERROR, "%s: %s", map, yperr_string(res));
83 		return EIO;
84 	}
85 }
86 
87 int nis_init P((char *map));
88 int nis_init(map)
89 char *map;
90 {
91 	char *name = 0;
92 
93 	if (!domain) {
94 		int error = determine_nis_domain();
95 		if (error)
96 			return error;
97 	}
98 
99 	/*
100 	 * To see if the map exists, try to find
101 	 * a master for it.
102 	 */
103 	if (yp_master(domain, map, &name))
104 		return ENOENT;
105 	free(name);
106 	return 0;
107 }
108 #endif /* HAS_NIS_MAPS */
109