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