]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/nssov/nssov.c
*** empty log message ***
[openldap] / contrib / slapd-modules / nssov / nssov.c
1 /* nssov.c - nss-ldap overlay for slapd */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 
4  *
5  * Copyright 2008-2010 The OpenLDAP Foundation.
6  * Portions Copyright 2008 by Howard Chu, Symas Corp.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This code references portions of the nss-ldapd package
19  * written by Arthur de Jong. The nss-ldapd code was forked
20  * from the nss-ldap library written by Luke Howard.
21  */
22
23 #include "nssov.h"
24
25 #ifndef SLAPD_OVER_NSSOV
26 #define SLAPD_OVER_NSSOV SLAPD_MOD_DYNAMIC
27 #endif
28
29 #include "../slapd/config.h"    /* not nss-ldapd config.h */
30
31 #include "lutil.h"
32
33 #include <ac/errno.h>
34 #include <ac/unistd.h>
35 #include <fcntl.h>
36 #include <sys/stat.h>
37
38 AttributeDescription *nssov_pam_host_ad;
39 AttributeDescription *nssov_pam_svc_ad;
40
41 /* buffer sizes for I/O */
42 #define READBUFFER_MINSIZE 32
43 #define READBUFFER_MAXSIZE 64
44 #define WRITEBUFFER_MINSIZE 64
45 #define WRITEBUFFER_MAXSIZE 64*1024
46
47 /* Find the given attribute's value in the RDN of the DN */
48 int nssov_find_rdnval(struct berval *dn, AttributeDescription *ad, struct berval *value)
49 {
50         struct berval rdn;
51         char *next;
52
53         BER_BVZERO(value);
54         dnRdn( dn, &rdn );
55         do {
56                 next = ber_bvchr( &rdn, '+' );
57                 if ( rdn.bv_val[ad->ad_cname.bv_len] == '=' &&
58                         !ber_bvcmp( &rdn, &ad->ad_cname )) {
59                         if ( next )
60                                 rdn.bv_len = next - rdn.bv_val;
61                         value->bv_val = rdn.bv_val + ad->ad_cname.bv_len + 1;
62                         value->bv_len = rdn.bv_len - ad->ad_cname.bv_len - 1;
63                         break;
64                 }
65                 if ( !next )
66                         break;
67                 next++;
68                 rdn.bv_len -= next - rdn.bv_val;
69                 rdn.bv_val = next;
70         } while (1);
71 }
72
73 /* create a search filter using a name that requires escaping */
74 int nssov_filter_byname(nssov_mapinfo *mi,int key,struct berval *name,struct berval *buf)
75 {
76         char buf2[1024];
77         struct berval bv2 = {sizeof(buf2),buf2};
78
79         /* escape attribute */
80         if (nssov_escape(name,&bv2))
81                 return -1;
82         /* build filter */
83         if (bv2.bv_len + mi->mi_filter.bv_len + mi->mi_attrs[key].an_desc->ad_cname.bv_len + 6 >
84                 buf->bv_len )
85                 return -1;
86         buf->bv_len = snprintf(buf->bv_val, buf->bv_len, "(&%s(%s=%s))",
87                 mi->mi_filter.bv_val, mi->mi_attrs[key].an_desc->ad_cname.bv_val,
88                 bv2.bv_val );
89         return 0;
90 }
91
92 /* create a search filter using a string converted from an int */
93 int nssov_filter_byid(nssov_mapinfo *mi,int key,struct berval *id,struct berval *buf)
94 {
95         /* build filter */
96         if (id->bv_len + mi->mi_filter.bv_len + mi->mi_attrs[key].an_desc->ad_cname.bv_len + 6 >
97                 buf->bv_len )
98                 return -1;
99         buf->bv_len = snprintf(buf->bv_val, buf->bv_len, "(&%s(%s=%s))",
100                 mi->mi_filter.bv_val, mi->mi_attrs[key].an_desc->ad_cname.bv_val,
101                 id->bv_val );
102         return 0;
103 }
104
105 void get_userpassword(struct berval *attr,struct berval *pw)
106 {
107         int i;
108         /* go over the entries and return the remainder of the value if it
109                  starts with {crypt} or crypt$ */
110         for (i=0;!BER_BVISNULL(&attr[i]);i++)
111         {
112                 if (strncasecmp(attr[i].bv_val,"{crypt}",7)==0) {
113                         pw->bv_val = attr[i].bv_val + 7;
114                         pw->bv_len = attr[i].bv_len - 7;
115                         return;
116                 }
117                 if (strncasecmp(attr[i].bv_val,"crypt$",6)==0) {
118                         pw->bv_val = attr[i].bv_val + 6;
119                         pw->bv_len = attr[i].bv_len - 6;
120                         return;
121                 }
122         }
123         /* just return the first value completely */
124         *pw = *attr;
125         /* TODO: support more password formats e.g. SMD5
126                 (which is $1$ but in a different format)
127                 (any code for this is more than welcome) */
128 }
129
130 /* this writes a single address to the stream */
131 int write_address(TFILE *fp,struct berval *addr)
132 {
133         int32_t tmpint32;
134         struct in_addr ipv4addr;
135         struct in6_addr ipv6addr;
136         /* try to parse the address as IPv4 first, fall back to IPv6 */
137         if (inet_pton(AF_INET,addr->bv_val,&ipv4addr)>0)
138         {
139                 /* write address type */
140                 WRITE_INT32(fp,AF_INET);
141                 /* write the address length */
142                 WRITE_INT32(fp,sizeof(struct in_addr));
143                 /* write the address itself (in network byte order) */
144                 WRITE_TYPE(fp,ipv4addr,struct in_addr);
145         }
146         else if (inet_pton(AF_INET6,addr->bv_val,&ipv6addr)>0)
147         {
148                 /* write address type */
149                 WRITE_INT32(fp,AF_INET6);
150                 /* write the address length */
151                 WRITE_INT32(fp,sizeof(struct in6_addr));
152                 /* write the address itself (in network byte order) */
153                 WRITE_TYPE(fp,ipv6addr,struct in6_addr);
154         }
155         else
156         {
157                 /* failure, log but write simple invalid address
158                          (otherwise the address list is messed up) */
159                 /* TODO: have error message in correct format */
160                 Debug(LDAP_DEBUG_ANY,"nssov: unparseable address: %s\n",addr->bv_val,0,0);
161                 /* write an illegal address type */
162                 WRITE_INT32(fp,-1);
163                 /* write an empty address */
164                 WRITE_INT32(fp,0);
165         }
166         /* we're done */
167         return 0;
168 }
169
170 int read_address(TFILE *fp,char *addr,int *addrlen,int *af)
171 {
172         int32_t tmpint32;
173         int len;
174         /* read address family */
175         READ_INT32(fp,*af);
176         if ((*af!=AF_INET)&&(*af!=AF_INET6))
177         {
178                 Debug(LDAP_DEBUG_ANY,"nssov: incorrect address family specified: %d\n",*af,0,0);
179                 return -1;
180         }
181         /* read address length */
182         READ_INT32(fp,len);
183         if ((len>*addrlen)||(len<=0))
184         {
185                 Debug(LDAP_DEBUG_ANY,"nssov: address length incorrect: %d\n",len,0,0);
186                 return -1;
187         }
188         *addrlen=len;
189         /* read address */
190         READ(fp,addr,len);
191         /* we're done */
192         return 0;
193 }
194
195 int nssov_escape(struct berval *src,struct berval *dst)
196 {
197         size_t pos=0;
198         int i;
199         /* go over all characters in source string */
200         for (i=0;i<src->bv_len;i++)
201         {
202                 /* check if char will fit */
203                 if (pos>=(dst->bv_len-4))
204                         return -1;
205                 /* do escaping for some characters */
206                 switch (src->bv_val[i])
207                 {
208                         case '*':
209                                 strcpy(dst->bv_val+pos,"\\2a");
210                                 pos+=3;
211                                 break;
212                         case '(':
213                                 strcpy(dst->bv_val+pos,"\\28");
214                                 pos+=3;
215                                 break;
216                         case ')':
217                                 strcpy(dst->bv_val+pos,"\\29");
218                                 pos+=3;
219                                 break;
220                         case '\\':
221                                 strcpy(dst->bv_val+pos,"\\5c");
222                                 pos+=3;
223                                 break;
224                         default:
225                                 /* just copy character */
226                                 dst->bv_val[pos++]=src->bv_val[i];
227                                 break;
228                 }
229         }
230         /* terminate destination string */
231         dst->bv_val[pos]='\0';
232         dst->bv_len = pos;
233         return 0;
234 }
235
236 /* read the version information and action from the stream
237    this function returns the read action in location pointer to by action */
238 static int read_header(TFILE *fp,int32_t *action)
239 {
240   int32_t tmpint32;
241   /* read the protocol version */
242   READ_TYPE(fp,tmpint32,int32_t);
243   if (tmpint32 != (int32_t)NSLCD_VERSION)
244   {
245     Debug( LDAP_DEBUG_TRACE,"nssov: wrong nslcd version id (%d)\n",(int)tmpint32,0,0);
246     return -1;
247   }
248   /* read the request type */
249   READ(fp,action,sizeof(int32_t));
250   return 0;
251 }
252
253 /* read a request message, returns <0 in case of errors,
254    this function closes the socket */
255 static void handleconnection(nssov_info *ni,int sock,Operation *op)
256 {
257   TFILE *fp;
258   int32_t action;
259   struct timeval readtimeout,writetimeout;
260   uid_t uid;
261   gid_t gid;
262   char authid[sizeof("gidNumber=4294967295+uidNumber=424967295,cn=peercred,cn=external,cn=auth")];
263
264   /* log connection */
265   if (lutil_getpeereid(sock,&uid,&gid))
266     Debug( LDAP_DEBUG_TRACE,"nssov: connection from unknown client: %s\n",strerror(errno),0,0);
267   else
268     Debug( LDAP_DEBUG_TRACE,"nssov: connection from uid=%d gid=%d\n",
269                       (int)uid,(int)gid,0);
270
271   /* Should do authid mapping too */
272   op->o_dn.bv_len = sprintf(authid,"gidNumber=%d+uidNumber=%d,cn=peercred,cn=external,cn=auth",
273         (int)uid, (int)gid );
274   op->o_dn.bv_val = authid;
275   op->o_ndn = op->o_dn;
276
277   /* set the timeouts */
278   readtimeout.tv_sec=0; /* clients should send their request quickly */
279   readtimeout.tv_usec=500000;
280   writetimeout.tv_sec=5; /* clients could be taking some time to process the results */
281   writetimeout.tv_usec=0;
282   /* create a stream object */
283   if ((fp=tio_fdopen(sock,&readtimeout,&writetimeout,
284                      READBUFFER_MINSIZE,READBUFFER_MAXSIZE,
285                      WRITEBUFFER_MINSIZE,WRITEBUFFER_MAXSIZE))==NULL)
286   {
287     Debug( LDAP_DEBUG_ANY,"nssov: cannot create stream for writing: %s",strerror(errno),0,0);
288     (void)close(sock);
289     return;
290   }
291   /* read request */
292   if (read_header(fp,&action))
293   {
294     (void)tio_close(fp);
295     return;
296   }
297   /* handle request */
298   switch (action)
299   {
300     case NSLCD_ACTION_ALIAS_BYNAME:     (void)nssov_alias_byname(ni,fp,op); break;
301     case NSLCD_ACTION_ALIAS_ALL:        (void)nssov_alias_all(ni,fp,op); break;
302     case NSLCD_ACTION_ETHER_BYNAME:     (void)nssov_ether_byname(ni,fp,op); break;
303     case NSLCD_ACTION_ETHER_BYETHER:    (void)nssov_ether_byether(ni,fp,op); break;
304     case NSLCD_ACTION_ETHER_ALL:        (void)nssov_ether_all(ni,fp,op); break;
305     case NSLCD_ACTION_GROUP_BYNAME:     (void)nssov_group_byname(ni,fp,op); break;
306     case NSLCD_ACTION_GROUP_BYGID:      (void)nssov_group_bygid(ni,fp,op); break;
307     case NSLCD_ACTION_GROUP_BYMEMBER:   (void)nssov_group_bymember(ni,fp,op); break;
308     case NSLCD_ACTION_GROUP_ALL:        (void)nssov_group_all(ni,fp,op); break;
309     case NSLCD_ACTION_HOST_BYNAME:      (void)nssov_host_byname(ni,fp,op); break;
310     case NSLCD_ACTION_HOST_BYADDR:      (void)nssov_host_byaddr(ni,fp,op); break;
311     case NSLCD_ACTION_HOST_ALL:         (void)nssov_host_all(ni,fp,op); break;
312     case NSLCD_ACTION_NETGROUP_BYNAME:  (void)nssov_netgroup_byname(ni,fp,op); break;
313     case NSLCD_ACTION_NETWORK_BYNAME:   (void)nssov_network_byname(ni,fp,op); break;
314     case NSLCD_ACTION_NETWORK_BYADDR:   (void)nssov_network_byaddr(ni,fp,op); break;
315     case NSLCD_ACTION_NETWORK_ALL:      (void)nssov_network_all(ni,fp,op); break;
316     case NSLCD_ACTION_PASSWD_BYNAME:    (void)nssov_passwd_byname(ni,fp,op); break;
317     case NSLCD_ACTION_PASSWD_BYUID:     (void)nssov_passwd_byuid(ni,fp,op); break;
318     case NSLCD_ACTION_PASSWD_ALL:       (void)nssov_passwd_all(ni,fp,op); break;
319     case NSLCD_ACTION_PROTOCOL_BYNAME:  (void)nssov_protocol_byname(ni,fp,op); break;
320     case NSLCD_ACTION_PROTOCOL_BYNUMBER:(void)nssov_protocol_bynumber(ni,fp,op); break;
321     case NSLCD_ACTION_PROTOCOL_ALL:     (void)nssov_protocol_all(ni,fp,op); break;
322     case NSLCD_ACTION_RPC_BYNAME:       (void)nssov_rpc_byname(ni,fp,op); break;
323     case NSLCD_ACTION_RPC_BYNUMBER:     (void)nssov_rpc_bynumber(ni,fp,op); break;
324     case NSLCD_ACTION_RPC_ALL:          (void)nssov_rpc_all(ni,fp,op); break;
325     case NSLCD_ACTION_SERVICE_BYNAME:   (void)nssov_service_byname(ni,fp,op); break;
326     case NSLCD_ACTION_SERVICE_BYNUMBER: (void)nssov_service_bynumber(ni,fp,op); break;
327     case NSLCD_ACTION_SERVICE_ALL:      (void)nssov_service_all(ni,fp,op); break;
328     case NSLCD_ACTION_SHADOW_BYNAME:    if (uid==0) (void)nssov_shadow_byname(ni,fp,op); break;
329     case NSLCD_ACTION_SHADOW_ALL:       if (uid==0) (void)nssov_shadow_all(ni,fp,op); break;
330         case NSLCD_ACTION_PAM_AUTHC:            (void)pam_authc(ni,fp,op); break;
331         case NSLCD_ACTION_PAM_AUTHZ:            (void)pam_authz(ni,fp,op); break;
332         case NSLCD_ACTION_PAM_SESS_O:           if (uid==0) (void)pam_sess_o(ni,fp,op); break;
333         case NSLCD_ACTION_PAM_SESS_C:           if (uid==0) (void)pam_sess_c(ni,fp,op); break;
334         case NSLCD_ACTION_PAM_PWMOD:            (void)pam_pwmod(ni,fp,op); break;
335     default:
336       Debug( LDAP_DEBUG_ANY,"nssov: invalid request id: %d",(int)action,0,0);
337       break;
338   }
339   /* we're done with the request */
340   (void)tio_close(fp);
341   return;
342 }
343
344 /* accept a connection on the socket */
345 static void *acceptconn(void *ctx, void *arg)
346 {
347         nssov_info *ni = arg;
348         Connection conn = {0};
349         OperationBuffer opbuf;
350         Operation *op;
351         int csock;
352
353         if ( slapd_shutdown )
354                 return NULL;
355
356         {
357                 struct sockaddr_storage addr;
358                 socklen_t alen;
359                 int j;
360
361                 /* accept a new connection */
362                 alen=(socklen_t)sizeof(struct sockaddr_storage);
363                 csock=accept(ni->ni_socket,(struct sockaddr *)&addr,&alen);
364                 connection_client_enable(ni->ni_conn);
365                 if (csock<0)
366                 {
367                         if ((errno==EINTR)||(errno==EAGAIN)||(errno==EWOULDBLOCK))
368                         {
369                                 Debug( LDAP_DEBUG_TRACE,"nssov: accept() failed (ignored): %s",strerror(errno),0,0);
370                                 return;
371                         }
372                         Debug( LDAP_DEBUG_ANY,"nssov: accept() failed: %s",strerror(errno),0,0);
373                         return;
374                 }
375                 /* make sure O_NONBLOCK is not inherited */
376                 if ((j=fcntl(csock,F_GETFL,0))<0)
377                 {
378                         Debug( LDAP_DEBUG_ANY,"nssov: fcntl(F_GETFL) failed: %s",strerror(errno),0,0);
379                         if (close(csock))
380                                 Debug( LDAP_DEBUG_ANY,"nssov: problem closing socket: %s",strerror(errno),0,0);
381                         return;
382                 }
383                 if (fcntl(csock,F_SETFL,j&~O_NONBLOCK)<0)
384                 {
385                         Debug( LDAP_DEBUG_ANY,"nssov: fcntl(F_SETFL,~O_NONBLOCK) failed: %s",strerror(errno),0,0);
386                         if (close(csock))
387                                 Debug( LDAP_DEBUG_ANY,"nssov: problem closing socket: %s",strerror(errno),0,0);
388                         return;
389                 }
390         }
391         connection_fake_init( &conn, &opbuf, ctx );
392         op=&opbuf.ob_op;
393         conn.c_ssf = conn.c_transport_ssf = local_ssf;
394         op->o_bd = ni->ni_db;
395         op->o_tag = LDAP_REQ_SEARCH;
396
397         /* handle the connection */
398         handleconnection(ni,csock,op);
399 }
400
401 static slap_verbmasks nss_svcs[] = {
402         { BER_BVC("aliases"), NM_alias },
403         { BER_BVC("ethers"), NM_ether },
404         { BER_BVC("group"), NM_group },
405         { BER_BVC("hosts"), NM_host },
406         { BER_BVC("netgroup"), NM_netgroup },
407         { BER_BVC("networks"), NM_network },
408         { BER_BVC("passwd"), NM_passwd },
409         { BER_BVC("protocols"), NM_protocol },
410         { BER_BVC("rpc"), NM_rpc },
411         { BER_BVC("services"), NM_service },
412         { BER_BVC("shadow"), NM_shadow },
413         { BER_BVNULL, 0 }
414 };
415
416 static slap_verbmasks pam_opts[] = {
417         { BER_BVC("userhost"), NI_PAM_USERHOST },
418         { BER_BVC("userservice"), NI_PAM_USERSVC },
419         { BER_BVC("usergroup"), NI_PAM_USERGRP },
420         { BER_BVC("hostservice"), NI_PAM_HOSTSVC },
421         { BER_BVC("authz2dn"), NI_PAM_SASL2DN },
422         { BER_BVC("uid2dn"), NI_PAM_UID2DN },
423         { BER_BVNULL, 0 }
424 };
425
426 enum {
427         NSS_SSD=1,
428         NSS_MAP,
429         NSS_PAM,
430         NSS_PAMGROUP,
431         NSS_PAMSESS
432 };
433
434 static ConfigDriver nss_cf_gen;
435
436 static ConfigTable nsscfg[] = {
437         { "nssov-ssd", "service> <url", 3, 3, 0, ARG_MAGIC|NSS_SSD,
438                 nss_cf_gen, "(OLcfgCtAt:3.1 NAME 'olcNssSsd' "
439                         "DESC 'URL for searches in a given service' "
440                         "EQUALITY caseIgnoreMatch "
441                         "SYNTAX OMsDirectoryString )", NULL, NULL },
442         { "nssov-map", "service> <orig> <new", 4, 4, 0, ARG_MAGIC|NSS_MAP,
443                 nss_cf_gen, "(OLcfgCtAt:3.2 NAME 'olcNssMap' "
444                         "DESC 'Map <service> lookups of <orig> attr to <new> attr' "
445                         "EQUALITY caseIgnoreMatch "
446                         "SYNTAX OMsDirectoryString )", NULL, NULL },
447         { "nssov-pam", "options", 2, 0, 0, ARG_MAGIC|NSS_PAM,
448                 nss_cf_gen, "(OLcfgCtAt:3.3 NAME 'olcNssPam' "
449                         "DESC 'PAM authentication and authorization options' "
450                         "EQUALITY caseIgnoreMatch "
451                         "SYNTAX OMsDirectoryString )", NULL, NULL },
452         { "nssov-pam-defhost", "hostname", 2, 2, 0, ARG_OFFSET|ARG_BERVAL,
453                 (void *)offsetof(struct nssov_info, ni_pam_defhost),
454                 "(OLcfgCtAt:3.4 NAME 'olcNssPamDefHost' "
455                         "DESC 'Default hostname for service checks' "
456                         "EQUALITY caseIgnoreMatch "
457                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
458         { "nssov-pam-group-dn", "DN", 2, 2, 0, ARG_MAGIC|ARG_DN|NSS_PAMGROUP,
459                 nss_cf_gen, "(OLcfgCtAt:3.5 NAME 'olcNssPamGroupDN' "
460                         "DESC 'DN of group in which membership is required' "
461                         "EQUALITY distinguishedNameMatch "
462                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
463         { "nssov-pam-group-ad", "attr", 2, 2, 0, ARG_OFFSET|ARG_ATDESC,
464                 (void *)offsetof(struct nssov_info, ni_pam_group_ad),
465                 "(OLcfgCtAt:3.6 NAME 'olcNssPamGroupAD' "
466                         "DESC 'Member attribute to use for group check' "
467                         "EQUALITY caseIgnoreMatch "
468                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
469         { "nssov-pam-min-uid", "uid", 2, 2, 0, ARG_OFFSET|ARG_INT,
470                 (void *)offsetof(struct nssov_info, ni_pam_min_uid),
471                 "(OLcfgCtAt:3.7 NAME 'olcNssPamMinUid' "
472                         "DESC 'Minimum UID allowed to login' "
473                         "EQUALITY integerMatch "
474                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
475         { "nssov-pam-max-uid", "uid", 2, 2, 0, ARG_OFFSET|ARG_INT,
476                 (void *)offsetof(struct nssov_info, ni_pam_max_uid),
477                 "(OLcfgCtAt:3.8 NAME 'olcNssPamMaxUid' "
478                         "DESC 'Maximum UID allowed to login' "
479                         "EQUALITY integerMatch "
480                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
481         { "nssov-pam-template-ad", "attr", 2, 2, 0, ARG_OFFSET|ARG_ATDESC,
482                 (void *)offsetof(struct nssov_info, ni_pam_template_ad),
483                 "(OLcfgCtAt:3.9 NAME 'olcNssPamTemplateAD' "
484                         "DESC 'Attribute to use for template login name' "
485                         "EQUALITY caseIgnoreMatch "
486                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
487         { "nssov-pam-template", "name", 2, 2, 0, ARG_OFFSET|ARG_BERVAL,
488                 (void *)offsetof(struct nssov_info, ni_pam_template),
489                 "(OLcfgCtAt:3.10 NAME 'olcNssPamTemplate' "
490                         "DESC 'Default template login name' "
491                         "EQUALITY caseIgnoreMatch "
492                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
493         { "nssov-pam-session", "service", 2, 2, 0, ARG_MAGIC|ARG_BERVAL|NSS_PAMSESS,
494                 nss_cf_gen, "(OLcfgCtAt:3.11 NAME 'olcNssPamSession' "
495                         "DESC 'Services for which sessions will be recorded' "
496                         "EQUALITY caseIgnoreMatch "
497                         "SYNTAX OMsDirectoryString )", NULL, NULL },
498         { NULL, NULL, 0,0,0, ARG_IGNORED }
499 };
500
501 static ConfigOCs nssocs[] = {
502         { "( OLcfgCtOc:3.1 "
503                 "NAME 'olcNssOvConfig' "
504                 "DESC 'NSS lookup configuration' "
505                 "SUP olcOverlayConfig "
506                 "MAY ( olcNssSsd $ olcNssMap $ olcNssPam $ olcNssPamDefHost $ "
507                         "olcNssPamGroupDN $ olcNssPamGroupAD $ "
508                         "olcNssPamMinUid $ olcNssPamMaxUid $ olcNssPamSession $ "
509                         "olcNssPamTemplateAD $ olcNssPamTemplate ) )",
510                 Cft_Overlay, nsscfg },
511         { NULL, 0, NULL }
512 };
513
514 static int
515 nss_cf_gen(ConfigArgs *c)
516 {
517         slap_overinst *on = (slap_overinst *)c->bi;
518         nssov_info *ni = on->on_bi.bi_private;
519         nssov_mapinfo *mi;
520         int i, j, rc = 0;
521         slap_mask_t m;
522
523         if ( c->op == SLAP_CONFIG_EMIT ) {
524                 switch(c->type) {
525                 case NSS_SSD:
526                         rc = 1;
527                         for (i=NM_alias;i<NM_NONE;i++) {
528                                 struct berval scope;
529                                 struct berval ssd;
530                                 struct berval base;
531
532                                 mi = &ni->ni_maps[i];
533
534                                 /* ignore all-default services */
535                                 if ( mi->mi_scope == LDAP_SCOPE_DEFAULT &&
536                                         bvmatch( &mi->mi_filter, &mi->mi_filter0 ) &&
537                                         BER_BVISNULL( &mi->mi_base ))
538                                         continue;
539
540                                 if ( BER_BVISNULL( &mi->mi_base ))
541                                         base = ni->ni_db->be_nsuffix[0];
542                                 else
543                                         base = mi->mi_base;
544                                 ldap_pvt_scope2bv(mi->mi_scope == LDAP_SCOPE_DEFAULT ?
545                                         LDAP_SCOPE_SUBTREE : mi->mi_scope, &scope);
546                                 ssd.bv_len = STRLENOF(" ldap:///???") + nss_svcs[i].word.bv_len +
547                                         base.bv_len + scope.bv_len + mi->mi_filter.bv_len;
548                                 ssd.bv_val = ch_malloc( ssd.bv_len + 1 );
549                                 sprintf(ssd.bv_val, "%s ldap:///%s??%s?%s", nss_svcs[i].word.bv_val,
550                                         base.bv_val, scope.bv_val, mi->mi_filter.bv_val );
551                                 ber_bvarray_add( &c->rvalue_vals, &ssd );
552                                 rc = 0;
553                         }
554                         break;
555                 case NSS_MAP:
556                         rc = 1;
557                         for (i=NM_alias;i<NM_NONE;i++) {
558
559                                 mi = &ni->ni_maps[i];
560                                 for (j=0;!BER_BVISNULL(&mi->mi_attrkeys[j]);j++) {
561                                         if ( ber_bvstrcasecmp(&mi->mi_attrkeys[j],
562                                                 &mi->mi_attrs[j].an_name)) {
563                                                 struct berval map;
564
565                                                 map.bv_len = nss_svcs[i].word.bv_len +
566                                                         mi->mi_attrkeys[j].bv_len +
567                                                         mi->mi_attrs[j].an_desc->ad_cname.bv_len + 2;
568                                                 map.bv_val = ch_malloc(map.bv_len + 1);
569                                                 sprintf(map.bv_val, "%s %s %s", nss_svcs[i].word.bv_val,
570                                                         mi->mi_attrkeys[j].bv_val, mi->mi_attrs[j].an_desc->ad_cname.bv_val );
571                                                 ber_bvarray_add( &c->rvalue_vals, &map );
572                                                 rc = 0;
573                                         }
574                                 }
575                         }
576                         break;
577                 case NSS_PAM:
578                         rc = mask_to_verbs( pam_opts, ni->ni_pam_opts, &c->rvalue_vals );
579                         break;
580                 case NSS_PAMGROUP:
581                         if (!BER_BVISEMPTY( &ni->ni_pam_group_dn )) {
582                                 value_add_one( &c->rvalue_vals, &ni->ni_pam_group_dn );
583                                 value_add_one( &c->rvalue_nvals, &ni->ni_pam_group_dn );
584                         } else {
585                                 rc = 1;
586                         }
587                         break;
588                 case NSS_PAMSESS:
589                         if (ni->ni_pam_sessions) {
590                                 ber_bvarray_dup_x( &c->rvalue_vals, ni->ni_pam_sessions, NULL );
591                         } else {
592                                 rc = 1;
593                         }
594                         break;
595                 }
596                 return rc;
597         } else if ( c->op == LDAP_MOD_DELETE ) {
598                 /* FIXME */
599                 return 1;
600         }
601         switch( c->type ) {
602         case NSS_SSD: {
603                 LDAPURLDesc *lud;
604
605                 i = verb_to_mask(c->argv[1], nss_svcs);
606                 if ( i == NM_NONE )
607                         return 1;
608
609                 mi = &ni->ni_maps[i];
610                 rc = ldap_url_parse(c->argv[2], &lud);
611                 if ( rc )
612                         return 1;
613                 do {
614                         struct berval base;
615                         /* Must be LDAP scheme */
616                         if (strcasecmp(lud->lud_scheme,"ldap")) {
617                                 rc = 1;
618                                 break;
619                         }
620                         /* Host part, attrs, and extensions must be empty */
621                         if (( lud->lud_host && *lud->lud_host ) ||
622                                 lud->lud_attrs || lud->lud_exts ) {
623                                 rc = 1;
624                                 break;
625                         }
626                         ber_str2bv( lud->lud_dn,0,0,&base);
627                         rc = dnNormalize( 0,NULL,NULL,&base,&mi->mi_base,NULL);
628                         if ( rc )
629                                 break;
630                         if ( lud->lud_filter ) {
631                                 /* steal this */
632                                 ber_str2bv( lud->lud_filter,0,0,&mi->mi_filter);
633                                 lud->lud_filter = NULL;
634                         }
635                         mi->mi_scope = lud->lud_scope;
636                 } while(0);
637                 ldap_free_urldesc( lud );
638                 }
639                 break;
640         case NSS_MAP:
641                 i = verb_to_mask(c->argv[1], nss_svcs);
642                 if ( i == NM_NONE )
643                         return 1;
644                 rc = 1;
645                 mi = &ni->ni_maps[i];
646                 for (j=0; !BER_BVISNULL(&mi->mi_attrkeys[j]); j++) {
647                         if (!strcasecmp(c->argv[2],mi->mi_attrkeys[j].bv_val)) {
648                                 AttributeDescription *ad = NULL;
649                                 const char *text;
650                                 rc = slap_str2ad( c->argv[3], &ad, &text);
651                                 if ( rc == 0 ) {
652                                         mi->mi_attrs[j].an_desc = ad;
653                                         mi->mi_attrs[j].an_name = ad->ad_cname;
654                                 }
655                                 break;
656                         }
657                 }
658                 break;
659         case NSS_PAM:
660                 m = ni->ni_pam_opts;
661                 i = verbs_to_mask(c->argc, c->argv, pam_opts, &m);
662                 if (i == 0) {
663                         ni->ni_pam_opts = m;
664                         if ((m & NI_PAM_USERHOST) && !nssov_pam_host_ad) {
665                                 const char *text;
666                                 i = slap_str2ad("host", &nssov_pam_host_ad, &text);
667                                 if (i != LDAP_SUCCESS) {
668                                         snprintf(c->cr_msg, sizeof(c->cr_msg),
669                                                 "nssov: host attr unknown: %s", text);
670                                         Debug(LDAP_DEBUG_ANY,"%s\n",c->cr_msg,0,0);
671                                         rc = 1;
672                                         break;
673                                 }
674                         }
675                         if ((m & (NI_PAM_USERSVC|NI_PAM_HOSTSVC)) && !nssov_pam_svc_ad) {
676                                 const char *text;
677                                 i = slap_str2ad("authorizedService", &nssov_pam_svc_ad, &text);
678                                 if (i != LDAP_SUCCESS) {
679                                         snprintf(c->cr_msg, sizeof(c->cr_msg),
680                                                 "nssov: authorizedService attr unknown: %s", text);
681                                         Debug(LDAP_DEBUG_ANY,"%s\n",c->cr_msg,0,0);
682                                         rc = 1;
683                                         break;
684                                 }
685                         }
686                 } else {
687                         rc = 1;
688                 }
689                 break;
690         case NSS_PAMGROUP:
691                 ni->ni_pam_group_dn = c->value_ndn;
692                 ch_free( c->value_dn.bv_val );
693                 break;
694         case NSS_PAMSESS:
695                 ber_bvarray_add( &ni->ni_pam_sessions, &c->value_bv );
696                 break;
697         }
698         return rc;
699 }
700
701 static int
702 nssov_db_init(
703         BackendDB *be,
704         ConfigReply *cr )
705 {
706         slap_overinst *on = (slap_overinst *)be->bd_info;
707         nssov_info *ni;
708         nssov_mapinfo *mi;
709         int rc;
710
711         rc = nssov_pam_init();
712         if (rc) return rc;
713
714         ni = ch_calloc( 1, sizeof(nssov_info) );
715         on->on_bi.bi_private = ni;
716
717         /* set up map keys */
718         nssov_alias_init(ni);
719         nssov_ether_init(ni);
720         nssov_group_init(ni);
721         nssov_host_init(ni);
722         nssov_netgroup_init(ni);
723         nssov_network_init(ni);
724         nssov_passwd_init(ni);
725         nssov_protocol_init(ni);
726         nssov_rpc_init(ni);
727         nssov_service_init(ni);
728         nssov_shadow_init(ni);
729
730         ni->ni_db = be->bd_self;
731         ni->ni_pam_opts = NI_PAM_UID2DN;
732
733         return 0;
734 }
735
736 static int
737 nssov_db_destroy(
738         BackendDB *be,
739         ConfigReply *cr )
740 {
741 }
742
743 static int
744 nssov_db_open(
745         BackendDB *be,
746         ConfigReply *cr )
747 {
748         slap_overinst *on = (slap_overinst *)be->bd_info;
749         nssov_info *ni = on->on_bi.bi_private;
750         nssov_mapinfo *mi;
751
752         int i, sock;
753         struct sockaddr_un addr;
754
755         /* Set default bases */
756         for (i=0; i<NM_NONE; i++) {
757                 if ( BER_BVISNULL( &ni->ni_maps[i].mi_base )) {
758                         ber_dupbv( &ni->ni_maps[i].mi_base, &be->be_nsuffix[0] );
759                 }
760                 if ( ni->ni_maps[i].mi_scope == LDAP_SCOPE_DEFAULT )
761                         ni->ni_maps[i].mi_scope = LDAP_SCOPE_SUBTREE;
762         }
763         /* validate attribute maps */
764         mi = ni->ni_maps;
765         for ( i=0; i<NM_NONE; i++,mi++) {
766                 const char *text;
767                 int j;
768                 for (j=0; !BER_BVISNULL(&mi->mi_attrkeys[j]); j++) {
769                         /* skip attrs we already validated */
770                         if ( mi->mi_attrs[j].an_desc ) continue;
771                         if ( slap_bv2ad( &mi->mi_attrs[j].an_name,
772                                 &mi->mi_attrs[j].an_desc, &text )) {
773                                 Debug(LDAP_DEBUG_ANY,"nssov: invalid attr \"%s\": %s\n",
774                                         mi->mi_attrs[j].an_name.bv_val, text, 0 );
775                                 return -1;
776                         }
777                 }
778                 BER_BVZERO(&mi->mi_attrs[j].an_name);
779                 mi->mi_attrs[j].an_desc = NULL;
780         }
781
782         /* Find host and authorizedService definitions */
783         if ((ni->ni_pam_opts & NI_PAM_USERHOST) && !nssov_pam_host_ad)
784         {
785                 const char *text;
786                 i = slap_str2ad("host", &nssov_pam_host_ad, &text);
787                 if (i != LDAP_SUCCESS) {
788                         Debug(LDAP_DEBUG_ANY,"nssov: host attr unknown: %s\n",
789                                 text, 0, 0 );
790                         return -1;
791                 }
792         }
793         if ((ni->ni_pam_opts & (NI_PAM_USERSVC|NI_PAM_HOSTSVC)) &&
794                 !nssov_pam_svc_ad)
795         {
796                 const char *text;
797                 i = slap_str2ad("authorizedService", &nssov_pam_svc_ad, &text);
798                 if (i != LDAP_SUCCESS) {
799                         Debug(LDAP_DEBUG_ANY,"nssov: authorizedService attr unknown: %s\n",
800                                 text, 0, 0 );
801                         return -1;
802                 }
803         }
804         if ( slapMode & SLAP_SERVER_MODE ) {
805                 /* make sure /var/run/nslcd exists */
806                 if (mkdir(NSLCD_PATH, (mode_t) 0555)) {
807                         Debug(LDAP_DEBUG_TRACE,"nssov: mkdir(%s) failed (ignored): %s\n",
808                                         NSLCD_PATH,strerror(errno),0);
809                 } else {
810                         Debug(LDAP_DEBUG_TRACE,"nssov: created %s\n",NSLCD_PATH,0,0);
811                 }
812
813                 /* create a socket */
814                 if ( (sock=socket(PF_UNIX,SOCK_STREAM,0))<0 )
815                 {
816                         Debug(LDAP_DEBUG_ANY,"nssov: cannot create socket: %s\n",strerror(errno),0,0);
817                         return -1;
818                 }
819                 /* remove existing named socket */
820                 if (unlink(NSLCD_SOCKET)<0)
821                 {
822                         Debug( LDAP_DEBUG_TRACE,"nssov: unlink() of "NSLCD_SOCKET" failed (ignored): %s\n",
823                                                         strerror(errno),0,0);
824                 }
825                 /* create socket address structure */
826                 memset(&addr,0,sizeof(struct sockaddr_un));
827                 addr.sun_family=AF_UNIX;
828                 strncpy(addr.sun_path,NSLCD_SOCKET,sizeof(addr.sun_path));
829                 addr.sun_path[sizeof(addr.sun_path)-1]='\0';
830                 /* bind to the named socket */
831                 if (bind(sock,(struct sockaddr *)&addr,sizeof(struct sockaddr_un)))
832                 {
833                         Debug( LDAP_DEBUG_ANY,"nssov: bind() to "NSLCD_SOCKET" failed: %s",
834                                                         strerror(errno),0,0);
835                         if (close(sock))
836                                 Debug( LDAP_DEBUG_ANY,"nssov: problem closing socket: %s",strerror(errno),0,0);
837                         return -1;
838                 }
839                 /* close the file descriptor on exit */
840                 if (fcntl(sock,F_SETFD,FD_CLOEXEC)<0)
841                 {
842                         Debug( LDAP_DEBUG_ANY,"nssov: fcntl(F_SETFL,O_NONBLOCK) failed: %s",strerror(errno),0,0);
843                         if (close(sock))
844                                 Debug( LDAP_DEBUG_ANY,"nssov: problem closing socket: %s",strerror(errno),0,0);
845                         return -1;
846                 }
847                 /* set permissions of socket so anybody can do requests */
848                 /* Note: we use chmod() here instead of fchmod() because
849                          fchmod does not work on sockets
850                          http://www.opengroup.org/onlinepubs/009695399/functions/fchmod.html
851                          http://lkml.org/lkml/2005/5/16/11 */
852                 if (chmod(NSLCD_SOCKET,(mode_t)0666))
853                 {
854                         Debug( LDAP_DEBUG_ANY,"nssov: chmod(0666) failed: %s",strerror(errno),0,0);
855                         if (close(sock))
856                                 Debug( LDAP_DEBUG_ANY,"nssov: problem closing socket: %s",strerror(errno),0,0);
857                         return -1;
858                 }
859                 /* start listening for connections */
860                 if (listen(sock,SOMAXCONN)<0)
861                 {
862                         Debug( LDAP_DEBUG_ANY,"nssov: listen() failed: %s",strerror(errno),0,0);
863                         if (close(sock))
864                                 Debug( LDAP_DEBUG_ANY,"nssov: problem closing socket: %s",strerror(errno),0,0);
865                         return -1;
866                 }
867                 ni->ni_socket = sock;
868                 ni->ni_conn = connection_client_setup( sock, acceptconn, ni );
869         }
870
871         return 0;
872 }
873
874 static int
875 nssov_db_close(
876         BackendDB *be,
877         ConfigReply *cr )
878 {
879         slap_overinst *on = (slap_overinst *)be->bd_info;
880         nssov_info *ni = on->on_bi.bi_private;
881
882         /* close socket if it's still in use */
883         if (ni->ni_socket >= 0);
884         {
885                 if (close(ni->ni_socket))
886                         Debug( LDAP_DEBUG_ANY,"problem closing server socket (ignored): %s",strerror(errno),0,0);
887                 ni->ni_socket = -1;
888         }
889         /* remove existing named socket */
890         if (unlink(NSLCD_SOCKET)<0)
891         {
892                 Debug( LDAP_DEBUG_TRACE,"unlink() of "NSLCD_SOCKET" failed (ignored): %s",
893                         strerror(errno),0,0);
894         }
895 }
896
897 static slap_overinst nssov;
898
899 int
900 nssov_initialize( void )
901 {
902         int rc;
903
904         nssov.on_bi.bi_type = "nssov";
905         nssov.on_bi.bi_db_init = nssov_db_init;
906         nssov.on_bi.bi_db_destroy = nssov_db_destroy;
907         nssov.on_bi.bi_db_open = nssov_db_open;
908         nssov.on_bi.bi_db_close = nssov_db_close;
909
910         nssov.on_bi.bi_cf_ocs = nssocs;
911
912         rc = config_register_schema( nsscfg, nssocs );
913         if ( rc ) return rc;
914
915         return overlay_register(&nssov);
916 }
917
918 #if SLAPD_OVER_NSSOV == SLAPD_MOD_DYNAMIC
919 int
920 init_module( int argc, char *argv[] )
921 {
922         return nssov_initialize();
923 }
924 #endif