17c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27c478bd9Sstevel@tonic-gate 
37c478bd9Sstevel@tonic-gate /*
47c478bd9Sstevel@tonic-gate  * lib/krb5/ccache/ser_rc.c
57c478bd9Sstevel@tonic-gate  *
67c478bd9Sstevel@tonic-gate  * Copyright 1995 by the Massachusetts Institute of Technology.
77c478bd9Sstevel@tonic-gate  * All Rights Reserved.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
107c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
117c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
127c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
157c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
167c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
177c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
187c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
197c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
207c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
217c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
227c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
237c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
247c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
257c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
267c478bd9Sstevel@tonic-gate  * or implied warranty.
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * ser_rcdfl.c - Serialize replay cache context.
327c478bd9Sstevel@tonic-gate  */
33*505d05c7Sgtb #include "k5-int.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * Routines to deal with externalizing krb5_ccache.
377c478bd9Sstevel@tonic-gate  *	krb5_ccache_size();
387c478bd9Sstevel@tonic-gate  *	krb5_ccache_externalize();
397c478bd9Sstevel@tonic-gate  *	krb5_ccache_internalize();
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate static krb5_error_code krb5_ccache_size
42*505d05c7Sgtb 	(krb5_context, krb5_pointer, size_t *);
437c478bd9Sstevel@tonic-gate static krb5_error_code krb5_ccache_externalize
44*505d05c7Sgtb 	(krb5_context, krb5_pointer, krb5_octet **, size_t *);
457c478bd9Sstevel@tonic-gate static krb5_error_code krb5_ccache_internalize
46*505d05c7Sgtb 	(krb5_context,krb5_pointer *, krb5_octet **, size_t *);
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * Serialization entry for this type.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate static const krb5_ser_entry krb5_ccache_ser_entry = {
527c478bd9Sstevel@tonic-gate     KV5M_CCACHE,			/* Type			*/
537c478bd9Sstevel@tonic-gate     krb5_ccache_size,			/* Sizer routine	*/
547c478bd9Sstevel@tonic-gate     krb5_ccache_externalize,		/* Externalize routine	*/
557c478bd9Sstevel@tonic-gate     krb5_ccache_internalize		/* Internalize routine	*/
567c478bd9Sstevel@tonic-gate };
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * krb5_ccache_size()	- Determine the size required to externalize
607c478bd9Sstevel@tonic-gate  *				  this krb5_ccache variant.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate static krb5_error_code
krb5_ccache_size(krb5_context kcontext,krb5_pointer arg,size_t * sizep)63*505d05c7Sgtb krb5_ccache_size(krb5_context kcontext, krb5_pointer arg, size_t *sizep)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate     krb5_error_code	kret;
667c478bd9Sstevel@tonic-gate     krb5_ccache		ccache;
677c478bd9Sstevel@tonic-gate     size_t		required;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate     kret = EINVAL;
70*505d05c7Sgtb     if ((ccache = (krb5_ccache) arg)) {
717c478bd9Sstevel@tonic-gate 	/*
727c478bd9Sstevel@tonic-gate 	 * Saving FILE: variants of krb5_ccache requires at minimum:
737c478bd9Sstevel@tonic-gate 	 *	krb5_int32	for KV5M_CCACHE
747c478bd9Sstevel@tonic-gate 	 *	krb5_int32	for length of ccache name.
757c478bd9Sstevel@tonic-gate 	 *	krb5_int32	for KV5M_CCACHE
767c478bd9Sstevel@tonic-gate 	 */
777c478bd9Sstevel@tonic-gate 	required = sizeof(krb5_int32) * 3;
787c478bd9Sstevel@tonic-gate 	if (ccache->ops && ccache->ops->prefix)
797c478bd9Sstevel@tonic-gate 	    required += (strlen(ccache->ops->prefix)+1);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	/*
827c478bd9Sstevel@tonic-gate 	 * The ccache name is formed as follows:
837c478bd9Sstevel@tonic-gate 	 *	<prefix>:<name>
847c478bd9Sstevel@tonic-gate 	 */
85*505d05c7Sgtb 	required += strlen(krb5_cc_get_name(kcontext, ccache));
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	kret = 0;
887c478bd9Sstevel@tonic-gate 	*sizep += required;
897c478bd9Sstevel@tonic-gate     }
907c478bd9Sstevel@tonic-gate     return(kret);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * krb5_ccache_externalize()	- Externalize the krb5_ccache.
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate static krb5_error_code
krb5_ccache_externalize(krb5_context kcontext,krb5_pointer arg,krb5_octet ** buffer,size_t * lenremain)97*505d05c7Sgtb krb5_ccache_externalize(krb5_context kcontext, krb5_pointer arg, krb5_octet **buffer, size_t *lenremain)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate     krb5_error_code	kret;
1007c478bd9Sstevel@tonic-gate     krb5_ccache		ccache;
1017c478bd9Sstevel@tonic-gate     size_t		required;
1027c478bd9Sstevel@tonic-gate     krb5_octet		*bp;
1037c478bd9Sstevel@tonic-gate     size_t		remain;
1047c478bd9Sstevel@tonic-gate     char		*ccname;
1057c478bd9Sstevel@tonic-gate     size_t		namelen;
106*505d05c7Sgtb     const char		*fnamep;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate     required = 0;
1097c478bd9Sstevel@tonic-gate     bp = *buffer;
1107c478bd9Sstevel@tonic-gate     remain = *lenremain;
1117c478bd9Sstevel@tonic-gate     kret = EINVAL;
112*505d05c7Sgtb     if ((ccache = (krb5_ccache) arg)) {
1137c478bd9Sstevel@tonic-gate 	kret = ENOMEM;
1147c478bd9Sstevel@tonic-gate 	if (!krb5_ccache_size(kcontext, arg, &required) &&
1157c478bd9Sstevel@tonic-gate 	    (required <= remain)) {
1167c478bd9Sstevel@tonic-gate 	    /* Our identifier */
1177c478bd9Sstevel@tonic-gate 	    (void) krb5_ser_pack_int32(KV5M_CCACHE, &bp, &remain);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	    /* Calculate the length of the name */
1207c478bd9Sstevel@tonic-gate 	    namelen = (ccache->ops && ccache->ops->prefix) ?
1217c478bd9Sstevel@tonic-gate 		strlen(ccache->ops->prefix)+1 : 0;
122*505d05c7Sgtb 	    fnamep = krb5_cc_get_name(kcontext, ccache);
1237c478bd9Sstevel@tonic-gate 	    namelen += (strlen(fnamep)+1);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	    if ((ccname = (char *) malloc(namelen))) {
1267c478bd9Sstevel@tonic-gate 		/* Format the ccache name. */
1277c478bd9Sstevel@tonic-gate 		if (ccache->ops && ccache->ops->prefix)
1287c478bd9Sstevel@tonic-gate 		    sprintf(ccname, "%s:%s", ccache->ops->prefix, fnamep);
1297c478bd9Sstevel@tonic-gate 		else
1307c478bd9Sstevel@tonic-gate 		    strcpy(ccname, fnamep);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 		/* Put the length of the file name */
1337c478bd9Sstevel@tonic-gate 		(void) krb5_ser_pack_int32((krb5_int32) strlen(ccname),
1347c478bd9Sstevel@tonic-gate 					   &bp, &remain);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 		/* Put the name */
1377c478bd9Sstevel@tonic-gate 		(void) krb5_ser_pack_bytes((krb5_octet *) ccname,
1387c478bd9Sstevel@tonic-gate 					   strlen(ccname),
1397c478bd9Sstevel@tonic-gate 					   &bp, &remain);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		/* Put the trailer */
1427c478bd9Sstevel@tonic-gate 		(void) krb5_ser_pack_int32(KV5M_CCACHE, &bp, &remain);
1437c478bd9Sstevel@tonic-gate 		kret = 0;
1447c478bd9Sstevel@tonic-gate 		*buffer = bp;
1457c478bd9Sstevel@tonic-gate 		*lenremain = remain;
1467c478bd9Sstevel@tonic-gate 		free(ccname);
1477c478bd9Sstevel@tonic-gate 	    }
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate     }
1507c478bd9Sstevel@tonic-gate     return(kret);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * krb5_ccache_internalize()	- Internalize the krb5_ccache.
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate static krb5_error_code
krb5_ccache_internalize(krb5_context kcontext,krb5_pointer * argp,krb5_octet ** buffer,size_t * lenremain)157*505d05c7Sgtb krb5_ccache_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_octet **buffer, size_t *lenremain)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate     krb5_error_code	kret;
1607c478bd9Sstevel@tonic-gate     krb5_ccache		ccache;
1617c478bd9Sstevel@tonic-gate     krb5_int32		ibuf;
1627c478bd9Sstevel@tonic-gate     krb5_octet		*bp;
1637c478bd9Sstevel@tonic-gate     size_t		remain;
1647c478bd9Sstevel@tonic-gate     char		*ccname;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate     bp = *buffer;
1677c478bd9Sstevel@tonic-gate     remain = *lenremain;
1687c478bd9Sstevel@tonic-gate     kret = EINVAL;
1697c478bd9Sstevel@tonic-gate     /* Read our magic number */
1707c478bd9Sstevel@tonic-gate     if (krb5_ser_unpack_int32(&ibuf, &bp, &remain))
1717c478bd9Sstevel@tonic-gate 	ibuf = 0;
1727c478bd9Sstevel@tonic-gate     if (ibuf == KV5M_CCACHE) {
1737c478bd9Sstevel@tonic-gate 	kret = ENOMEM;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/* Get the length of the ccache name */
1767c478bd9Sstevel@tonic-gate 	kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if (!kret &&
1797c478bd9Sstevel@tonic-gate 	    (ccname = (char *) malloc((size_t) (ibuf+1))) &&
1807c478bd9Sstevel@tonic-gate 	    !(kret = krb5_ser_unpack_bytes((krb5_octet *) ccname,
1817c478bd9Sstevel@tonic-gate 					   (size_t) ibuf,
1827c478bd9Sstevel@tonic-gate 					   &bp, &remain))) {
1837c478bd9Sstevel@tonic-gate 	    ccname[ibuf] = '\0';
1847c478bd9Sstevel@tonic-gate 	    if (!(kret = krb5_cc_resolve(kcontext, ccname, &ccache)) &&
1857c478bd9Sstevel@tonic-gate 		!(kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain)) &&
1867c478bd9Sstevel@tonic-gate 		(ibuf == KV5M_CCACHE)) {
1877c478bd9Sstevel@tonic-gate 		*buffer = bp;
1887c478bd9Sstevel@tonic-gate 		*lenremain = remain;
1897c478bd9Sstevel@tonic-gate 		*argp = (krb5_pointer) ccache;
1907c478bd9Sstevel@tonic-gate 	    }
1917c478bd9Sstevel@tonic-gate 	    free(ccname);
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate     }
1947c478bd9Sstevel@tonic-gate     return(kret);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  * Register the ccache serializer.
1997c478bd9Sstevel@tonic-gate  */
200*505d05c7Sgtb krb5_error_code KRB5_CALLCONV
krb5_ser_ccache_init(krb5_context kcontext)201*505d05c7Sgtb krb5_ser_ccache_init(krb5_context kcontext)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate     return(krb5_register_serializer(kcontext, &krb5_ccache_ser_entry));
2047c478bd9Sstevel@tonic-gate }
205