]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/nssov/nss-ldapd/nslcd/network.c
nss overlay
[openldap] / contrib / slapd-modules / nssov / nss-ldapd / nslcd / network.c
1 /*
2    network.c - network address entry lookup routines
3    Parts of this file were part of the nss_ldap library (as ldap-network.c)
4    which has been forked into the nss-ldapd library.
5
6    Copyright (C) 1997-2005 Luke Howard
7    Copyright (C) 2006 West Consulting
8    Copyright (C) 2006, 2007 Arthur de Jong
9
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 2.1 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301 USA
24 */
25
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
34
35 #include "common.h"
36 #include "log.h"
37 #include "myldap.h"
38 #include "cfg.h"
39 #include "attmap.h"
40
41 /* ( nisSchema.2.7 NAME 'ipNetwork' SUP top STRUCTURAL
42  *   DESC 'Abstraction of a network. The distinguished value of
43  *   MUST ( cn $ ipNetworkNumber )
44  *   MAY ( ipNetmaskNumber $ l $ description $ manager ) )
45  */
46
47 /* the search base for searches */
48 const char *network_base = NULL;
49
50 /* the search scope for searches */
51 int network_scope = LDAP_SCOPE_DEFAULT;
52
53 /* the basic search filter for searches */
54 const char *network_filter = "(objectClass=ipNetwork)";
55
56 /* the attributes used in searches */
57 const char *attmap_network_cn              = "cn";
58 const char *attmap_network_ipNetworkNumber = "ipNetworkNumber";
59 /*const char *attmap_network_ipNetmaskNumber = "ipNetmaskNumber"; */
60
61 /* the attribute list to request with searches */
62 static const char *network_attrs[3];
63
64 /* create a search filter for searching a network entry
65    by name, return -1 on errors */
66 static int mkfilter_network_byname(const char *name,
67                                    char *buffer,size_t buflen)
68 {
69   char buf2[1024];
70   /* escape attribute */
71   if (myldap_escape(name,buf2,sizeof(buf2)))
72     return -1;
73   /* build filter */
74   return mysnprintf(buffer,buflen,
75                     "(&%s(%s=%s))",
76                     network_filter,
77                     attmap_network_cn,buf2);
78 }
79
80 static int mkfilter_network_byaddr(const char *name,
81                                    char *buffer,size_t buflen)
82 {
83   char buf2[1024];
84   /* escape attribute */
85   if (myldap_escape(name,buf2,sizeof(buf2)))
86     return -1;
87   /* build filter */
88   return mysnprintf(buffer,buflen,
89                     "(&%s(%s=%s))",
90                     network_filter,
91                     attmap_network_ipNetworkNumber,buf2);
92 }
93
94 static void network_init(void)
95 {
96   /* set up base */
97   if (network_base==NULL)
98     network_base=nslcd_cfg->ldc_base;
99   /* set up scope */
100   if (network_scope==LDAP_SCOPE_DEFAULT)
101     network_scope=nslcd_cfg->ldc_scope;
102   /* set up attribute list */
103   network_attrs[0]=attmap_network_cn;
104   network_attrs[1]=attmap_network_ipNetworkNumber;
105   network_attrs[2]=NULL;
106 }
107
108 /* write a single network entry to the stream */
109 static int write_network(TFILE *fp,MYLDAP_ENTRY *entry)
110 {
111   int32_t tmpint32,tmp2int32,tmp3int32;
112   int numaddr,i;
113   const char *networkname;
114   const char **networknames;
115   const char **addresses;
116   /* get the most canonical name */
117   networkname=myldap_get_rdn_value(entry,attmap_network_cn);
118   /* get the other names for the network */
119   networknames=myldap_get_values(entry,attmap_network_cn);
120   if ((networknames==NULL)||(networknames[0]==NULL))
121   {
122     log_log(LOG_WARNING,"network entry %s does not contain %s value",
123                         myldap_get_dn(entry),attmap_network_cn);
124     return 0;
125   }
126   /* if the networkname is not yet found, get the first entry from networknames */
127   if (networkname==NULL)
128     networkname=networknames[0];
129   /* get the addresses */
130   addresses=myldap_get_values(entry,attmap_network_ipNetworkNumber);
131   if ((addresses==NULL)||(addresses[0]==NULL))
132   {
133     log_log(LOG_WARNING,"network entry %s does not contain %s value",
134                         myldap_get_dn(entry),attmap_network_ipNetworkNumber);
135     return 0;
136   }
137   /* write the entry */
138   WRITE_INT32(fp,NSLCD_RESULT_SUCCESS);
139   WRITE_STRING(fp,networkname);
140   WRITE_STRINGLIST_EXCEPT(fp,networknames,networkname);
141   for (numaddr=0;addresses[numaddr]!=NULL;numaddr++)
142     /*noting*/ ;
143   WRITE_INT32(fp,numaddr);
144   for (i=0;i<numaddr;i++)
145   {
146     WRITE_ADDRESS(fp,addresses[i]);
147   }
148   return 0;
149 }
150
151 NSLCD_HANDLE(
152   network,byname,
153   char name[256];
154   char filter[1024];
155   READ_STRING_BUF2(fp,name,sizeof(name));,
156   log_log(LOG_DEBUG,"nslcd_network_byname(%s)",name);,
157   NSLCD_ACTION_NETWORK_BYNAME,
158   mkfilter_network_byname(name,filter,sizeof(filter)),
159   write_network(fp,entry)
160 )
161
162 NSLCD_HANDLE(
163   network,byaddr,
164   int af;
165   char addr[64];
166   int len=sizeof(addr);
167   char name[1024];
168   char filter[1024];
169   READ_ADDRESS(fp,addr,len,af);
170   /* translate the address to a string */
171   if (inet_ntop(af,addr,name,sizeof(name))==NULL)
172   {
173     log_log(LOG_WARNING,"unable to convert address to string");
174     return -1;
175   },
176   log_log(LOG_DEBUG,"nslcd_network_byaddr(%s)",name);,
177   NSLCD_ACTION_NETWORK_BYADDR,
178   mkfilter_network_byaddr(name,filter,sizeof(filter)),
179   write_network(fp,entry)
180 )
181
182 NSLCD_HANDLE(
183   network,all,
184   const char *filter;
185   /* no parameters to read */,
186   log_log(LOG_DEBUG,"nslcd_network_all()");,
187   NSLCD_ACTION_NETWORK_ALL,
188   (filter=network_filter,0),
189   write_network(fp,entry)
190 )