/*******************************************************************************
 * Copyright (c) 2026, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#pragma once

#include <uuidp.h>
#include <string>

#include "authdb.h"
#include "hash.h"

#ifdef Windows
#define VISIBILITY __declspec(dllexport)
#else
#define VISIBILITY __attribute__((visibility("default")))
#endif

namespace authdb {

    class AuthData;

    /**
     * ServiceData - Stores a Kerberos service principal.
     *
     * Each service is assigned to a domain (via the backend it lives in)
     * and optionally to a client (host).  The service has:
     *   - servicename   e.g. "HTTP", "ldap", "imap"
     *   - hostname      e.g. "mail.example.com"  (the instance part)
     *   - clientid      UUID of the owning Client (may be empty)
     *   - krbkey        hex-encoded Kerberos long-term key
     *
     * The resulting Kerberos principal is:  <servicename>/<hostname>@REALM
     */
    class VISIBILITY ServiceData : public AuthData {
    public:
        ServiceData(uuid::uuid sid);
        ~ServiceData();

        void setServiceName(const char *name);
        const char *getServiceName();

        void setHostName(const char *hostname);
        const char *getHostName();

        void setClientId(const char *clientid);
        const char *getClientId();

        void setKrbKey(const std::string &src);
        const std::string getKrbKey();

        void setSecret(const char *secret);
        const char *getSecret();

    private:
        AuthData::Record *_ServiceName;
        AuthData::Record *_HostName;
        AuthData::Record *_ClientId;
        AuthData::Record *_KrbKey;
        AuthData::Record *_Secret;
        friend class ServiceManager;
    };

    class VISIBILITY ServiceManager {
    public:
        void create(AuthBackend &backend, class ServiceData *data);
        void remove(AuthBackend &backend, const uuid::uuid &sid);
        bool exits(AuthBackend &backend, const uuid::uuid &sid, const char *name, int &ret);
        void info(AuthBackend &backend, class ServiceData &dat, size_t &pos);
    };

};
