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.
6 Copyright (C) 1997-2005 Luke Howard
7 Copyright (C) 2006 West Consulting
8 Copyright (C) 2006, 2007 Arthur de Jong
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.
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.
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
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
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 ) )
47 /* the search base for searches */
48 const char *network_base = NULL;
50 /* the search scope for searches */
51 int network_scope = LDAP_SCOPE_DEFAULT;
53 /* the basic search filter for searches */
54 const char *network_filter = "(objectClass=ipNetwork)";
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"; */
61 /* the attribute list to request with searches */
62 static const char *network_attrs[3];
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)
70 /* escape attribute */
71 if (myldap_escape(name,buf2,sizeof(buf2)))
74 return mysnprintf(buffer,buflen,
77 attmap_network_cn,buf2);
80 static int mkfilter_network_byaddr(const char *name,
81 char *buffer,size_t buflen)
84 /* escape attribute */
85 if (myldap_escape(name,buf2,sizeof(buf2)))
88 return mysnprintf(buffer,buflen,
91 attmap_network_ipNetworkNumber,buf2);
94 static void network_init(void)
97 if (network_base==NULL)
98 network_base=nslcd_cfg->ldc_base;
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;
108 /* write a single network entry to the stream */
109 static int write_network(TFILE *fp,MYLDAP_ENTRY *entry)
111 int32_t tmpint32,tmp2int32,tmp3int32;
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))
122 log_log(LOG_WARNING,"network entry %s does not contain %s value",
123 myldap_get_dn(entry),attmap_network_cn);
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))
133 log_log(LOG_WARNING,"network entry %s does not contain %s value",
134 myldap_get_dn(entry),attmap_network_ipNetworkNumber);
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++)
143 WRITE_INT32(fp,numaddr);
144 for (i=0;i<numaddr;i++)
146 WRITE_ADDRESS(fp,addresses[i]);
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)
166 int len=sizeof(addr);
169 READ_ADDRESS(fp,addr,len,af);
170 /* translate the address to a string */
171 if (inet_ntop(af,addr,name,sizeof(name))==NULL)
173 log_log(LOG_WARNING,"unable to convert address to string");
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)
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)