17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * lib/krb5/krb/pr_to_salt.c
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * Copyright 1990 by the Massachusetts Institute of Technology.
57c478bd9Sstevel@tonic-gate  * All Rights Reserved.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
87c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
97c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
107c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
11*55fea89dSDan Cross  *
127c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
137c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
147c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
157c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
167c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
177c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
187c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
197c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
207c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
217c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
227c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
237c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
247c478bd9Sstevel@tonic-gate  * or implied warranty.
25*55fea89dSDan Cross  *
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * krb5_principal2salt()
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
30159d09a2SMark Phalan #include "k5-int.h"
317c478bd9Sstevel@tonic-gate 
32505d05c7Sgtb static krb5_error_code krb5_principal2salt_internal
33505d05c7Sgtb     (krb5_context, krb5_const_principal, krb5_data *ret, int);
34505d05c7Sgtb 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * Convert a krb5_principal into the default salt for that principal.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
39505d05c7Sgtb static krb5_error_code
krb5_principal2salt_internal(krb5_context context,register krb5_const_principal pr,krb5_data * ret,int use_realm)40505d05c7Sgtb krb5_principal2salt_internal(krb5_context context, register krb5_const_principal pr, krb5_data *ret, int use_realm)
417c478bd9Sstevel@tonic-gate {
42159d09a2SMark Phalan     unsigned int size = 0, offset=0;
437c478bd9Sstevel@tonic-gate     krb5_int32 nelem;
447c478bd9Sstevel@tonic-gate     register int i;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate     if (pr == 0) {
477c478bd9Sstevel@tonic-gate 	ret->length = 0;
487c478bd9Sstevel@tonic-gate 	ret->data = 0;
497c478bd9Sstevel@tonic-gate 	return 0;
507c478bd9Sstevel@tonic-gate     }
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate     nelem = krb5_princ_size(context, pr);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate     if (use_realm)
557c478bd9Sstevel@tonic-gate 	    size += krb5_princ_realm(context, pr)->length;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate     for (i = 0; i < (int) nelem; i++)
587c478bd9Sstevel@tonic-gate 	size += krb5_princ_component(context, pr, i)->length;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate     ret->length = size;
617c478bd9Sstevel@tonic-gate     if (!(ret->data = malloc (size)))
627c478bd9Sstevel@tonic-gate 	return ENOMEM;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate     if (use_realm) {
657c478bd9Sstevel@tonic-gate 	    offset = krb5_princ_realm(context, pr)->length;
667c478bd9Sstevel@tonic-gate 	    memcpy(ret->data, krb5_princ_realm(context, pr)->data, offset);
677c478bd9Sstevel@tonic-gate     }
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate     for (i = 0; i < (int) nelem; i++) {
707c478bd9Sstevel@tonic-gate 	memcpy(&ret->data[offset], krb5_princ_component(context, pr, i)->data,
717c478bd9Sstevel@tonic-gate 	       krb5_princ_component(context, pr, i)->length);
727c478bd9Sstevel@tonic-gate 	offset += krb5_princ_component(context, pr, i)->length;
737c478bd9Sstevel@tonic-gate     }
747c478bd9Sstevel@tonic-gate     return 0;
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate krb5_error_code
krb5_principal2salt(krb5_context context,register krb5_const_principal pr,krb5_data * ret)78505d05c7Sgtb krb5_principal2salt(krb5_context context, register krb5_const_principal pr, krb5_data *ret)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	return krb5_principal2salt_internal(context, pr, ret, 1);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate krb5_error_code
krb5_principal2salt_norealm(krb5_context context,register krb5_const_principal pr,krb5_data * ret)84505d05c7Sgtb krb5_principal2salt_norealm(krb5_context context, register krb5_const_principal pr, krb5_data *ret)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	return krb5_principal2salt_internal(context, pr, ret, 0);
877c478bd9Sstevel@tonic-gate }
88