]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/nssov/pam.c
ITS#8079 nssov: fix compare for usergroup
[openldap] / contrib / slapd-modules / nssov / pam.c
1 /* pam.c - pam processing routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 
4  *
5  * Copyright 2008-2015 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
19 #include "nssov.h"
20 #include "lutil.h"
21
22 static int ppolicy_cid;
23 static AttributeDescription *ad_loginStatus;
24
25 struct paminfo {
26         struct berval uid;
27         struct berval dn;
28         struct berval svc;
29         struct berval pwd;
30         int authz;
31         struct berval msg;
32         int ispwdmgr;
33 };
34
35 static int pam_bindcb(
36         Operation *op, SlapReply *rs)
37 {
38         struct paminfo *pi = op->o_callback->sc_private;
39         LDAPControl *ctrl = ldap_control_find(LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
40                 rs->sr_ctrls, NULL);
41         if (ctrl) {
42                 LDAP *ld;
43                 ber_int_t expire, grace;
44                 LDAPPasswordPolicyError error;
45
46                 ldap_create(&ld);
47                 if (ld) {
48                         int rc = ldap_parse_passwordpolicy_control(ld,ctrl,
49                                 &expire,&grace,&error);
50                         if (rc == LDAP_SUCCESS) {
51                                 if (expire >= 0) {
52                                         char *unit = "seconds";
53                                         if (expire > 60) {
54                                                 expire /= 60;
55                                                 unit = "minutes";
56                                         }
57                                         if (expire > 60) {
58                                                 expire /= 60;
59                                                 unit = "hours";
60                                         }
61                                         if (expire > 24) {
62                                                 expire /= 24;
63                                                 unit = "days";
64                                         }
65 #if 0   /* Who warns about expiration so far in advance? */
66                                         if (expire > 7) {
67                                                 expire /= 7;
68                                                 unit = "weeks";
69                                         }
70                                         if (expire > 4) {
71                                                 expire /= 4;
72                                                 unit = "months";
73                                         }
74                                         if (expire > 12) {
75                                                 expire /= 12;
76                                                 unit = "years";
77                                         }
78 #endif
79                                         pi->msg.bv_len = sprintf(pi->msg.bv_val,
80                                                 "\nWARNING: Password expires in %d %s\n", expire, unit);
81                                 } else if (grace > 0) {
82                                         pi->msg.bv_len = sprintf(pi->msg.bv_val,
83                                                 "Password expired; %d grace logins remaining",
84                                                 grace);
85                                         pi->authz = NSLCD_PAM_NEW_AUTHTOK_REQD;
86                                 } else if (error != PP_noError) {
87                                         ber_str2bv(ldap_passwordpolicy_err2txt(error), 0, 0,
88                                                 &pi->msg);
89                                         switch (error) {
90                                         case PP_passwordExpired:
91                                                 /* report this during authz */
92                                                 rs->sr_err = LDAP_SUCCESS;
93                                                 /* fallthru */
94                                         case PP_changeAfterReset:
95                                                 pi->authz = NSLCD_PAM_NEW_AUTHTOK_REQD;
96                                         }
97                                 }
98                         }
99                         ldap_ld_free(ld,0,NULL,NULL);
100                 }
101         }
102         return LDAP_SUCCESS;
103 }
104
105 static int pam_uid2dn(nssov_info *ni, Operation *op,
106         struct paminfo *pi)
107 {
108         struct berval sdn;
109
110         BER_BVZERO(&pi->dn);
111
112         if (!isvalidusername(&pi->uid)) {
113                 Debug(LDAP_DEBUG_ANY,"nssov_pam_uid2dn(%s): invalid user name\n",
114                         pi->uid.bv_val ? pi->uid.bv_val : "NULL",0,0);
115                 return NSLCD_PAM_USER_UNKNOWN;
116         }
117
118         if (ni->ni_pam_opts & NI_PAM_SASL2DN) {
119                 int hlen = global_host_bv.bv_len;
120
121                 /* cn=<service>+uid=<user>,cn=<host>,cn=pam,cn=auth */
122                 sdn.bv_len = pi->uid.bv_len + pi->svc.bv_len + hlen +
123                         STRLENOF( "cn=+uid=,cn=,cn=pam,cn=auth" );
124                 sdn.bv_val = op->o_tmpalloc( sdn.bv_len + 1, op->o_tmpmemctx );
125                 sprintf(sdn.bv_val, "cn=%s+uid=%s,cn=%s,cn=pam,cn=auth",
126                         pi->svc.bv_val, pi->uid.bv_val, global_host_bv.bv_val);
127                 slap_sasl2dn(op, &sdn, &pi->dn, 0);
128                 op->o_tmpfree( sdn.bv_val, op->o_tmpmemctx );
129         }
130
131         /* If no luck, do a basic uid search */
132         if (BER_BVISEMPTY(&pi->dn) && (ni->ni_pam_opts & NI_PAM_UID2DN)) {
133                 nssov_uid2dn(op, ni, &pi->uid, &pi->dn);
134                 if (!BER_BVISEMPTY(&pi->dn)) {
135                         sdn = pi->dn;
136                         dnNormalize( 0, NULL, NULL, &sdn, &pi->dn, op->o_tmpmemctx );
137                 }
138         }
139         if (BER_BVISEMPTY(&pi->dn)) {
140                 return NSLCD_PAM_USER_UNKNOWN;
141         }
142         return 0;
143 }
144
145 int pam_do_bind(nssov_info *ni,TFILE *fp,Operation *op,
146         struct paminfo *pi)
147 {
148         int rc;
149         slap_callback cb = {0};
150         SlapReply rs = {REP_RESULT};
151
152         pi->msg.bv_val = pi->pwd.bv_val;
153         pi->msg.bv_len = 0;
154         pi->authz = NSLCD_PAM_SUCCESS;
155
156         if (!pi->ispwdmgr) {
157
158                 BER_BVZERO(&pi->dn);
159                 rc = pam_uid2dn(ni, op, pi);
160                 if (rc) goto finish;
161
162                 if (BER_BVISEMPTY(&pi->pwd)) {
163                         rc = NSLCD_PAM_PERM_DENIED;
164                         goto finish;
165                 }
166
167                 /* Should only need to do this once at open time, but there's always
168                  * the possibility that ppolicy will get loaded later.
169                  */
170                 if (!ppolicy_cid) {
171                         rc = slap_find_control_id(LDAP_CONTROL_PASSWORDPOLICYREQUEST,
172                                 &ppolicy_cid);
173                 }
174                 /* of course, 0 is a valid cid, but it won't be ppolicy... */
175                 if (ppolicy_cid) {
176                         op->o_ctrlflag[ppolicy_cid] = SLAP_CONTROL_NONCRITICAL;
177                 }
178         }
179
180         cb.sc_response = pam_bindcb;
181         cb.sc_private = pi;
182         op->o_callback = &cb;
183         op->o_dn.bv_val[0] = 0;
184         op->o_dn.bv_len = 0;
185         op->o_ndn.bv_val[0] = 0;
186         op->o_ndn.bv_len = 0;
187         op->o_tag = LDAP_REQ_BIND;
188         op->o_protocol = LDAP_VERSION3;
189         op->orb_method = LDAP_AUTH_SIMPLE;
190         op->orb_cred = pi->pwd;
191         op->o_req_dn = pi->dn;
192         op->o_req_ndn = pi->dn;
193         slap_op_time( &op->o_time, &op->o_tincr );
194         rc = op->o_bd->be_bind( op, &rs );
195         memset(pi->pwd.bv_val,0,pi->pwd.bv_len);
196         /* quirk: on successful bind, caller has to send result. we need
197          * to make sure callbacks run.
198          */
199         if (rc == LDAP_SUCCESS)
200                 send_ldap_result(op, &rs);
201         switch(rs.sr_err) {
202         case LDAP_SUCCESS: rc = NSLCD_PAM_SUCCESS; break;
203         case LDAP_INVALID_CREDENTIALS: rc = NSLCD_PAM_AUTH_ERR; break;
204         default: rc = NSLCD_PAM_AUTH_ERR; break;
205         }
206 finish:
207         Debug(LDAP_DEBUG_ANY,"pam_do_bind (%s): rc (%d)\n",
208                 pi->dn.bv_val ? pi->dn.bv_val : "NULL", rc, 0);
209         return rc;
210 }
211
212 int pam_authc(nssov_info *ni,TFILE *fp,Operation *op)
213 {
214         int32_t tmpint32;
215         int rc;
216         slap_callback cb = {0};
217         char dnc[1024];
218         char uidc[32];
219         char svcc[256];
220         char pwdc[256];
221         struct berval sdn, dn;
222         struct paminfo pi;
223
224
225         READ_STRING(fp,uidc);
226         pi.uid.bv_val = uidc;
227         pi.uid.bv_len = tmpint32;
228         READ_STRING(fp,dnc);
229         pi.dn.bv_val = dnc;
230         pi.dn.bv_len = tmpint32;
231         READ_STRING(fp,svcc);
232         pi.svc.bv_val = svcc;
233         pi.svc.bv_len = tmpint32;
234         READ_STRING(fp,pwdc);
235         pi.pwd.bv_val = pwdc;
236         pi.pwd.bv_len = tmpint32;
237
238         Debug(LDAP_DEBUG_TRACE,"nssov_pam_authc(%s)\n",
239                         pi.uid.bv_val ? pi.uid.bv_val : "NULL",0,0);
240
241         pi.ispwdmgr = 0;
242
243         /* if service is "passwd" and "nssov-pam-password-prohibit-message */
244         /* is set, deny the auth request */
245         if (!strcmp(svcc, "passwd") &&
246                 !BER_BVISEMPTY(&ni->ni_pam_password_prohibit_message)) {
247                 Debug(LDAP_DEBUG_TRACE,"nssov_pam_authc(): %s (%s)\n",
248                         "password_prohibit_message for passwd",
249                         ni->ni_pam_password_prohibit_message.bv_val,0);
250                 ber_str2bv(ni->ni_pam_password_prohibit_message.bv_val, 0, 0, &pi.msg);
251                 pi.authz = NSLCD_PAM_PERM_DENIED;
252                 rc = NSLCD_PAM_PERM_DENIED;
253                 goto finish;
254         }
255
256         /* if username is null, pwdmgr password preliminary check */
257         if (BER_BVISEMPTY(&pi.uid)) {
258                 if (BER_BVISEMPTY(&ni->ni_pam_pwdmgr_dn)) {
259                         /* pwdmgr dn not configured */
260                         Debug(LDAP_DEBUG_TRACE,"nssov_pam_authc(prelim check): %s\n",
261                                 "pwdmgr dn not configured", 0, 0);
262                         ber_str2bv("pwdmgr dn not configured", 0, 0, &pi.msg);
263                         pi.authz = NSLCD_PAM_PERM_DENIED;
264                         rc = NSLCD_PAM_PERM_DENIED;
265                         goto finish;
266                 } else {
267                         /* use pwdmgr dn */
268                         ber_str2bv(ni->ni_pam_pwdmgr_dn.bv_val, 0, 0, &pi.dn);
269                 }
270
271                 /* use pwdmgr pwd if configured */
272                 if (BER_BVISEMPTY(&pi.pwd)) {
273                         if (BER_BVISEMPTY(&ni->ni_pam_pwdmgr_pwd)) {
274                                 Debug(LDAP_DEBUG_TRACE,"nssov_pam_authc(prelim check): %s\n",
275                                         "no pwdmgr pwd", 0, 0);
276                                 ber_str2bv("pwdmgr pwd not configured", 0, 0, &pi.msg);
277                                 pi.authz = NSLCD_PAM_PERM_DENIED;
278                                 rc = NSLCD_PAM_PERM_DENIED;
279                                 goto finish;
280                         }
281                         /* use configured pwdmgr pwd */
282                         memset((void *) pwdc, 0, 256);
283                         strncpy(pi.pwd.bv_val, ni->ni_pam_pwdmgr_pwd.bv_val,
284                                         ni->ni_pam_pwdmgr_pwd.bv_len);
285                         pi.pwd.bv_len = ni->ni_pam_pwdmgr_pwd.bv_len;
286                 }
287                 pi.ispwdmgr = 1;
288         }
289
290
291         rc = pam_do_bind(ni, fp, op, &pi);
292
293 finish:
294         Debug(LDAP_DEBUG_TRACE,"nssov_pam_authc(%s): rc (%d)\n",
295                 pi.dn.bv_val ? pi.dn.bv_val : "NULL",rc,0);
296         WRITE_INT32(fp,NSLCD_VERSION);
297         WRITE_INT32(fp,NSLCD_ACTION_PAM_AUTHC);
298         WRITE_INT32(fp,NSLCD_RESULT_BEGIN);
299         WRITE_BERVAL(fp,&pi.uid);
300         WRITE_BERVAL(fp,&pi.dn);
301         WRITE_INT32(fp,rc);
302         WRITE_INT32(fp,pi.authz);       /* authz */
303         WRITE_BERVAL(fp,&pi.msg);       /* authzmsg */
304         return 0;
305 }
306
307 static struct berval grpmsg =
308         BER_BVC("Access denied by group check");
309 static struct berval hostmsg =
310         BER_BVC("Access denied for this host");
311 static struct berval svcmsg =
312         BER_BVC("Access denied for this service");
313 static struct berval uidmsg =
314         BER_BVC("Access denied by UID check");
315
316 static int pam_compare_cb(Operation *op, SlapReply *rs)
317 {
318         if (rs->sr_err == LDAP_COMPARE_TRUE)
319                 op->o_callback->sc_private = (void *)1;
320         return LDAP_SUCCESS;
321 }
322
323 int pam_authz(nssov_info *ni,TFILE *fp,Operation *op)
324 {
325         struct berval dn, uid, svc, ruser, rhost, tty;
326         struct berval authzmsg = BER_BVNULL;
327         int32_t tmpint32;
328         char dnc[1024];
329         char uidc[32];
330         char svcc[256];
331         char ruserc[32];
332         char rhostc[256];
333         char ttyc[256];
334         int rc;
335         Entry *e = NULL;
336         Attribute *a;
337         slap_callback cb = {0};
338
339         READ_STRING(fp,uidc);
340         uid.bv_val = uidc;
341         uid.bv_len = tmpint32;
342         READ_STRING(fp,dnc);
343         dn.bv_val = dnc;
344         dn.bv_len = tmpint32;
345         READ_STRING(fp,svcc);
346         svc.bv_val = svcc;
347         svc.bv_len = tmpint32;
348         READ_STRING(fp,ruserc);
349         ruser.bv_val = ruserc;
350         ruser.bv_len = tmpint32;
351         READ_STRING(fp,rhostc);
352         rhost.bv_val = rhostc;
353         rhost.bv_len = tmpint32;
354         READ_STRING(fp,ttyc);
355         tty.bv_val = ttyc;
356         tty.bv_len = tmpint32;
357
358         Debug(LDAP_DEBUG_TRACE,"nssov_pam_authz(%s)\n",
359                         dn.bv_val ? dn.bv_val : "NULL",0,0);
360
361         /* If we didn't do authc, we don't have a DN yet */
362         if (BER_BVISEMPTY(&dn)) {
363                 struct paminfo pi;
364                 pi.uid = uid;
365                 pi.svc = svc;
366
367                 rc = pam_uid2dn(ni, op, &pi);
368                 if (rc) goto finish;
369                 dn = pi.dn;
370         }
371
372         /* See if they have access to the host and service */
373         if ((ni->ni_pam_opts & NI_PAM_HOSTSVC) && nssov_pam_svc_ad) {
374                 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
375                 struct berval hostdn = BER_BVNULL;
376                 struct berval odn = op->o_ndn;
377                 SlapReply rs = {REP_RESULT};
378                 op->o_dn = dn;
379                 op->o_ndn = dn;
380                 {
381                         nssov_mapinfo *mi = &ni->ni_maps[NM_host];
382                         char fbuf[1024];
383                         struct berval filter = {sizeof(fbuf),fbuf};
384                         SlapReply rs2 = {REP_RESULT};
385
386                         /* Lookup the host entry */
387                         nssov_filter_byname(mi,0,&global_host_bv,&filter);
388                         cb.sc_private = &hostdn;
389                         cb.sc_response = nssov_name2dn_cb;
390                         op->o_callback = &cb;
391                         op->o_req_dn = mi->mi_base;
392                         op->o_req_ndn = mi->mi_base;
393                         op->ors_scope = mi->mi_scope;
394                         op->ors_filterstr = filter;
395                         op->ors_filter = str2filter_x(op, filter.bv_val);
396                         op->ors_attrs = slap_anlist_no_attrs;
397                         op->ors_tlimit = SLAP_NO_LIMIT;
398                         op->ors_slimit = 2;
399                         rc = op->o_bd->be_search(op, &rs2);
400                         filter_free_x(op, op->ors_filter, 1);
401
402                         if (BER_BVISEMPTY(&hostdn) &&
403                                 !BER_BVISEMPTY(&ni->ni_pam_defhost)) {
404                                 filter.bv_len = sizeof(fbuf);
405                                 filter.bv_val = fbuf;
406                                 rs_reinit(&rs2, REP_RESULT);
407                                 nssov_filter_byname(mi,0,&ni->ni_pam_defhost,&filter);
408                                 op->ors_filterstr = filter;
409                                 op->ors_filter = str2filter_x(op, filter.bv_val);
410                                 rc = op->o_bd->be_search(op, &rs2);
411                                 filter_free_x(op, op->ors_filter, 1);
412                         }
413
414                         /* no host entry, no default host -> deny */
415                         if (BER_BVISEMPTY(&hostdn)) {
416                                 rc = NSLCD_PAM_PERM_DENIED;
417                                 authzmsg = hostmsg;
418                                 goto finish;
419                         }
420                 }
421
422                 cb.sc_response = pam_compare_cb;
423                 cb.sc_private = NULL;
424                 op->o_tag = LDAP_REQ_COMPARE;
425                 op->o_req_dn = hostdn;
426                 op->o_req_ndn = hostdn;
427                 ava.aa_desc = nssov_pam_svc_ad;
428                 ava.aa_value = svc;
429                 op->orc_ava = &ava;
430                 rc = op->o_bd->be_compare( op, &rs );
431                 if ( cb.sc_private == NULL ) {
432                         authzmsg = svcmsg;
433                         rc = NSLCD_PAM_PERM_DENIED;
434                         goto finish;
435                 }
436                 op->o_dn = odn;
437                 op->o_ndn = odn;
438         }
439
440         /* See if they're a member of the group */
441         if ((ni->ni_pam_opts & NI_PAM_USERGRP) &&
442                 !BER_BVISEMPTY(&ni->ni_pam_group_dn) &&
443                 ni->ni_pam_group_ad) {
444                 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
445                 SlapReply rs = {REP_RESULT};
446                 op->o_callback = &cb;
447                 cb.sc_response = pam_compare_cb;
448                 cb.sc_private = NULL;
449                 op->o_tag = LDAP_REQ_COMPARE;
450                 op->o_req_dn = ni->ni_pam_group_dn;
451                 op->o_req_ndn = ni->ni_pam_group_dn;
452                 ava.aa_desc = ni->ni_pam_group_ad;
453                 ava.aa_value = dn;
454                 op->orc_ava = &ava;
455                 rc = op->o_bd->be_compare( op, &rs );
456                 if ( cb.sc_private == NULL ) {
457                         authzmsg = grpmsg;
458                         rc = NSLCD_PAM_PERM_DENIED;
459                         goto finish;
460                 }
461         }
462
463         /* We need to check the user's entry for these bits */
464         if ((ni->ni_pam_opts & (NI_PAM_USERHOST|NI_PAM_USERSVC)) ||
465                 ni->ni_pam_template_ad ||
466                 ni->ni_pam_min_uid || ni->ni_pam_max_uid ) {
467                 rc = be_entry_get_rw( op, &dn, NULL, NULL, 0, &e );
468                 if (rc != LDAP_SUCCESS) {
469                         rc = NSLCD_PAM_USER_UNKNOWN;
470                         goto finish;
471                 }
472         }
473         if ((ni->ni_pam_opts & NI_PAM_USERHOST) && nssov_pam_host_ad) {
474                 a = attr_find(e->e_attrs, nssov_pam_host_ad);
475                 if (!a || attr_valfind( a,
476                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
477                         SLAP_MR_VALUE_OF_SYNTAX,
478                         &global_host_bv, NULL, op->o_tmpmemctx )) {
479                         rc = NSLCD_PAM_PERM_DENIED;
480                         authzmsg = hostmsg;
481                         goto finish;
482                 }
483         }
484         if ((ni->ni_pam_opts & NI_PAM_USERSVC) && nssov_pam_svc_ad) {
485                 a = attr_find(e->e_attrs, nssov_pam_svc_ad);
486                 if (!a || attr_valfind( a,
487                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
488                         SLAP_MR_VALUE_OF_SYNTAX,
489                         &svc, NULL, op->o_tmpmemctx )) {
490                         rc = NSLCD_PAM_PERM_DENIED;
491                         authzmsg = svcmsg;
492                         goto finish;
493                 }
494         }
495
496 /* from passwd.c */
497 #define UIDN_KEY        2
498
499         if (ni->ni_pam_min_uid || ni->ni_pam_max_uid) {
500                 int id;
501                 char *tmp;
502                 nssov_mapinfo *mi = &ni->ni_maps[NM_passwd];
503                 a = attr_find(e->e_attrs, mi->mi_attrs[UIDN_KEY].an_desc);
504                 if (!a) {
505                         rc = NSLCD_PAM_PERM_DENIED;
506                         authzmsg = uidmsg;
507                         goto finish;
508                 }
509                 id = (int)strtol(a->a_vals[0].bv_val,&tmp,0);
510                 if (a->a_vals[0].bv_val[0] == '\0' || *tmp != '\0') {
511                         rc = NSLCD_PAM_PERM_DENIED;
512                         authzmsg = uidmsg;
513                         goto finish;
514                 }
515                 if ((ni->ni_pam_min_uid && id < ni->ni_pam_min_uid) ||
516                         (ni->ni_pam_max_uid && id > ni->ni_pam_max_uid)) {
517                         rc = NSLCD_PAM_PERM_DENIED;
518                         authzmsg = uidmsg;
519                         goto finish;
520                 }
521         }
522
523         if (ni->ni_pam_template_ad) {
524                 a = attr_find(e->e_attrs, ni->ni_pam_template_ad);
525                 if (a)
526                         uid = a->a_vals[0];
527                 else if (!BER_BVISEMPTY(&ni->ni_pam_template))
528                         uid = ni->ni_pam_template;
529         }
530         rc = NSLCD_PAM_SUCCESS;
531
532 finish:
533         WRITE_INT32(fp,NSLCD_VERSION);
534         WRITE_INT32(fp,NSLCD_ACTION_PAM_AUTHZ);
535         WRITE_INT32(fp,NSLCD_RESULT_BEGIN);
536         WRITE_BERVAL(fp,&uid);
537         WRITE_BERVAL(fp,&dn);
538         WRITE_INT32(fp,rc);
539         WRITE_BERVAL(fp,&authzmsg);
540         if (e) {
541                 be_entry_release_r(op, e);
542         }
543         switch (rc) {
544         case NSLCD_PAM_SUCCESS:
545                 Debug(LDAP_DEBUG_TRACE,"nssov_pam_authz(): success\n", 0,0,0);
546                 break;
547         case NSLCD_PAM_PERM_DENIED:
548                 Debug(LDAP_DEBUG_TRACE,"nssov_pam_authz(): %s\n",
549                         authzmsg.bv_val ? authzmsg.bv_val : "NULL",0,0);
550                 break;
551         default:
552                 Debug(LDAP_DEBUG_TRACE,
553                         "nssov_pam_authz(): permission denied, rc (%d)\n",
554                         rc, 0, 0);
555         }
556         return 0;
557 }
558
559 static int pam_sess(nssov_info *ni,TFILE *fp,Operation *op,int action)
560 {
561         struct berval dn, uid, svc, tty, rhost, ruser;
562         int32_t tmpint32;
563         char dnc[1024];
564         char svcc[256];
565         char uidc[32];
566         char ttyc[32];
567         char rhostc[256];
568         char ruserc[32];
569         slap_callback cb = {0};
570         SlapReply rs = {REP_RESULT};
571         char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE];
572         struct berval timestamp, bv[2], *nbv;
573         time_t stamp;
574         Modifications mod;
575         int rc = 0;
576         int sessionID = -1;
577
578         READ_STRING(fp,uidc);
579         uid.bv_val = uidc;
580         uid.bv_len = tmpint32;
581         READ_STRING(fp,dnc);
582         dn.bv_val = dnc;
583         dn.bv_len = tmpint32;
584         READ_STRING(fp,svcc);
585         svc.bv_val = svcc;
586         svc.bv_len = tmpint32;
587         READ_STRING(fp,ttyc);
588         tty.bv_val = ttyc;
589         tty.bv_len = tmpint32;
590         READ_STRING(fp,rhostc);
591         rhost.bv_val = rhostc;
592         rhost.bv_len = tmpint32;
593         READ_STRING(fp,ruserc);
594         ruser.bv_val = ruserc;
595         ruser.bv_len = tmpint32;
596         READ_INT32(fp,stamp);
597
598         Debug(LDAP_DEBUG_TRACE,"nssov_pam_sess_%c(%s)\n",
599                 action==NSLCD_ACTION_PAM_SESS_O ? 'o' : 'c', dn.bv_val,0);
600
601         if (!dn.bv_len) {
602                 Debug(LDAP_DEBUG_TRACE,"nssov_pam_sess_%c(): %s\n",
603                         action==NSLCD_ACTION_PAM_SESS_O ? 'o' : 'c', "null DN",0);
604                 rc = -1;
605                 goto done;
606         }
607
608         if (!ni->ni_pam_sessions) {
609                 Debug(LDAP_DEBUG_TRACE,"nssov_pam_sess_%c(): %s\n",
610                         action==NSLCD_ACTION_PAM_SESS_O ? 'o' : 'c',
611                         "pam session(s) not configured, ignored",0);
612                 rc = -1;
613                 goto done;
614         }
615
616         {
617                 int i, found=0;
618                 for (i=0; !BER_BVISNULL(&ni->ni_pam_sessions[i]); i++) {
619                         if (ni->ni_pam_sessions[i].bv_len != svc.bv_len)
620                                 continue;
621                         if (!strcasecmp(ni->ni_pam_sessions[i].bv_val, svc.bv_val)) {
622                                 found = 1;
623                                 break;
624                         }
625                 }
626                 if (!found) {
627                         Debug(LDAP_DEBUG_TRACE,
628                                 "nssov_pam_sess_%c(): service(%s) not configured, ignored\n",
629                                 action==NSLCD_ACTION_PAM_SESS_O ? 'o' : 'c',
630                                 svc.bv_val,0);
631                         rc = -1;
632                         goto done;
633                 }
634         }
635
636         slap_op_time( &op->o_time, &op->o_tincr );
637         timestamp.bv_len = sizeof(timebuf);
638         timestamp.bv_val = timebuf;
639         if (action == NSLCD_ACTION_PAM_SESS_O )
640                 stamp = op->o_time;
641         slap_timestamp( &stamp, &timestamp );
642         bv[0].bv_len = timestamp.bv_len + global_host_bv.bv_len + svc.bv_len +
643                 tty.bv_len + ruser.bv_len + rhost.bv_len + STRLENOF("    (@)");
644         bv[0].bv_val = op->o_tmpalloc( bv[0].bv_len+1, op->o_tmpmemctx );
645         sprintf(bv[0].bv_val, "%s %s %s %s (%s@%s)",
646                 timestamp.bv_val, global_host_bv.bv_val, svc.bv_val, tty.bv_val,
647                 ruser.bv_val, rhost.bv_val);
648
649         Debug(LDAP_DEBUG_TRACE, "nssov_pam_sess_%c(): loginStatus (%s) \n",
650                         action==NSLCD_ACTION_PAM_SESS_O ? 'o' : 'c', bv[0].bv_val,0);
651         
652         mod.sml_numvals = 1;
653         mod.sml_values = bv;
654         BER_BVZERO(&bv[1]);
655         attr_normalize( ad_loginStatus, bv, &nbv, op->o_tmpmemctx );
656         mod.sml_nvalues = nbv;
657         mod.sml_desc = ad_loginStatus;
658         mod.sml_op = action == NSLCD_ACTION_PAM_SESS_O ? LDAP_MOD_ADD :
659                 LDAP_MOD_DELETE;
660         mod.sml_flags = SLAP_MOD_INTERNAL;
661         mod.sml_next = NULL;
662
663         cb.sc_response = slap_null_cb;
664         op->o_callback = &cb;
665         op->o_tag = LDAP_REQ_MODIFY;
666         op->o_dn = op->o_bd->be_rootdn;
667         op->o_ndn = op->o_bd->be_rootndn;
668         op->orm_modlist = &mod;
669         op->orm_no_opattrs = 1;
670         op->o_req_dn = dn;
671         op->o_req_ndn = dn;
672         if (op->o_bd->be_modify( op, &rs ) != LDAP_SUCCESS) {
673                 Debug(LDAP_DEBUG_TRACE,
674                         "nssov_pam_sess_%c(): modify op failed\n",
675                         action==NSLCD_ACTION_PAM_SESS_O ? 'o' : 'c',
676                         0,0);
677                 rc = -1;
678         }
679
680         if ( mod.sml_next ) {
681                 slap_mods_free( mod.sml_next, 1 );
682         }
683         ber_bvarray_free_x( nbv, op->o_tmpmemctx );
684
685 done:;
686
687         if (rc == 0) {
688                 Debug(LDAP_DEBUG_TRACE,
689                         "nssov_pam_sess_%c(): success\n",
690                         action==NSLCD_ACTION_PAM_SESS_O ? 'o' : 'c',
691                         0,0);
692                 sessionID = op->o_time;
693         }
694         WRITE_INT32(fp,NSLCD_VERSION);
695         WRITE_INT32(fp,action);
696         WRITE_INT32(fp,NSLCD_RESULT_BEGIN);
697         WRITE_INT32(fp,sessionID);
698         return 0;
699 }
700
701 int pam_sess_o(nssov_info *ni,TFILE *fp,Operation *op)
702 {
703         return pam_sess(ni,fp,op,NSLCD_ACTION_PAM_SESS_O);
704 }
705
706 int pam_sess_c(nssov_info *ni,TFILE *fp,Operation *op)
707 {
708         return pam_sess(ni,fp,op,NSLCD_ACTION_PAM_SESS_C);
709 }
710
711 int pam_pwmod(nssov_info *ni,TFILE *fp,Operation *op)
712 {
713         struct berval npw;
714         int32_t tmpint32;
715         char dnc[1024];
716         char uidc[32];
717         char opwc[256];
718         char npwc[256];
719         char svcc[256];
720         struct paminfo pi;
721         int rc;
722
723         READ_STRING(fp,uidc);
724         pi.uid.bv_val = uidc;
725         pi.uid.bv_len = tmpint32;
726         READ_STRING(fp,dnc);
727         pi.dn.bv_val = dnc;
728         pi.dn.bv_len = tmpint32;
729         READ_STRING(fp,svcc);
730         pi.svc.bv_val = svcc;
731         pi.svc.bv_len = tmpint32;
732         READ_STRING(fp,opwc);
733         pi.pwd.bv_val = opwc;
734         pi.pwd.bv_len = tmpint32;
735         READ_STRING(fp,npwc);
736         npw.bv_val = npwc;
737         npw.bv_len = tmpint32;
738
739         Debug(LDAP_DEBUG_TRACE,"nssov_pam_pwmod(%s), %s\n",
740                 pi.dn.bv_val ? pi.dn.bv_val : "NULL",
741                 pi.uid.bv_val ? pi.uid.bv_val : "NULL" ,0);
742
743         BER_BVZERO(&pi.msg);
744         pi.ispwdmgr = 0;
745
746         /* nssov_pam prohibits password mod */
747         if (!BER_BVISEMPTY(&ni->ni_pam_password_prohibit_message)) {
748                 Debug(LDAP_DEBUG_TRACE,"nssov_pam_pwmod(): %s (%s)\n",
749                         "password_prohibit_message",
750                         ni->ni_pam_password_prohibit_message.bv_val,0);
751                 ber_str2bv(ni->ni_pam_password_prohibit_message.bv_val, 0, 0, &pi.msg);
752                 rc = NSLCD_PAM_PERM_DENIED;
753                 goto done;
754         }
755
756         if (BER_BVISEMPTY(&pi.dn)) {
757                 /* should not be here at all, pam_authc() should have returned */
758                 /* error */
759                 Debug(LDAP_DEBUG_TRACE,"nssov_pam_pwmod(), %s\n",
760                         "prelim checking failed", 0, 0);
761                 ber_str2bv("no pwmod requesting dn", 0, 0, &pi.msg);
762                 rc = NSLCD_PAM_PERM_DENIED;
763                 goto done;
764         }
765
766         if (BER_BVISEMPTY(&ni->ni_pam_pwdmgr_dn)) {
767                 Debug(LDAP_DEBUG_TRACE,"nssov_pam_pwmod(), %s\n",
768                         "pwdmgr not configured", 0, 0);
769                 ber_str2bv("pwdmgr not configured", 0, 0, &pi.msg);
770                 rc = NSLCD_PAM_PERM_DENIED;
771                 goto done;
772         } else if (!ber_bvcmp(&pi.dn, &ni->ni_pam_pwdmgr_dn)) {
773                 /* root user requesting pwmod, convert uid to dn */
774                 pi.ispwdmgr = 1;
775                 rc = pam_uid2dn(ni, op, &pi);
776                 if (rc) {
777                         ber_str2bv("unable to convert uid to dn", 0, 0, &pi.msg);
778                         rc = NSLCD_PAM_PERM_DENIED;
779                         goto done;
780                 }
781         }
782
783         BerElementBuffer berbuf;
784         BerElement *ber = (BerElement *)&berbuf;
785         struct berval bv;
786         SlapReply rs = {REP_RESULT};
787         slap_callback cb = {0};
788
789         ber_init_w_nullc(ber, LBER_USE_DER);
790         ber_printf(ber, "{");
791         if (!BER_BVISEMPTY(&pi.dn))
792                 ber_printf(ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
793                         &pi.dn);
794         /* supply old pwd only when end-user changing pwd */
795         if (!BER_BVISEMPTY(&pi.pwd) && pi.ispwdmgr == 0)
796                 ber_printf(ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_OLD,
797                         &pi.pwd);
798         if (!BER_BVISEMPTY(&npw))
799                 ber_printf(ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW,
800                         &npw);
801         ber_printf(ber, "N}");
802         ber_flatten2(ber, &bv, 0);
803         op->o_tag = LDAP_REQ_EXTENDED;
804         op->ore_reqoid = slap_EXOP_MODIFY_PASSWD;
805         op->ore_reqdata = &bv;
806
807         if (pi.ispwdmgr) {
808                 /* root user changing end-user passwords */
809                 op->o_dn = ni->ni_pam_pwdmgr_dn;
810                 op->o_ndn = ni->ni_pam_pwdmgr_dn;
811         } else {
812                 /* end-user self-pwd-mod */
813                 op->o_dn = pi.dn;
814                 op->o_ndn = pi.dn;
815         }
816         op->o_callback = &cb;
817         op->o_conn->c_authz_backend = op->o_bd;
818         cb.sc_response = slap_null_cb;
819         op->o_bd = frontendDB;
820         rc = op->o_bd->be_extended(op, &rs);
821         if (rs.sr_text)
822                 ber_str2bv(rs.sr_text, 0, 0, &pi.msg);
823         if (rc == LDAP_SUCCESS)
824                 rc = NSLCD_PAM_SUCCESS;
825         else
826                 rc = NSLCD_PAM_PERM_DENIED;
827
828 done:;
829         Debug(LDAP_DEBUG_TRACE,"nssov_pam_pwmod(), rc (%d)\n", rc, 0, 0);
830         WRITE_INT32(fp,NSLCD_VERSION);
831         WRITE_INT32(fp,NSLCD_ACTION_PAM_PWMOD);
832         WRITE_INT32(fp,NSLCD_RESULT_BEGIN);
833         WRITE_BERVAL(fp,&pi.uid);
834         WRITE_BERVAL(fp,&pi.dn);
835         WRITE_INT32(fp,rc);
836         WRITE_BERVAL(fp,&pi.msg);
837         return 0;
838 }
839
840 int nssov_pam_init()
841 {
842         int code = 0;
843         const char *text;
844         if (!ad_loginStatus)
845                 code = slap_str2ad("loginStatus", &ad_loginStatus, &text);
846
847         return code;
848 }