1 /* $NetBSD: host.c,v 1.3 2021/08/14 16:14:52 christos Exp $ */
2
3 /* host.c - host lookup routines */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 *
7 * Copyright 2008-2021 The OpenLDAP Foundation.
8 * Portions Copyright 2008 by Howard Chu, Symas Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted only as authorized by the OpenLDAP
13 * Public License.
14 *
15 * A copy of this license is available in the file LICENSE in the
16 * top-level directory of the distribution or, alternatively, at
17 * <http://www.OpenLDAP.org/license.html>.
18 */
19 /* ACKNOWLEDGEMENTS:
20 * This code references portions of the nss-ldapd package
21 * written by Arthur de Jong. The nss-ldapd code was forked
22 * from the nss-ldap library written by Luke Howard.
23 */
24
25 #include "nssov.h"
26
27 /* ( nisSchema.2.6 NAME 'ipHost' SUP top AUXILIARY
28 * DESC 'Abstraction of a host, an IP device. The distinguished
29 * value of the cn attribute denotes the host's canonical
30 * name. Device SHOULD be used as a structural class'
31 * MUST ( cn $ ipHostNumber )
32 * MAY ( l $ description $ manager ) )
33 */
34
35 /* the basic search filter for searches */
36 static struct berval host_filter = BER_BVC("(objectClass=ipHost)");
37
38 /* the attributes to request with searches */
39 static struct berval host_keys[] = {
40 BER_BVC("cn"),
41 BER_BVC("ipHostNumber"),
42 BER_BVNULL
43 };
44
45 NSSOV_INIT(host)
46
47 NSSOV_CBPRIV(host,
48 char buf[1024];
49 struct berval name;
50 struct berval addr;);
51
52 /* write a single host entry to the stream */
write_host(nssov_host_cbp * cbp,Entry * entry)53 static int write_host(nssov_host_cbp *cbp,Entry *entry)
54 {
55 int32_t tmpint32;
56 int numaddr,i,numname,dupname;
57 struct berval name,*names,*addrs;
58 Attribute *a;
59
60 /* get the most canonical name */
61 nssov_find_rdnval( &entry->e_nname, cbp->mi->mi_attrs[0].an_desc, &name );
62 /* get the other names for the host */
63 a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[0].an_desc );
64 if ( !a || !a->a_vals )
65 {
66 Debug(LDAP_DEBUG_ANY,"host entry %s does not contain %s value\n",
67 entry->e_name.bv_val, cbp->mi->mi_attrs[0].an_desc->ad_cname.bv_val );
68 return 0;
69 }
70 names = a->a_vals;
71 numname = a->a_numvals;
72 /* if the name is not yet found, get the first entry from names */
73 if (BER_BVISNULL(&name)) {
74 name=names[0];
75 dupname = 0;
76 } else {
77 dupname = -1;
78 for (i=0; i<numname; i++) {
79 if ( bvmatch(&name, &a->a_nvals[i])) {
80 dupname = i;
81 break;
82 }
83 }
84 }
85 /* get the addresses */
86 a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[1].an_desc );
87 if ( !a || !a->a_vals )
88 {
89 Debug(LDAP_DEBUG_ANY,"host entry %s does not contain %s value\n",
90 entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val );
91 return 0;
92 }
93 addrs = a->a_vals;
94 numaddr = a->a_numvals;
95 /* write the entry */
96 WRITE_INT32(cbp->fp,NSLCD_RESULT_BEGIN);
97 WRITE_BERVAL(cbp->fp,&name);
98 if ( dupname >= 0 ) {
99 WRITE_INT32(cbp->fp,numname-1);
100 } else {
101 WRITE_INT32(cbp->fp,numname);
102 }
103 for (i=0;i<numname;i++) {
104 if (i == dupname) continue;
105 WRITE_BERVAL(cbp->fp,&names[i]);
106 }
107 WRITE_INT32(cbp->fp,numaddr);
108 for (i=0;i<numaddr;i++)
109 {
110 WRITE_ADDRESS(cbp->fp,&addrs[i]);
111 }
112 return 0;
113 }
114
115 NSSOV_CB(host)
116
117 NSSOV_HANDLE(
118 host,byname,
119 char fbuf[1024];
120 struct berval filter = {sizeof(fbuf)};
121 filter.bv_val = fbuf;
122 BER_BVZERO(&cbp.addr);
123 READ_STRING(fp,cbp.buf);
124 cbp.name.bv_len = tmpint32;
125 cbp.name.bv_val = cbp.buf;,
126 Debug(LDAP_DEBUG_TRACE,"nssov_host_byname(%s)\n",cbp.name.bv_val);,
127 NSLCD_ACTION_HOST_BYNAME,
128 nssov_filter_byname(cbp.mi,0,&cbp.name,&filter)
129 )
130
131 NSSOV_HANDLE(
132 host,byaddr,
133 int af;
134 char addr[64];
135 int len=sizeof(addr);
136 char fbuf[1024];
137 struct berval filter = {sizeof(fbuf)};
138 filter.bv_val = fbuf;
139 BER_BVZERO(&cbp.name);
140 READ_ADDRESS(fp,addr,len,af);
141 /* translate the address to a string */
142 if (inet_ntop(af,addr,cbp.buf,sizeof(cbp.buf))==NULL)
143 {
144 Debug(LDAP_DEBUG_ANY,"nssov: unable to convert address to string\n");
145 return -1;
146 }
147 cbp.addr.bv_val = cbp.buf;
148 cbp.addr.bv_len = strlen(cbp.buf);,
149 Debug(LDAP_DEBUG_TRACE,"nssov_host_byaddr(%s)\n",cbp.addr.bv_val);,
150 NSLCD_ACTION_HOST_BYADDR,
151 nssov_filter_byid(cbp.mi,1,&cbp.addr,&filter)
152 )
153
154 NSSOV_HANDLE(
155 host,all,
156 struct berval filter;
157 /* no parameters to read */
158 BER_BVZERO(&cbp.name);
159 BER_BVZERO(&cbp.addr);,
160 Debug(LDAP_DEBUG_TRACE,"nssov_host_all()\n");,
161 NSLCD_ACTION_HOST_ALL,
162 (filter=cbp.mi->mi_filter,0)
163 )
164