1 #pragma ident "%Z%%M% %I% %E% SMI" 2 /* 3 * lib/krb5/krb/bld_pr_ext.c 4 * 5 * Copyright 1991 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 * Build a principal from a list of lengths and strings 29 */ 30 31 #include <k5-int.h> 32 33 #include <stdarg.h> 34 35 /*ARGSUSED*/ 36 krb5_error_code KRB5_CALLCONV_C 37 krb5_build_principal_ext(krb5_context context, krb5_principal * princ, 38 unsigned int rlen, const char * realm, ...) 39 { 40 va_list ap; 41 register int i, count = 0; 42 register unsigned int size; 43 register char *next; 44 char *tmpdata; 45 krb5_data *princ_data; 46 krb5_principal princ_ret; 47 48 va_start(ap, realm); 49 /* count up */ 50 while (va_arg(ap, int) != 0) { 51 (void)va_arg(ap, char *); /* pass one up */ 52 count++; 53 } 54 va_end(ap); 55 56 /* we do a 2-pass to avoid the need to guess on allocation needs 57 cf. bld_princ.c */ 58 /* get space for array */ 59 princ_data = (krb5_data *) malloc(sizeof(krb5_data) * count); 60 if (!princ_data) 61 return ENOMEM; 62 princ_ret = (krb5_principal) malloc(sizeof(krb5_principal_data)); 63 if (!princ_ret) { 64 krb5_xfree(princ_data); 65 return ENOMEM; 66 } 67 princ_ret->data = princ_data; 68 princ_ret->length = count; 69 tmpdata = malloc(rlen+1); 70 if (!tmpdata) { 71 krb5_xfree(princ_data); 72 krb5_xfree(princ_ret); 73 return ENOMEM; 74 } 75 krb5_princ_set_realm_length(context, princ_ret, rlen); 76 krb5_princ_set_realm_data(context, princ_ret, tmpdata); 77 memcpy(tmpdata, realm, rlen); 78 tmpdata[rlen] = 0; 79 80 /* process rest of components */ 81 va_start(ap, realm); 82 for (i = 0; i < count; i++) { 83 size = va_arg(ap, unsigned int); 84 next = va_arg(ap, char *); 85 princ_data[i].length = size; 86 princ_data[i].data = malloc(size+1); 87 if (!princ_data[i].data) 88 goto free_out; 89 memcpy(princ_data[i].data, next, size); 90 princ_data[i].data[size] = 0; 91 } 92 va_end(ap); 93 *princ = princ_ret; 94 krb5_princ_type(context, princ_ret) = KRB5_NT_UNKNOWN; 95 return 0; 96 97 free_out: 98 while (i-- >= 0) 99 krb5_xfree(princ_data[i].data); 100 krb5_xfree(princ_data); 101 krb5_xfree(princ_ret); 102 va_end(ap); 103 return ENOMEM; 104 } 105