1 /* pam.c - pam processing routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2008-2009 The OpenLDAP Foundation.
6 * Portions Copyright 2008 by Howard Chu, Symas Corp.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
21 static int ppolicy_cid;
22 static AttributeDescription *ad_loginStatus;
33 static int pam_bindcb(
34 Operation *op, SlapReply *rs)
36 struct paminfo *pi = op->o_callback->sc_private;
37 LDAPControl *ctrl = ldap_control_find(LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
41 ber_int_t expire, grace;
42 LDAPPasswordPolicyError error;
46 int rc = ldap_parse_passwordpolicy_control(ld,ctrl,
47 &expire,&grace,&error);
48 if (rc == LDAP_SUCCESS) {
50 char *unit = "seconds";
63 #if 0 /* Who warns about expiration so far in advance? */
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",
83 pi->authz = NSLCD_PAM_NEW_AUTHTOK_REQD;
84 } else if (error != PP_noError) {
85 ber_str2bv(ldap_passwordpolicy_err2txt(error), 0, 0,
88 case PP_passwordExpired:
89 /* report this during authz */
90 rs->sr_err = LDAP_SUCCESS;
92 case PP_changeAfterReset:
93 pi->authz = NSLCD_PAM_NEW_AUTHTOK_REQD;
97 ldap_ld_free(ld,0,NULL,NULL);
103 static int pam_uid2dn(nssov_info *ni, Operation *op,
110 if (!isvalidusername(&pi->uid)) {
111 Debug(LDAP_DEBUG_ANY,"nssov_pam_uid2dn(%s): invalid user name\n",
113 return NSLCD_PAM_USER_UNKNOWN;
116 if (ni->ni_pam_opts & NI_PAM_SASL2DN) {
117 int hlen = global_host_bv.bv_len;
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 );
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)) {
134 dnNormalize( 0, NULL, NULL, &sdn, &pi->dn, op->o_tmpmemctx );
137 if (BER_BVISEMPTY(&pi->dn)) {
138 return NSLCD_PAM_USER_UNKNOWN;
143 int pam_do_bind(nssov_info *ni,TFILE *fp,Operation *op,
147 slap_callback cb = {0};
148 SlapReply rs = {REP_RESULT};
150 pi->msg.bv_val = pi->pwd.bv_val;
152 pi->authz = NSLCD_PAM_SUCCESS;
155 rc = pam_uid2dn(ni, op, pi);
158 if (BER_BVISEMPTY(&pi->pwd)) {
159 rc = NSLCD_PAM_IGNORE;
163 /* Should only need to do this once at open time, but there's always
164 * the possibility that ppolicy will get loaded later.
167 rc = slap_find_control_id(LDAP_CONTROL_PASSWORDPOLICYREQUEST,
170 /* of course, 0 is a valid cid, but it won't be ppolicy... */
172 op->o_ctrlflag[ppolicy_cid] = SLAP_CONTROL_NONCRITICAL;
174 cb.sc_response = pam_bindcb;
176 op->o_callback = &cb;
177 op->o_dn.bv_val[0] = 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.
193 if (rc == LDAP_SUCCESS)
194 send_ldap_result(op, &rs);
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;
204 int pam_authc(nssov_info *ni,TFILE *fp,Operation *op)
208 slap_callback cb = {0};
209 SlapReply rs = {REP_RESULT};
214 struct berval sdn, dn;
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));
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;
231 Debug(LDAP_DEBUG_TRACE,"nssov_pam_authc(%s)\n",pi.uid.bv_val,0,0);
233 rc = pam_do_bind(ni, fp, op, &pi);
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);
242 WRITE_INT32(fp,pi.authz); /* authz */
243 WRITE_BERVAL(fp,&pi.msg); /* authzmsg */
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");
256 static int pam_compare_cb(Operation *op, SlapReply *rs)
258 if (rs->sr_err == LDAP_COMPARE_TRUE)
259 op->o_callback->sc_private = (void *)1;
263 int pam_authz(nssov_info *ni,TFILE *fp,Operation *op)
265 struct berval dn, uid, svc, ruser, rhost, tty;
266 struct berval authzmsg = BER_BVNULL;
277 SlapReply rs = {REP_RESULT};
278 slap_callback cb = {0};
280 READ_STRING_BUF2(fp,uidc,sizeof(uidc));
282 uid.bv_len = tmpint32;
283 READ_STRING_BUF2(fp,dnc,sizeof(dnc));
285 dn.bv_len = tmpint32;
286 READ_STRING_BUF2(fp,svcc,sizeof(svcc));
288 svc.bv_len = tmpint32;
289 READ_STRING_BUF2(fp,ruserc,sizeof(ruserc));
290 ruser.bv_val = ruserc;
291 ruser.bv_len = tmpint32;
292 READ_STRING_BUF2(fp,rhostc,sizeof(rhostc));
293 rhost.bv_val = rhostc;
294 rhost.bv_len = tmpint32;
295 READ_STRING_BUF2(fp,ttyc,sizeof(ttyc));
297 tty.bv_len = tmpint32;
299 Debug(LDAP_DEBUG_TRACE,"nssov_pam_authz(%s)\n",dn.bv_val,0,0);
301 /* If we didn't do authc, we don't have a DN yet */
302 if (BER_BVISEMPTY(&dn)) {
307 rc = pam_uid2dn(ni, op, &pi);
312 /* See if they have access to the host and service */
313 if ((ni->ni_pam_opts & NI_PAM_HOSTSVC) && nssov_pam_svc_ad) {
314 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
315 struct berval hostdn = BER_BVNULL;
316 struct berval odn = op->o_ndn;
320 nssov_mapinfo *mi = &ni->ni_maps[NM_host];
322 struct berval filter = {sizeof(fbuf),fbuf};
323 SlapReply rs2 = {REP_RESULT};
325 /* Lookup the host entry */
326 nssov_filter_byname(mi,0,&global_host_bv,&filter);
327 cb.sc_private = &hostdn;
328 cb.sc_response = nssov_name2dn_cb;
329 op->o_callback = &cb;
330 op->o_req_dn = mi->mi_base;
331 op->o_req_ndn = mi->mi_base;
332 op->ors_scope = mi->mi_scope;
333 op->ors_filterstr = filter;
334 op->ors_filter = str2filter_x(op, filter.bv_val);
335 op->ors_attrs = slap_anlist_no_attrs;
336 op->ors_tlimit = SLAP_NO_LIMIT;
338 rc = op->o_bd->be_search(op, &rs2);
339 filter_free_x(op, op->ors_filter, 1);
341 if (BER_BVISEMPTY(&hostdn) &&
342 !BER_BVISEMPTY(&ni->ni_pam_defhost)) {
343 filter.bv_len = sizeof(fbuf);
344 filter.bv_val = fbuf;
345 memset(&rs2, 0, sizeof(rs2));
346 rs2.sr_type = REP_RESULT;
347 nssov_filter_byname(mi,0,&ni->ni_pam_defhost,&filter);
348 op->ors_filterstr = filter;
349 op->ors_filter = str2filter_x(op, filter.bv_val);
350 rc = op->o_bd->be_search(op, &rs2);
351 filter_free_x(op, op->ors_filter, 1);
354 /* no host entry, no default host -> deny */
355 if (BER_BVISEMPTY(&hostdn)) {
356 rc = NSLCD_PAM_PERM_DENIED;
362 cb.sc_response = pam_compare_cb;
363 cb.sc_private = NULL;
364 op->o_tag = LDAP_REQ_COMPARE;
365 op->o_req_dn = hostdn;
366 op->o_req_ndn = hostdn;
367 ava.aa_desc = nssov_pam_svc_ad;
370 rc = op->o_bd->be_compare( op, &rs );
371 if ( cb.sc_private == NULL ) {
373 rc = NSLCD_PAM_PERM_DENIED;
380 /* See if they're a member of the group */
381 if ((ni->ni_pam_opts & NI_PAM_USERGRP) &&
382 !BER_BVISEMPTY(&ni->ni_pam_group_dn) &&
383 ni->ni_pam_group_ad) {
384 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
385 op->o_callback = &cb;
386 cb.sc_response = slap_null_cb;
387 op->o_tag = LDAP_REQ_COMPARE;
388 op->o_req_dn = ni->ni_pam_group_dn;
389 op->o_req_ndn = ni->ni_pam_group_dn;
390 ava.aa_desc = ni->ni_pam_group_ad;
393 rc = op->o_bd->be_compare( op, &rs );
394 if ( rs.sr_err != LDAP_COMPARE_TRUE ) {
396 rc = NSLCD_PAM_PERM_DENIED;
401 /* We need to check the user's entry for these bits */
402 if ((ni->ni_pam_opts & (NI_PAM_USERHOST|NI_PAM_USERSVC)) ||
403 ni->ni_pam_template_ad ||
404 ni->ni_pam_min_uid || ni->ni_pam_max_uid ) {
405 rc = be_entry_get_rw( op, &dn, NULL, NULL, 0, &e );
406 if (rc != LDAP_SUCCESS) {
407 rc = NSLCD_PAM_USER_UNKNOWN;
411 if ((ni->ni_pam_opts & NI_PAM_USERHOST) && nssov_pam_host_ad) {
412 a = attr_find(e->e_attrs, nssov_pam_host_ad);
413 if (!a || attr_valfind( a,
414 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
415 SLAP_MR_VALUE_OF_SYNTAX,
416 &global_host_bv, NULL, op->o_tmpmemctx )) {
417 rc = NSLCD_PAM_PERM_DENIED;
422 if ((ni->ni_pam_opts & NI_PAM_USERSVC) && nssov_pam_svc_ad) {
423 a = attr_find(e->e_attrs, nssov_pam_svc_ad);
424 if (!a || attr_valfind( a,
425 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
426 SLAP_MR_VALUE_OF_SYNTAX,
427 &svc, NULL, op->o_tmpmemctx )) {
428 rc = NSLCD_PAM_PERM_DENIED;
437 if (ni->ni_pam_min_uid || ni->ni_pam_max_uid) {
440 nssov_mapinfo *mi = &ni->ni_maps[NM_passwd];
441 a = attr_find(e->e_attrs, mi->mi_attrs[UIDN_KEY].an_desc);
443 rc = NSLCD_PAM_PERM_DENIED;
447 id = (int)strtol(a->a_vals[0].bv_val,&tmp,0);
448 if (a->a_vals[0].bv_val[0] == '\0' || *tmp != '\0') {
449 rc = NSLCD_PAM_PERM_DENIED;
453 if ((ni->ni_pam_min_uid && id < ni->ni_pam_min_uid) ||
454 (ni->ni_pam_max_uid && id > ni->ni_pam_max_uid)) {
455 rc = NSLCD_PAM_PERM_DENIED;
461 if (ni->ni_pam_template_ad) {
462 a = attr_find(e->e_attrs, ni->ni_pam_template_ad);
465 else if (!BER_BVISEMPTY(&ni->ni_pam_template))
466 uid = ni->ni_pam_template;
468 rc = NSLCD_PAM_SUCCESS;
471 WRITE_INT32(fp,NSLCD_VERSION);
472 WRITE_INT32(fp,NSLCD_ACTION_PAM_AUTHZ);
473 WRITE_INT32(fp,NSLCD_RESULT_SUCCESS);
474 WRITE_BERVAL(fp,&uid);
475 WRITE_BERVAL(fp,&dn);
477 WRITE_BERVAL(fp,&authzmsg);
479 be_entry_release_r(op, e);
484 static int pam_sess(nssov_info *ni,TFILE *fp,Operation *op,int action)
486 struct berval dn, uid, svc, tty, rhost, ruser;
494 slap_callback cb = {0};
495 SlapReply rs = {REP_RESULT};
496 char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE];
497 struct berval timestamp, bv[2], *nbv;
501 READ_STRING_BUF2(fp,uidc,sizeof(uidc));
503 uid.bv_len = tmpint32;
504 READ_STRING_BUF2(fp,dnc,sizeof(dnc));
506 dn.bv_len = tmpint32;
507 READ_STRING_BUF2(fp,svcc,sizeof(svcc));
509 svc.bv_len = tmpint32;
510 READ_STRING_BUF2(fp,ttyc,sizeof(ttyc));
512 tty.bv_len = tmpint32;
513 READ_STRING_BUF2(fp,rhostc,sizeof(rhostc));
514 rhost.bv_val = rhostc;
515 rhost.bv_len = tmpint32;
516 READ_STRING_BUF2(fp,ruserc,sizeof(ruserc));
517 ruser.bv_val = ruserc;
518 ruser.bv_len = tmpint32;
519 READ_INT32(fp,stamp);
521 Debug(LDAP_DEBUG_TRACE,"nssov_pam_sess_%c(%s)\n",
522 action==NSLCD_ACTION_PAM_SESS_O ? 'o' : 'c', dn.bv_val,0);
524 if (!dn.bv_len || !ni->ni_pam_sessions) return 0;
528 for (i=0; !BER_BVISNULL(&ni->ni_pam_sessions[i]); i++) {
529 if (ni->ni_pam_sessions[i].bv_len != svc.bv_len)
531 if (!strcasecmp(ni->ni_pam_sessions[i].bv_val, svc.bv_val)) {
536 if (!found) return 0;
539 slap_op_time( &op->o_time, &op->o_tincr );
540 timestamp.bv_len = sizeof(timebuf);
541 timestamp.bv_val = timebuf;
542 if (action == NSLCD_ACTION_PAM_SESS_O )
544 slap_timestamp( &stamp, ×tamp );
545 bv[0].bv_len = timestamp.bv_len + global_host_bv.bv_len + svc.bv_len +
546 tty.bv_len + ruser.bv_len + rhost.bv_len + STRLENOF(" (@)");
547 bv[0].bv_val = op->o_tmpalloc( bv[0].bv_len+1, op->o_tmpmemctx );
548 sprintf(bv[0].bv_val, "%s %s %s %s (%s@%s)",
549 timestamp.bv_val, global_host_bv.bv_val, svc.bv_val, tty.bv_val,
550 ruser.bv_val, rhost.bv_val);
555 attr_normalize( ad_loginStatus, bv, &nbv, op->o_tmpmemctx );
556 mod.sml_nvalues = nbv;
557 mod.sml_desc = ad_loginStatus;
558 mod.sml_op = action == NSLCD_ACTION_PAM_SESS_O ? LDAP_MOD_ADD :
560 mod.sml_flags = SLAP_MOD_INTERNAL;
563 cb.sc_response = slap_null_cb;
564 op->o_callback = &cb;
565 op->o_tag = LDAP_REQ_MODIFY;
566 op->o_dn = op->o_bd->be_rootdn;
567 op->o_ndn = op->o_bd->be_rootndn;
568 op->orm_modlist = &mod;
569 op->orm_no_opattrs = 1;
572 op->o_bd->be_modify( op, &rs );
573 if ( mod.sml_next ) {
574 slap_mods_free( mod.sml_next, 1 );
576 ber_bvarray_free_x( nbv, op->o_tmpmemctx );
578 WRITE_INT32(fp,NSLCD_VERSION);
579 WRITE_INT32(fp,action);
580 WRITE_INT32(fp,NSLCD_RESULT_SUCCESS);
581 WRITE_INT32(fp,op->o_time);
585 int pam_sess_o(nssov_info *ni,TFILE *fp,Operation *op)
587 return pam_sess(ni,fp,op,NSLCD_ACTION_PAM_SESS_O);
590 int pam_sess_c(nssov_info *ni,TFILE *fp,Operation *op)
592 return pam_sess(ni,fp,op,NSLCD_ACTION_PAM_SESS_C);
595 int pam_pwmod(nssov_info *ni,TFILE *fp,Operation *op)
607 READ_STRING_BUF2(fp,uidc,sizeof(uidc));
608 pi.uid.bv_val = uidc;
609 pi.uid.bv_len = tmpint32;
610 READ_STRING_BUF2(fp,dnc,sizeof(dnc));
612 pi.dn.bv_len = tmpint32;
613 READ_STRING_BUF2(fp,svcc,sizeof(svcc));
614 pi.svc.bv_val = svcc;
615 pi.svc.bv_len = tmpint32;
616 READ_STRING_BUF2(fp,opwc,sizeof(opwc));
617 pi.pwd.bv_val = opwc;
618 pi.pwd.bv_len = tmpint32;
619 READ_STRING_BUF2(fp,npwc,sizeof(npwc));
621 npw.bv_len = tmpint32;
623 Debug(LDAP_DEBUG_TRACE,"nssov_pam_pwmod(%s), %s\n",
624 pi.dn.bv_val,pi.uid.bv_val,0);
628 /* This is a prelim check */
629 if (BER_BVISEMPTY(&pi.dn)) {
630 rc = pam_do_bind(ni,fp,op,&pi);
631 if (rc == NSLCD_PAM_IGNORE)
632 rc = NSLCD_PAM_SUCCESS;
634 BerElementBuffer berbuf;
635 BerElement *ber = (BerElement *)&berbuf;
637 SlapReply rs = {REP_RESULT};
638 slap_callback cb = {0};
640 ber_init_w_nullc(ber, LBER_USE_DER);
641 ber_printf(ber, "{");
642 if (!BER_BVISEMPTY(&pi.pwd))
643 ber_printf(ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_OLD,
645 if (!BER_BVISEMPTY(&npw))
646 ber_printf(ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW,
648 ber_printf(ber, "N}");
649 ber_flatten2(ber, &bv, 0);
650 op->o_tag = LDAP_REQ_EXTENDED;
651 op->ore_reqoid = slap_EXOP_MODIFY_PASSWD;
652 op->ore_reqdata = &bv;
655 op->o_callback = &cb;
656 op->o_conn->c_authz_backend = op->o_bd;
657 cb.sc_response = slap_null_cb;
658 op->o_bd = frontendDB;
659 rc = op->o_bd->be_extended(op, &rs);
661 ber_str2bv(rs.sr_text, 0, 0, &pi.msg);
662 if (rc == LDAP_SUCCESS)
663 rc = NSLCD_PAM_SUCCESS;
665 rc = NSLCD_PAM_PERM_DENIED;
667 WRITE_INT32(fp,NSLCD_VERSION);
668 WRITE_INT32(fp,NSLCD_ACTION_PAM_PWMOD);
669 WRITE_INT32(fp,NSLCD_RESULT_SUCCESS);
670 WRITE_BERVAL(fp,&pi.uid);
671 WRITE_BERVAL(fp,&pi.dn);
673 WRITE_BERVAL(fp,&pi.msg);
682 code = slap_str2ad("loginStatus", &ad_loginStatus, &text);