1 #pragma ident "%Z%%M% %I% %E% SMI" 2 /* 3 * lib/krb5/krb/pr_to_salt.c 4 * 5 * Copyright 1990 by the Massachusetts Institute of Technology. 6 * All Rights Reserved. 7 * 8 * Export of this software from the United States of America may 9 * require a specific license from the United States Government. 10 * It is the responsibility of any person or organization contemplating 11 * export to obtain such a license before exporting. 12 * 13 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 14 * distribute this software and its documentation for any purpose and 15 * without fee is hereby granted, provided that the above copyright 16 * notice appear in all copies and that both that copyright notice and 17 * this permission notice appear in supporting documentation, and that 18 * the name of M.I.T. not be used in advertising or publicity pertaining 19 * to distribution of the software without specific, written prior 20 * permission. Furthermore if you modify this software you must label 21 * your software as modified software and not distribute it in such a 22 * fashion that it might be confused with the original M.I.T. software. 23 * M.I.T. makes no representations about the suitability of 24 * this software for any purpose. It is provided "as is" without express 25 * or implied warranty. 26 * 27 * 28 * krb5_principal2salt() 29 */ 30 31 #include <k5-int.h> 32 33 static krb5_error_code krb5_principal2salt_internal 34 (krb5_context, krb5_const_principal, krb5_data *ret, int); 35 36 /* 37 * Convert a krb5_principal into the default salt for that principal. 38 */ 39 /*ARGSUSED*/ 40 static krb5_error_code 41 krb5_principal2salt_internal(krb5_context context, register krb5_const_principal pr, krb5_data *ret, int use_realm) 42 { 43 unsigned int size = 0, offset = 0; 44 krb5_int32 nelem; 45 register int i; 46 47 if (pr == 0) { 48 ret->length = 0; 49 ret->data = 0; 50 return 0; 51 } 52 53 nelem = krb5_princ_size(context, pr); 54 55 if (use_realm) 56 size += krb5_princ_realm(context, pr)->length; 57 58 for (i = 0; i < (int) nelem; i++) 59 size += krb5_princ_component(context, pr, i)->length; 60 61 ret->length = size; 62 if (!(ret->data = malloc (size))) 63 return ENOMEM; 64 65 if (use_realm) { 66 offset = krb5_princ_realm(context, pr)->length; 67 memcpy(ret->data, krb5_princ_realm(context, pr)->data, offset); 68 } 69 70 for (i = 0; i < (int) nelem; i++) { 71 memcpy(&ret->data[offset], krb5_princ_component(context, pr, i)->data, 72 krb5_princ_component(context, pr, i)->length); 73 offset += krb5_princ_component(context, pr, i)->length; 74 } 75 return 0; 76 } 77 78 krb5_error_code 79 krb5_principal2salt(krb5_context context, register krb5_const_principal pr, krb5_data *ret) 80 { 81 return krb5_principal2salt_internal(context, pr, ret, 1); 82 } 83 84 krb5_error_code 85 krb5_principal2salt_norealm(krb5_context context, register krb5_const_principal pr, krb5_data *ret) 86 { 87 return krb5_principal2salt_internal(context, pr, ret, 0); 88 } 89