14ecbd6dmarkm/*
233f661cstas * Copyright (c) 1997 - 2001 Kungliga Tekniska H��gskolan
333f661cstas * (Royal Institute of Technology, Stockholm, Sweden).
433f661cstas * All rights reserved.
54ecbd6dmarkm *
633f661cstas * Redistribution and use in source and binary forms, with or without
733f661cstas * modification, are permitted provided that the following conditions
833f661cstas * are met:
94ecbd6dmarkm *
1033f661cstas * 1. Redistributions of source code must retain the above copyright
1133f661cstas *    notice, this list of conditions and the following disclaimer.
124ecbd6dmarkm *
1333f661cstas * 2. Redistributions in binary form must reproduce the above copyright
1433f661cstas *    notice, this list of conditions and the following disclaimer in the
1533f661cstas *    documentation and/or other materials provided with the distribution.
164ecbd6dmarkm *
1733f661cstas * 3. Neither the name of the Institute nor the names of its contributors
1833f661cstas *    may be used to endorse or promote products derived from this software
1933f661cstas *    without specific prior written permission.
204ecbd6dmarkm *
2133f661cstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2233f661cstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2333f661cstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2433f661cstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2533f661cstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2633f661cstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2733f661cstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2833f661cstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2933f661cstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3033f661cstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3133f661cstas * SUCH DAMAGE.
324ecbd6dmarkm */
334ecbd6dmarkm
344ecbd6dmarkm#include "krb5_locl.h"
354ecbd6dmarkm
3633f661cstasKRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
374ecbd6dmarkmkrb5_sock_to_principal (krb5_context context,
384ecbd6dmarkm			int sock,
394ecbd6dmarkm			const char *sname,
404ecbd6dmarkm			int32_t type,
414ecbd6dmarkm			krb5_principal *ret_princ)
424ecbd6dmarkm{
434ecbd6dmarkm    krb5_error_code ret;
444ecbd6dmarkm    struct sockaddr_storage __ss;
454ecbd6dmarkm    struct sockaddr *sa = (struct sockaddr *)&__ss;
4669a91benectar    socklen_t salen = sizeof(__ss);
4769a91benectar    char hostname[NI_MAXHOST];
484ecbd6dmarkm
4969a91benectar    if (getsockname (sock, sa, &salen) < 0) {
500c8fa35assar	ret = errno;
5133f661cstas	krb5_set_error_message (context, ret, "getsockname: %s", strerror(ret));
520c8fa35assar	return ret;
530c8fa35assar    }
5469a91benectar    ret = getnameinfo (sa, salen, hostname, sizeof(hostname), NULL, 0, 0);
5569a91benectar    if (ret) {
5669a91benectar	int save_errno = errno;
5733f661cstas	krb5_error_code ret2 = krb5_eai_to_heim_errno(ret, save_errno);
5833f661cstas	krb5_set_error_message (context, ret2, "getnameinfo: %s", gai_strerror(ret));
5933f661cstas	return ret2;
60ebfe6dcassar    }
61ebfe6dcassar
6269a91benectar    ret = krb5_sname_to_principal (context,
6369a91benectar				   hostname,
6469a91benectar				   sname,
6569a91benectar				   type,
6669a91benectar				   ret_princ);
6769a91benectar    return ret;
684ecbd6dmarkm}
69