On Sun, May 29, 2011 at 04:22:07PM -0300, Friedrich Locke wrote:
> Dear list users,
> 
> i am planning on migrating from tradicional unix password files to LDAP.
> But i have one question: what about uid definition? Does ldap will
> (for instance) auto increment it?
> If not, how will ldap manage uid alocation?
> 
> Thanks in advance.
> 

Hi!

You have to manage that for yourself; this might help your tools written
in C. And note that in most schemas it's not 'uid', but 'uidNumber'.

--
Martin Pelikan


unsigned
find_lowest_uidnum(LDAP *l, const char *bdn)
{
        static char uidNumber[] = "uidNumber";
        static char *attrs[] = { uidNumber, NULL };
        int error, cur, i;
        u_int8_t used[UIDNUMBER_MAX - UIDNUMBER_MIN];
        LDAPMessage *e;
        LDAPMessage *res;
        struct berval **vals;

        if ((error = ldap_search_ext_s(l, bdn, LDAP_SCOPE_ONELEVEL, NULL,
            attrs, 0, NULL, NULL, NULL, 0, &res)) != LDAP_SUCCESS)
                errx(1, "find_lowest_uidnum: synchronous search: %s",
                    ldap_err2string(error));

        memset(used, 0, sizeof used);

        for (e = ldap_first_entry(l, res); e; e = ldap_next_entry(l, e)) {
                vals = ldap_get_values_len(l, e, uidNumber);
                if (ldap_count_values_len(vals) != 1) {
                        warnx("Weird amount of UID numbers!");
                        ldap_value_free_len(vals);
                        continue;
                }

                cur = atoi(vals[0]->bv_val) - UIDNUMBER_MIN;
                if (cur >= 0 && cur < (UIDNUMBER_MAX - UIDNUMBER_MIN)) {
                        used[cur]++;
                }

                ldap_value_free_len(vals);
        }

        ldap_msgfree(res);

        for (i = 0; i < (UIDNUMBER_MAX - UIDNUMBER_MIN); ++i) {
                if (used[i] == 0) {
                        return (i + UIDNUMBER_MIN);
                }
        }
        return (-1);
}

Reply via email to