summaryrefslogtreecommitdiff
path: root/usr.sbin
diff options
context:
space:
mode:
authorMartin Hedenfal <martinh@cvs.openbsd.org>2011-01-17 14:24:02 +0000
committerMartin Hedenfal <martinh@cvs.openbsd.org>2011-01-17 14:24:02 +0000
commit172f35980ddbaaacf765faa931ca2b7e4d6c12da (patch)
tree5de658d9d66ef3f62c204001aa5440a5bf5f704d /usr.sbin
parent964190fa738bab0882d55d6fec657b121100ef24 (diff)
extract common code for building the idm_req struct for passwords
and groups in a new function client_build_req ok pyr@ CV: ----------------------------------------------------------------------
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/ypldap/ldapclient.c203
1 files changed, 86 insertions, 117 deletions
diff --git a/usr.sbin/ypldap/ldapclient.c b/usr.sbin/ypldap/ldapclient.c
index ec920b993c4..688637927f9 100644
--- a/usr.sbin/ypldap/ldapclient.c
+++ b/usr.sbin/ypldap/ldapclient.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ldapclient.c,v 1.19 2011/01/13 06:12:29 martinh Exp $ */
+/* $OpenBSD: ldapclient.c,v 1.20 2011/01/17 14:24:01 martinh Exp $ */
/*
* Copyright (c) 2008 Alexander Schrijver <aschrijver@openbsd.org>
@@ -47,6 +47,8 @@ void client_shutdown(void);
void client_connect(int, short, void *);
void client_configure(struct env *);
void client_periodic_update(int, short, void *);
+int client_build_req(struct idm *, struct idm_req *, struct aldap_message *,
+ int, int);
int client_try_idm(struct env *, struct idm *);
int client_addr_init(struct idm *);
int client_addr_free(struct idm *);
@@ -431,12 +433,84 @@ ldapclient(int pipe_main2client[2])
}
int
+client_build_req(struct idm *idm, struct idm_req *ir, struct aldap_message *m,
+ int min_attr, int max_attr)
+{
+ char **ldap_attrs;
+ int i, k;
+
+ bzero(ir, sizeof(*ir));
+ for (i = min_attr; i < max_attr; i++) {
+ if (idm->idm_flags & F_FIXED_ATTR(i)) {
+ if (strlcat(ir->ir_line, idm->idm_attrs[i],
+ sizeof(ir->ir_line)) >= sizeof(ir->ir_line))
+ /*
+ * entry yields a line > 1024, trash it.
+ */
+ return (-1);
+
+ if (i == ATTR_UID) {
+ ir->ir_key.ik_uid = strtonum(
+ idm->idm_attrs[i], 0,
+ UID_MAX, NULL);
+ } else if (i == ATTR_GR_GID) {
+ ir->ir_key.ik_gid = strtonum(
+ idm->idm_attrs[i], 0,
+ GID_MAX, NULL);
+ }
+ } else if (idm->idm_list & F_LIST(i)) {
+ if (aldap_match_entry(m, idm->idm_attrs[i], &ldap_attrs) == -1)
+ return (-1);
+ if (ldap_attrs[0] == NULL)
+ return (-1);
+ for (k = 0; k >= 0 && ldap_attrs[k] != NULL; k++) {
+ if (strlcat(ir->ir_line, ldap_attrs[k],
+ sizeof(ir->ir_line)) >= sizeof(ir->ir_line))
+ continue;
+ if (ldap_attrs[k+1] != NULL)
+ if (strlcat(ir->ir_line, ",",
+ sizeof(ir->ir_line))
+ >= sizeof(ir->ir_line)) {
+ aldap_free_entry(ldap_attrs);
+ return (-1);
+ }
+ }
+ aldap_free_entry(ldap_attrs);
+ } else {
+ if (aldap_match_entry(m, idm->idm_attrs[i], &ldap_attrs) == -1)
+ return (-1);
+ if (ldap_attrs[0] == NULL)
+ return (-1);
+ if (strlcat(ir->ir_line, ldap_attrs[0],
+ sizeof(ir->ir_line)) >= sizeof(ir->ir_line)) {
+ aldap_free_entry(ldap_attrs);
+ return (-1);
+ }
+ if (i == ATTR_UID) {
+ ir->ir_key.ik_uid = strtonum(
+ ldap_attrs[0], 0, UID_MAX, NULL);
+ } else if (i == ATTR_GR_GID) {
+ ir->ir_key.ik_uid = strtonum(
+ ldap_attrs[0], 0, GID_MAX, NULL);
+ }
+ aldap_free_entry(ldap_attrs);
+ }
+
+ if (i + 1 != max_attr)
+ if (strlcat(ir->ir_line, ":",
+ sizeof(ir->ir_line)) >= sizeof(ir->ir_line))
+ return (-1);
+ }
+
+ return (0);
+}
+
+int
client_try_idm(struct env *env, struct idm *idm)
{
const char *where, *errstr;
char *attrs[ATTR_MAX+1];
- char **ldap_attrs;
- int i, j, k;
+ int i, j;
struct idm_req ir;
struct aldap_message *m;
struct aldap *al;
@@ -497,63 +571,10 @@ client_try_idm(struct env *env, struct idm *idm)
aldap_freemsg(m);
goto bad;
}
- /* search entry */
- bzero(&ir, sizeof(ir));
- for (i = 0, j = 0; i < ATTR_MAX; i++) {
- if (idm->idm_flags & F_FIXED_ATTR(i)) {
- if (strlcat(ir.ir_line, idm->idm_attrs[i],
- sizeof(ir.ir_line)) >= sizeof(ir.ir_line))
- /*
- * entry yields a line > 1024, trash it.
- */
- goto next_pwdentry;
- if (i == ATTR_UID) {
- ir.ir_key.ik_uid = strtonum(
- idm->idm_attrs[i], 0,
- UID_MAX, NULL);
- }
- } else if (idm->idm_list & F_LIST(i)) {
- if (aldap_match_entry(m, attrs[j++], &ldap_attrs) == -1)
- goto next_pwdentry;
- if (ldap_attrs[0] == NULL)
- goto next_pwdentry;
- for (k = 0; k >= 0 && ldap_attrs[k] != NULL; k++) {
- if (strlcat(ir.ir_line, ldap_attrs[k],
- sizeof(ir.ir_line)) >= sizeof(ir.ir_line))
- continue;
- if (ldap_attrs[k+1] != NULL)
- if (strlcat(ir.ir_line, ",",
- sizeof(ir.ir_line))
- >= sizeof(ir.ir_line)) {
- aldap_free_entry(ldap_attrs);
- goto next_pwdentry;
- }
- }
- aldap_free_entry(ldap_attrs);
- } else {
- if (aldap_match_entry(m, attrs[j++], &ldap_attrs) == -1)
- goto next_pwdentry;
- if (ldap_attrs[0] == NULL)
- goto next_pwdentry;
- if (strlcat(ir.ir_line, ldap_attrs[0],
- sizeof(ir.ir_line)) >= sizeof(ir.ir_line)) {
- aldap_free_entry(ldap_attrs);
- goto next_pwdentry;
- }
- if (i == ATTR_UID) {
- ir.ir_key.ik_uid = strtonum(
- ldap_attrs[0], 0, UID_MAX, NULL);
- }
- aldap_free_entry(ldap_attrs);
- }
- if (i != ATTR_SHELL)
- if (strlcat(ir.ir_line, ":",
- sizeof(ir.ir_line)) >= sizeof(ir.ir_line))
- goto next_pwdentry;
- }
- imsg_compose_event(env->sc_iev, IMSG_PW_ENTRY, 0, 0, -1,
- &ir, sizeof(ir));
-next_pwdentry:
+
+ if (client_build_req(idm, &ir, m, 0, ATTR_MAX) == 0)
+ imsg_compose_event(env->sc_iev, IMSG_PW_ENTRY, 0, 0, -1,
+ &ir, sizeof(ir));
aldap_freemsg(m);
}
@@ -594,63 +615,11 @@ next_pwdentry:
aldap_freemsg(m);
goto bad;
}
- /* search entry */
- bzero(&ir, sizeof(ir));
- for (i = ATTR_GR_MIN, j = 0; i < ATTR_GR_MAX; i++) {
- if (idm->idm_flags & F_FIXED_ATTR(i)) {
- if (strlcat(ir.ir_line, idm->idm_attrs[i],
- sizeof(ir.ir_line)) >= sizeof(ir.ir_line))
- /*
- * entry yields a line > 1024, trash it.
- */
- goto next_grpentry;
- if (i == ATTR_GR_GID) {
- ir.ir_key.ik_gid = strtonum(
- idm->idm_attrs[i], 0,
- GID_MAX, NULL);
- }
- } else if (idm->idm_list & F_LIST(i)) {
- if (aldap_match_entry(m, attrs[j++], &ldap_attrs) == -1)
- goto next_grpentry;
- if (ldap_attrs[0] == NULL)
- goto next_grpentry;
- for (k = 0; k >= 0 && ldap_attrs[k] != NULL; k++) {
- if (strlcat(ir.ir_line, ldap_attrs[k],
- sizeof(ir.ir_line)) >= sizeof(ir.ir_line))
- continue;
- if (ldap_attrs[k+1] != NULL)
- if (strlcat(ir.ir_line, ",",
- sizeof(ir.ir_line))
- >= sizeof(ir.ir_line)) {
- aldap_free_entry(ldap_attrs);
- goto next_grpentry;
- }
- }
- aldap_free_entry(ldap_attrs);
- } else {
- if (aldap_match_entry(m, attrs[j++], &ldap_attrs) == -1)
- goto next_grpentry;
- if (ldap_attrs[0] == NULL)
- goto next_grpentry;
- if (strlcat(ir.ir_line, ldap_attrs[0],
- sizeof(ir.ir_line)) >= sizeof(ir.ir_line)) {
- aldap_free_entry(ldap_attrs);
- goto next_grpentry;
- }
- if (i == ATTR_GR_GID) {
- ir.ir_key.ik_uid = strtonum(
- ldap_attrs[0], 0, GID_MAX, NULL);
- }
- aldap_free_entry(ldap_attrs);
- }
- if (i != ATTR_GR_MEMBERS)
- if (strlcat(ir.ir_line, ":",
- sizeof(ir.ir_line)) >= sizeof(ir.ir_line))
- goto next_grpentry;
- }
- imsg_compose_event(env->sc_iev, IMSG_GRP_ENTRY, 0, 0, -1,
- &ir, sizeof(ir));
-next_grpentry:
+
+ if (client_build_req(idm, &ir, m, ATTR_GR_MIN, ATTR_GR_MAX) == 0)
+ imsg_compose_event(env->sc_iev, IMSG_GRP_ENTRY, 0, 0, -1,
+ &ir, sizeof(ir));
+
aldap_freemsg(m);
}