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