#include "gssapiP_krb5.h"
#ifndef NO_PASSWORD
#include <pwd.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
OM_uint32
krb5_gss_import_name(minor_status, input_name_buffer,
input_name_type, output_name)
OM_uint32 *minor_status;
gss_buffer_t input_name_buffer;
gss_OID input_name_type;
gss_name_t *output_name;
{
krb5_context context;
krb5_principal princ;
krb5_error_code code;
char *stringrep, *tmp, *tmp2, *cp;
OM_uint32 length;
#ifndef NO_PASSWORD
struct passwd *pw;
#endif
if (GSS_ERROR(kg_get_context(minor_status, &context)))
return(GSS_S_FAILURE);
*output_name = NULL;
*minor_status = 0;
if ((input_name_type != GSS_C_NULL_OID) &&
(g_OID_equal(input_name_type, gss_nt_service_name) ||
g_OID_equal(input_name_type, gss_nt_service_name_v2))) {
char *service, *host;
if ((tmp =
(char *) xmalloc(input_name_buffer->length + 1)) == NULL) {
*minor_status = ENOMEM;
return(GSS_S_FAILURE);
}
memcpy(tmp, input_name_buffer->value, input_name_buffer->length);
tmp[input_name_buffer->length] = 0;
service = tmp;
if ((host = strchr(tmp, '@'))) {
*host = '\0';
host++;
}
code = krb5_sname_to_principal(context, host, service, KRB5_NT_SRV_HST,
&princ);
xfree(tmp);
} else if ((input_name_type != GSS_C_NULL_OID) &&
(g_OID_equal(input_name_type, gss_nt_krb5_principal))) {
krb5_principal input;
if (input_name_buffer->length != sizeof(krb5_principal)) {
*minor_status = (OM_uint32) G_WRONG_SIZE;
return(GSS_S_BAD_NAME);
}
input = *((krb5_principal *) input_name_buffer->value);
if ((code = krb5_copy_principal(context, input, &princ))) {
*minor_status = code;
return(GSS_S_FAILURE);
}
} else {
stringrep = NULL;
if ((tmp =
(char *) xmalloc(input_name_buffer->length + 1)) == NULL) {
*minor_status = ENOMEM;
return(GSS_S_FAILURE);
}
tmp2 = 0;
memcpy(tmp, input_name_buffer->value, input_name_buffer->length);
tmp[input_name_buffer->length] = 0;
if ((input_name_type == GSS_C_NULL_OID) ||
g_OID_equal(input_name_type, gss_nt_krb5_name) ||
g_OID_equal(input_name_type, gss_nt_user_name)) {
stringrep = (char *) tmp;
#ifndef NO_PASSWORD
} else if (g_OID_equal(input_name_type, gss_nt_machine_uid_name)) {
if ((pw = getpwuid(*((uid_t *) input_name_buffer->value))))
stringrep = pw->pw_name;
else
*minor_status = (OM_uint32) G_NOUSER;
} else if (g_OID_equal(input_name_type, gss_nt_string_uid_name)) {
if ((pw = getpwuid((uid_t) atoi(tmp))))
stringrep = pw->pw_name;
else
*minor_status = (OM_uint32) G_NOUSER;
#endif
} else if (g_OID_equal(input_name_type, gss_nt_exported_name)) {
cp = tmp;
if (*cp++ != 0x04)
goto fail_name;
if (*cp++ != 0x01)
goto fail_name;
if (*cp++ != 0x00)
goto fail_name;
length = *cp++;
if (length != gss_mech_krb5->length+2)
goto fail_name;
if (*cp++ != 0x06)
goto fail_name;
length = *cp++;
if (length != gss_mech_krb5->length)
goto fail_name;
if (memcmp(cp, gss_mech_krb5->elements, length) != 0)
goto fail_name;
cp += length;
length = *cp++;
length = (length << 8) | *cp++;
length = (length << 8) | *cp++;
length = (length << 8) | *cp++;
tmp2 = malloc(length+1);
if (tmp2 == NULL) {
xfree(tmp);
*minor_status = ENOMEM;
return GSS_S_FAILURE;
}
strncpy(tmp2, cp, length);
tmp2[length] = 0;
stringrep = tmp2;
} else {
return(GSS_S_BAD_NAMETYPE);
}
if (stringrep)
code = krb5_parse_name(context, (char *) stringrep, &princ);
else {
fail_name:
xfree(tmp);
if (tmp2)
xfree(tmp2);
return(GSS_S_BAD_NAME);
}
if (tmp2)
xfree(tmp2);
xfree(tmp);
}
if (code) {
*minor_status = (OM_uint32) code;
return(GSS_S_BAD_NAME);
}
if (! kg_save_name((gss_name_t) princ)) {
krb5_free_principal(context, princ);
*minor_status = (OM_uint32) G_VALIDATE_FAILED;
return(GSS_S_FAILURE);
}
*output_name = (gss_name_t) princ;
return(GSS_S_COMPLETE);
}