Commit 1f62744a authored by jan.koester's avatar jan.koester
Browse files

lookupUser for clients

parent a356172a
Loading
Loading
Loading
Loading
Loading
+1 −1
Changes for Dockerfile: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@
# images from another registry/namespace -- see DOCKER_PREFIX in systempp/build-chroot.sh.

ARG IMAGE_PREFIX=tuxist/
ARG FENRIROS_TAG=20261029
ARG FENRIROS_TAG=20261107

FROM ${IMAGE_PREFIX}fenriros-devel:${FENRIROS_TAG} AS build

+68 −0
Changes for client/client.cpp: 68 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1154,6 +1154,74 @@ std::vector<Client::GroupInfo> Client::listGroups() {
    return groups;
}

Client::UserLookup Client::lookupUser(const std::string &nameOrMail) {
    ensure_http_client(_Con);

    json_object* jresponse = json_object_new_object();
    json_object* cmd = json_object_new_array();
    json_object* obj = json_object_new_object();
    json_object_object_add(obj, "clientsession", json_object_new_string(_Con.getClientId().c_str()));
    json_object_object_add(obj, "lookupuser", json_object_new_string(nameOrMail.c_str()));
    json_object_array_add(cmd, obj);
    json_object_object_add(jresponse, "commands", cmd);

    size_t outsize = 0;
    const char* tmp = json_object_to_json_string_length(jresponse, 0, &outsize);
    std::vector<char> out(tmp, tmp + outsize);
    json_object_put(jresponse);

    libhttppp::HttpRequest nreq;
    nreq.setHeaderData("accept")->push_back("application/json");
    nreq.setHeaderData("content-type")->push_back("application/json");
    nreq.setRequestURL(_Con._RequestPath.c_str());
    std::vector<char> resp(safe_post(_Con, nreq, out, _Con._MaxTries));
    resp.push_back('\0');

    json_tokener* jtok = json_tokener_new();
    json_object* jrequest = json_tokener_parse_ex(jtok, resp.data(), resp.size() - 1);
    if (!jrequest) {
        const char* err = json_tokener_error_desc(json_tokener_get_error(jtok));
        json_tokener_free(jtok);
        throw fenrirds::AuthBackendError(err);
    }
    if (json_object_get_type(jrequest) != json_type_array) {
        json_object_put(jrequest);
        json_tokener_free(jtok);
        throw fenrirds::AuthBackendError("Invalid Api Commands");
    }

    UserLookup result;
    bool answered = false;
    std::string error;
    auto str = [](json_object *o, const char *k) {
        json_object *v = json_object_object_get(o, k);
        return v ? std::string(json_object_get_string(v)) : std::string();
    };
    int fcount = json_object_array_length(jrequest);
    for (int i = 0; i < fcount; ++i) {
        json_object_object_foreach(json_object_array_get_idx(jrequest, i), key, val) {
            if (strcmp(key, "error") == 0) error = json_object_get_string(val);
            if (strcmp(key, "lookupuser") == 0 && json_object_get_type(val) == json_type_object) {
                answered = true;
                json_object *jf = json_object_object_get(val, "found");
                result.found = jf && json_object_get_boolean(jf);
                json_object *jd = json_object_object_get(val, "disabled");
                result.disabled = jd && json_object_get_boolean(jd);
                result.userid = str(val, "userid");
                result.username = str(val, "username");
                result.firstname = str(val, "firstname");
                result.lastname = str(val, "lastname");
                result.mail = str(val, "mail");
            }
        }
    }
    json_object_put(jrequest);
    json_tokener_free(jtok);
    if (!error.empty()) throw fenrirds::AuthBackendError(error);
    if (!answered) throw fenrirds::AuthBackendError("lookupuser: not supported by this fenrirds server");
    return result;
}

std::string Client::addGroup(const uuid::uuid& gid, const std::string& name) {
    ensure_http_client(_Con);

+15 −0
Changes for client/client.h: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -142,6 +142,21 @@ namespace fenrirds {
            };

            std::vector<GroupInfo> listGroups();

            // Directory lookup by account name or mail address, with the client login
            // only (no user session): does this user exist? found=false = no such
            // user. Throws AuthBackendError on errors -- also when the server is too
            // old to know the command, so callers can tell "unknown" from "absent".
            struct UserLookup {
                bool        found = false;
                bool        disabled = false;
                std::string userid;
                std::string username;
                std::string firstname;
                std::string lastname;
                std::string mail;
            };
            UserLookup lookupUser(const std::string &nameOrMail);
            std::string addGroup(const uuid::uuid &gid, const std::string &name);

            // GPO result cache TTL in seconds (default 300 = 5 min)
+68 −0
Changes for src/api.cpp: 68 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -29,7 +29,9 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <cctype>
#include <cstring>
#include <string>

#include <json-c/json.h>
#include <json-c/json_types.h>
@@ -787,6 +789,71 @@ public:
        }
    }

    // Client-only lookup of one user by account name or mail address -- lets a service
    // (e.g. a mail server) accept mail for directory users who never logged in to it.
    // {"lookupuser": "alice" | "alice@example.com"} -> {"lookupuser": {"found": bool, ...}}
    void lookupUser(json_object *request, json_object *response) {
        json_object* cmd = get_commands_array_or_throw(request);

        int fcount = json_object_array_length(cmd);
        for (int i = 0; i < fcount; ++i) {
            json_object_object_foreach(json_object_array_get_idx(cmd, i), key, val) {
                if (strcmp(key, "lookupuser") != 0) continue;
                std::string query = val ? json_object_get_string(val) : "";
                while (!query.empty() && isspace(static_cast<unsigned char>(query.back()))) query.pop_back();
                while (!query.empty() && isspace(static_cast<unsigned char>(query.front()))) query.erase(0, 1);
                auto lower = [](std::string v) {
                    for (auto &c : v) c = static_cast<char>(tolower(static_cast<unsigned char>(c)));
                    return v;
                };

                json_object* res = json_object_new_object();
                bool found = false;
                if (g_AdPlugin && !query.empty() && query.size() <= 320) {
                    const std::string domain = _Backend.getDomain();
                    std::string err;
                    dsdb::Entry e;
                    found = g_AdPlugin->dirGetBySam(domain, query, e, err) && e.isClass("user");
                    if (!found && query.find('@') != std::string::npos) {
                        // a mail address: match mail or userPrincipalName, else the
                        // local part as account name
                        std::vector<dsdb::Entry> users;
                        if (g_AdPlugin->dirList(domain, AdPlugin::DirUser, users, err)) {
                            const std::string q = lower(query);
                            for (auto &u : users) {
                                if (lower(u.str("mail")) == q || lower(u.str("userPrincipalName")) == q) {
                                    e = u;
                                    found = true;
                                    break;
                                }
                            }
                        }
                        if (!found) {
                            std::string local = query.substr(0, query.find('@'));
                            found = !local.empty() && g_AdPlugin->dirGetBySam(domain, local, e, err) &&
                                    e.isClass("user");
                        }
                    }
                    if (found) {
                        long uac = 0;
                        try { uac = std::stol(e.str("userAccountControl")); } catch (...) {}
                        json_object_object_add(res, "userid", json_object_new_string(e.objectGuid.c_str()));
                        json_object_object_add(res, "username",
                                               json_object_new_string(e.str("sAMAccountName").c_str()));
                        json_object_object_add(res, "firstname", json_object_new_string(e.str("givenName").c_str()));
                        json_object_object_add(res, "lastname", json_object_new_string(e.str("sn").c_str()));
                        json_object_object_add(res, "mail", json_object_new_string(e.str("mail").c_str()));
                        json_object_object_add(res, "disabled", json_object_new_boolean((uac & 0x2) != 0));
                    }
                }
                json_object_object_add(res, "found", json_object_new_boolean(found));
                json_object* out = json_object_new_object();
                json_object_object_add(out, "lookupuser", res);
                json_object_array_add(response, out);
            }
        }
    }

    Api(AuthBackend &admin, class ::uuid::uuid did, libhttppp::HttpRequest& curreq,
        const int /*tid*/, ULONG_PTR /*args*/)
        : _Backend(admin), _Did(did), _SessionData(nullptr) {
@@ -846,6 +913,7 @@ public:
                addGPO(jrequest, jresponse);
                addGroup(jrequest, jresponse);
                listGroups(jrequest, jresponse);
                lookupUser(jrequest, jresponse);
            }

            // With user session only