/*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
 *
 * n4: A fast userspace NFSv4 server.
 * $Id: rpcauth.c,v 1.6 2001/06/08 08:54:44 mbp Exp $
 * 
 * Copyright (C) 2001 by Martin Pool <mbp@samba.org>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#define LOG_DOMAIN "n4rpc"
#include "includes.h"



/**
 * Swallow a Credential/Verifier pair from an RPC header.
 **/
MKPROTO n4err n4rpc_swallow_req_auth(n4inbuf *b)
{
    n4err                 err;
    enum rpc_auth_flavor  cred_flavor, verf_flavor;
    size_t                cred_len, verf_len;
    gconstpointer         cred_p, verf_p;

    /** Need to at least get at least an rpc_auth_flavor plus the
     * length of the opaque body. */
    if ((err = n4inbuf_need(b, 2)))
        return err;

/* credential flavor, length */
    cred_flavor = g_ntohl(UNPACK(b));
    cred_len = g_ntohl(UNPACK(b));

    n4trace("got auth credential, flavor=%d(%s), length=%ld",
             cred_flavor, n4rpc_auth_flavor_name(cred_flavor),
             (long) cred_len);

    if ((err = n4inbuf_need(b, PADDED(cred_len))))
        return err;
    cred_p = UNPACK_BYTES(b, cred_len);

/* verifier flavor, length */
    if ((err = n4inbuf_need(b, 2)))
        return err;
    verf_flavor = g_ntohl(UNPACK(b));
    verf_len = g_ntohl(UNPACK(b));

    /* TODO: Rewrite to use n4xdr_unpack_opaque */

    n4trace("got auth verified, flavor=%d(%s), length=%ld",
             verf_flavor, n4rpc_auth_flavor_name(verf_flavor),
             (long) verf_len);

    if ((err = n4inbuf_need(b, PADDED(verf_len))))
        return err;
    verf_p = UNPACK_BYTES(b, verf_len);

    return N4OK;
}



n4err n4unpack_opaque_auth(n4inbuf *inb)
{
    enum rpc_auth_flavor flavor;
    size_t               auth_len;
    char const           *auth_opaque;

    flavor = FROM_NET(UNPACK(inb));
    CHECK_ERR(n4unpack_var(inb, &auth_len, &auth_opaque));

    n4log_binary(N4LOG_DEBUG, auth_opaque, auth_len,
                 "opaque authenticator flavor %s(%d), %d bytes:",
                 n4rpc_auth_flavor_name(flavor), flavor, auth_len);

    /* TODO: Check we can handle this method */

    if (flavor == RPC_AUTH_NONE  &&  auth_len != 0) {
        n4log_error("AUTH_NONE should have no body!");
        return N4ERR_RPC_AUTH;
    }

    return N4OK;
}



n4err n4pack_null_auth(n4outbuf *outb)
{
    CHECK_ERR(n4outbuf_expand(outb, 2));
    PACK(outb, TO_NET(RPC_AUTH_NONE));
    PACK(outb, TO_NET(0));            /* no data */
    return N4OK;
}


/**
 * Return string form for an \b rpc_auth_flavor
 */
MKPROTO gchar const * n4rpc_auth_flavor_name(enum rpc_auth_flavor flavor)
{
    switch (flavor) {
    case RPC_AUTH_NONE:
        return "RPC_AUTH_NONE";
    case RPC_AUTH_SHORT:
        return "RPC_AUTH_SHORT";
    case RPC_AUTH_SYS:
        return "RPC_AUTH_SYS";
    default:
        return "UNKNOWN";
    }
}
