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