]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/nssov/nss-ldapd/nslcd/alias.c
nss overlay
[openldap] / contrib / slapd-modules / nssov / nss-ldapd / nslcd / alias.c
1 /*
2    alias.c - alias entry lookup routines
3    Parts of this file were part of the nss_ldap library (as ldap-alias.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
32 #include "common.h"
33 #include "log.h"
34 #include "myldap.h"
35 #include "cfg.h"
36 #include "attmap.h"
37
38 /* Vendor-specific attributes and object classes.
39  * (Mainly from Sun.)
40  * ( 1.3.6.1.4.1.42.2.27.1.2.5 NAME 'nisMailAlias' SUP top STRUCTURAL
41  *   DESC 'NIS mail alias'
42  *   MUST cn
43  *   MAY rfc822MailMember )
44  */
45
46 /* the search base for searches */
47 const char *alias_base = NULL;
48
49 /* the search scope for searches */
50 int alias_scope = LDAP_SCOPE_DEFAULT;
51
52 /* the basic search filter for searches */
53 const char *alias_filter = "(objectClass=nisMailAlias)";
54
55 /* the attributes to request with searches */
56 const char *attmap_alias_cn               = "cn";
57 const char *attmap_alias_rfc822MailMember = "rfc822MailMember";
58
59 /* the attribute list to request with searches */
60 static const char *alias_attrs[3];
61
62 /* create a search filter for searching an alias by name,
63    return -1 on errors */
64 static int mkfilter_alias_byname(const char *name,
65                                  char *buffer,size_t buflen)
66 {
67   char buf2[1024];
68   /* escape attribute */
69   if (myldap_escape(name,buf2,sizeof(buf2)))
70     return -1;
71   /* build filter */
72   return mysnprintf(buffer,buflen,
73                     "(&%s(%s=%s))",
74                     alias_filter,
75                     attmap_alias_cn,buf2);
76 }
77
78 static void alias_init(void)
79 {
80   /* set up base */
81   if (alias_base==NULL)
82     alias_base=nslcd_cfg->ldc_base;
83   /* set up scope */
84   if (alias_scope==LDAP_SCOPE_DEFAULT)
85     alias_scope=nslcd_cfg->ldc_scope;
86   /* set up attribute list */
87   alias_attrs[0]=attmap_alias_cn;
88   alias_attrs[1]=attmap_alias_rfc822MailMember;
89   alias_attrs[2]=NULL;
90 }
91
92 static int write_alias(TFILE *fp,MYLDAP_ENTRY *entry,const char *reqalias)
93 {
94   int32_t tmpint32,tmp2int32,tmp3int32;
95   const char *tmparr[2];
96   const char **names,**members;
97   int i;
98   /* get the name of the alias */
99   if (reqalias!=NULL)
100   {
101     names=tmparr;
102     names[0]=reqalias;
103     names[1]=NULL;
104   }
105   else
106   {
107     names=myldap_get_values(entry,attmap_alias_cn);
108     if ((names==NULL)||(names[0]==NULL))
109     {
110       log_log(LOG_WARNING,"alias entry %s does not contain %s value",
111                           myldap_get_dn(entry),attmap_alias_cn);
112       return 0;
113     }
114   }
115   /* get the members of the alias */
116   members=myldap_get_values(entry,attmap_alias_rfc822MailMember);
117   /* for each name, write an entry */
118   for (i=0;names[i]!=NULL;i++)
119   {
120     WRITE_INT32(fp,NSLCD_RESULT_SUCCESS);
121     WRITE_STRING(fp,names[i]);
122     WRITE_STRINGLIST(fp,members);
123   }
124   return 0;
125 }
126
127 NSLCD_HANDLE(
128   alias,byname,
129   char name[256];
130   char filter[1024];
131   READ_STRING_BUF2(fp,name,sizeof(name));,
132   log_log(LOG_DEBUG,"nslcd_alias_byname(%s)",name);,
133   NSLCD_ACTION_ALIAS_BYNAME,
134   mkfilter_alias_byname(name,filter,sizeof(filter)),
135   write_alias(fp,entry,name)
136 )
137
138 NSLCD_HANDLE(
139   alias,all,
140   const char *filter;
141   /* no parameters to read */,
142   log_log(LOG_DEBUG,"nslcd_alias_all()");,
143   NSLCD_ACTION_ALIAS_ALL,
144   (filter=alias_filter,0),
145   write_alias(fp,entry,NULL)
146 )