Blame src/kprop/kprop_util.c

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit fd8b60
/* kprop/kprop_util.c */
Packit fd8b60
/*
Packit fd8b60
 * Copyright (C) 2010 by the Massachusetts Institute of Technology.
Packit fd8b60
 * All rights reserved.
Packit fd8b60
 *
Packit fd8b60
 * Export of this software from the United States of America may
Packit fd8b60
 *   require a specific license from the United States Government.
Packit fd8b60
 *   It is the responsibility of any person or organization contemplating
Packit fd8b60
 *   export to obtain such a license before exporting.
Packit fd8b60
 *
Packit fd8b60
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
Packit fd8b60
 * distribute this software and its documentation for any purpose and
Packit fd8b60
 * without fee is hereby granted, provided that the above copyright
Packit fd8b60
 * notice appear in all copies and that both that copyright notice and
Packit fd8b60
 * this permission notice appear in supporting documentation, and that
Packit fd8b60
 * the name of M.I.T. not be used in advertising or publicity pertaining
Packit fd8b60
 * to distribution of the software without specific, written prior
Packit fd8b60
 * permission.  Furthermore if you modify this software you must label
Packit fd8b60
 * your software as modified software and not distribute it in such a
Packit fd8b60
 * fashion that it might be confused with the original M.I.T. software.
Packit fd8b60
 * M.I.T. makes no representations about the suitability of
Packit fd8b60
 * this software for any purpose.  It is provided "as is" without express
Packit fd8b60
 * or implied warranty.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
/* sockaddr2krbaddr() utility function used by kprop and kpropd */
Packit fd8b60
Packit fd8b60
#include "k5-int.h"
Packit fd8b60
#include "kprop.h"
Packit fd8b60
Packit fd8b60
#include <sys/types.h>
Packit fd8b60
#include <sys/socket.h>
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * Convert an IPv4 or IPv6 socket address to a newly allocated krb5_address.
Packit fd8b60
 * There is similar code elsewhere in the tree, so this should possibly become
Packit fd8b60
 * a libkrb5 API in the future.
Packit fd8b60
 */
Packit fd8b60
krb5_error_code
Packit fd8b60
sockaddr2krbaddr(krb5_context context, int family, struct sockaddr *sa,
Packit fd8b60
                 krb5_address **dest)
Packit fd8b60
{
Packit fd8b60
    krb5_address addr;
Packit fd8b60
Packit fd8b60
    addr.magic = KV5M_ADDRESS;
Packit fd8b60
    if (family == AF_INET) {
Packit fd8b60
        struct sockaddr_in *sa4 = sa2sin(sa);
Packit fd8b60
        addr.addrtype = ADDRTYPE_INET;
Packit fd8b60
        addr.length = sizeof(sa4->sin_addr);
Packit fd8b60
        addr.contents = (krb5_octet *) &sa4->sin_addr;
Packit fd8b60
    } else if (family == AF_INET6) {
Packit fd8b60
        struct sockaddr_in6 *sa6 = sa2sin6(sa);
Packit fd8b60
        if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
Packit fd8b60
            addr.addrtype = ADDRTYPE_INET;
Packit fd8b60
            addr.contents = (krb5_octet *) &sa6->sin6_addr + 12;
Packit fd8b60
            addr.length = 4;
Packit fd8b60
        } else {
Packit fd8b60
            addr.addrtype = ADDRTYPE_INET6;
Packit fd8b60
            addr.length = sizeof(sa6->sin6_addr);
Packit fd8b60
            addr.contents = (krb5_octet *) &sa6->sin6_addr;
Packit fd8b60
        }
Packit fd8b60
    } else
Packit fd8b60
        return KRB5_PROG_ATYPE_NOSUPP;
Packit fd8b60
Packit fd8b60
    return krb5_copy_addr(context, &addr, dest);
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* Construct a host-based principal, similar to krb5_sname_to_principal() but
Packit fd8b60
 * with a specified realm. */
Packit fd8b60
krb5_error_code
Packit fd8b60
sn2princ_realm(krb5_context context, const char *hostname, const char *sname,
Packit fd8b60
               const char *realm, krb5_principal *princ_out)
Packit fd8b60
{
Packit fd8b60
    krb5_error_code ret;
Packit fd8b60
    char *canonhost, localname[MAXHOSTNAMELEN];
Packit fd8b60
Packit fd8b60
    *princ_out = NULL;
Packit fd8b60
    assert(sname != NULL && realm != NULL);
Packit fd8b60
Packit fd8b60
    /* If hostname is NULL, use the local hostname. */
Packit fd8b60
    if (hostname == NULL) {
Packit fd8b60
        if (gethostname(localname, MAXHOSTNAMELEN) != 0)
Packit fd8b60
            return SOCKET_ERRNO;
Packit fd8b60
        hostname = localname;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    ret = krb5_expand_hostname(context, hostname, &canonhost);
Packit fd8b60
    if (ret)
Packit fd8b60
        return ret;
Packit fd8b60
Packit fd8b60
    ret = krb5_build_principal(context, princ_out, strlen(realm), realm, sname,
Packit fd8b60
                               canonhost, (char *)NULL);
Packit fd8b60
    krb5_free_string(context, canonhost);
Packit fd8b60
    if (!ret)
Packit fd8b60
        (*princ_out)->type = KRB5_NT_SRV_HST;
Packit fd8b60
    return ret;
Packit fd8b60
}