17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * lib/krb5/ccache/ser_rc.c
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * Copyright 1995 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.
117c478bd9Sstevel@tonic-gate  *
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.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * ser_rcdfl.c - Serialize replay cache context.
307c478bd9Sstevel@tonic-gate  */
31505d05c7Sgtb #include "k5-int.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * Routines to deal with externalizing krb5_ccache.
357c478bd9Sstevel@tonic-gate  *	krb5_ccache_size();
367c478bd9Sstevel@tonic-gate  *	krb5_ccache_externalize();
377c478bd9Sstevel@tonic-gate  *	krb5_ccache_internalize();
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate static krb5_error_code krb5_ccache_size
40505d05c7Sgtb 	(krb5_context, krb5_pointer, size_t *);
417c478bd9Sstevel@tonic-gate static krb5_error_code krb5_ccache_externalize
42505d05c7Sgtb 	(krb5_context, krb5_pointer, krb5_octet **, size_t *);
437c478bd9Sstevel@tonic-gate static krb5_error_code krb5_ccache_internalize
44505d05c7Sgtb 	(krb5_context,krb5_pointer *, krb5_octet **, size_t *);
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * Serialization entry for this type.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate static const krb5_ser_entry krb5_ccache_ser_entry = {
507c478bd9Sstevel@tonic-gate     KV5M_CCACHE,			/* Type			*/
517c478bd9Sstevel@tonic-gate     krb5_ccache_size,			/* Sizer routine	*/
527c478bd9Sstevel@tonic-gate     krb5_ccache_externalize,		/* Externalize routine	*/
537c478bd9Sstevel@tonic-gate     krb5_ccache_internalize		/* Internalize routine	*/
547c478bd9Sstevel@tonic-gate };
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * krb5_ccache_size()	- Determine the size required to externalize
587c478bd9Sstevel@tonic-gate  *				  this krb5_ccache variant.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate static krb5_error_code
krb5_ccache_size(krb5_context kcontext,krb5_pointer arg,size_t * sizep)61505d05c7Sgtb krb5_ccache_size(krb5_context kcontext, krb5_pointer arg, size_t *sizep)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate     krb5_error_code	kret;
647c478bd9Sstevel@tonic-gate     krb5_ccache		ccache;
657c478bd9Sstevel@tonic-gate     size_t		required;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate     kret = EINVAL;
68505d05c7Sgtb     if ((ccache = (krb5_ccache) arg)) {
697c478bd9Sstevel@tonic-gate 	/*
707c478bd9Sstevel@tonic-gate 	 * Saving FILE: variants of krb5_ccache requires at minimum:
717c478bd9Sstevel@tonic-gate 	 *	krb5_int32	for KV5M_CCACHE
727c478bd9Sstevel@tonic-gate 	 *	krb5_int32	for length of ccache name.
737c478bd9Sstevel@tonic-gate 	 *	krb5_int32	for KV5M_CCACHE
747c478bd9Sstevel@tonic-gate 	 */
757c478bd9Sstevel@tonic-gate 	required = sizeof(krb5_int32) * 3;
767c478bd9Sstevel@tonic-gate 	if (ccache->ops && ccache->ops->prefix)
777c478bd9Sstevel@tonic-gate 	    required += (strlen(ccache->ops->prefix)+1);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	/*
807c478bd9Sstevel@tonic-gate 	 * The ccache name is formed as follows:
817c478bd9Sstevel@tonic-gate 	 *	<prefix>:<name>
827c478bd9Sstevel@tonic-gate 	 */
83505d05c7Sgtb 	required += strlen(krb5_cc_get_name(kcontext, ccache));
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	kret = 0;
867c478bd9Sstevel@tonic-gate 	*sizep += required;
877c478bd9Sstevel@tonic-gate     }
887c478bd9Sstevel@tonic-gate     return(kret);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * krb5_ccache_externalize()	- Externalize the krb5_ccache.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate static krb5_error_code
krb5_ccache_externalize(krb5_context kcontext,krb5_pointer arg,krb5_octet ** buffer,size_t * lenremain)95505d05c7Sgtb krb5_ccache_externalize(krb5_context kcontext, krb5_pointer arg, krb5_octet **buffer, size_t *lenremain)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate     krb5_error_code	kret;
987c478bd9Sstevel@tonic-gate     krb5_ccache		ccache;
997c478bd9Sstevel@tonic-gate     size_t		required;
1007c478bd9Sstevel@tonic-gate     krb5_octet		*bp;
1017c478bd9Sstevel@tonic-gate     size_t		remain;
1027c478bd9Sstevel@tonic-gate     char		*ccname;
1037c478bd9Sstevel@tonic-gate     size_t		namelen;
104505d05c7Sgtb     const char		*fnamep;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate     required = 0;
1077c478bd9Sstevel@tonic-gate     bp = *buffer;
1087c478bd9Sstevel@tonic-gate     remain = *lenremain;
1097c478bd9Sstevel@tonic-gate     kret = EINVAL;
110505d05c7Sgtb     if ((ccache = (krb5_ccache) arg)) {
1117c478bd9Sstevel@tonic-gate 	kret = ENOMEM;
1127c478bd9Sstevel@tonic-gate 	if (!krb5_ccache_size(kcontext, arg, &required) &&
1137c478bd9Sstevel@tonic-gate 	    (required <= remain)) {
1147c478bd9Sstevel@tonic-gate 	    /* Our identifier */
1157c478bd9Sstevel@tonic-gate 	    (void) krb5_ser_pack_int32(KV5M_CCACHE, &bp, &remain);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	    /* Calculate the length of the name */
1187c478bd9Sstevel@tonic-gate 	    namelen = (ccache->ops && ccache->ops->prefix) ?
1197c478bd9Sstevel@tonic-gate 		strlen(ccache->ops->prefix)+1 : 0;
120505d05c7Sgtb 	    fnamep = krb5_cc_get_name(kcontext, ccache);
1217c478bd9Sstevel@tonic-gate 	    namelen += (strlen(fnamep)+1);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	    if ((ccname = (char *) malloc(namelen))) {
1247c478bd9Sstevel@tonic-gate 		/* Format the ccache name. */
1257c478bd9Sstevel@tonic-gate 		if (ccache->ops && ccache->ops->prefix)
1267c478bd9Sstevel@tonic-gate 		    sprintf(ccname, "%s:%s", ccache->ops->prefix, fnamep);
1277c478bd9Sstevel@tonic-gate 		else
1287c478bd9Sstevel@tonic-gate 		    strcpy(ccname, fnamep);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		/* Put the length of the file name */
1317c478bd9Sstevel@tonic-gate 		(void) krb5_ser_pack_int32((krb5_int32) strlen(ccname),
1327c478bd9Sstevel@tonic-gate 					   &bp, &remain);
133*1da57d55SToomas Soome 
1347c478bd9Sstevel@tonic-gate 		/* Put the name */
1357c478bd9Sstevel@tonic-gate 		(void) krb5_ser_pack_bytes((krb5_octet *) ccname,
1367c478bd9Sstevel@tonic-gate 					   strlen(ccname),
1377c478bd9Sstevel@tonic-gate 					   &bp, &remain);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		/* Put the trailer */
1407c478bd9Sstevel@tonic-gate 		(void) krb5_ser_pack_int32(KV5M_CCACHE, &bp, &remain);
1417c478bd9Sstevel@tonic-gate 		kret = 0;
1427c478bd9Sstevel@tonic-gate 		*buffer = bp;
1437c478bd9Sstevel@tonic-gate 		*lenremain = remain;
1447c478bd9Sstevel@tonic-gate 		free(ccname);
1457c478bd9Sstevel@tonic-gate 	    }
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate     }
1487c478bd9Sstevel@tonic-gate     return(kret);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate  * krb5_ccache_internalize()	- Internalize the krb5_ccache.
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate static krb5_error_code
krb5_ccache_internalize(krb5_context kcontext,krb5_pointer * argp,krb5_octet ** buffer,size_t * lenremain)155505d05c7Sgtb krb5_ccache_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_octet **buffer, size_t *lenremain)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate     krb5_error_code	kret;
1587c478bd9Sstevel@tonic-gate     krb5_ccache		ccache;
1597c478bd9Sstevel@tonic-gate     krb5_int32		ibuf;
1607c478bd9Sstevel@tonic-gate     krb5_octet		*bp;
1617c478bd9Sstevel@tonic-gate     size_t		remain;
1627c478bd9Sstevel@tonic-gate     char		*ccname;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate     bp = *buffer;
1657c478bd9Sstevel@tonic-gate     remain = *lenremain;
1667c478bd9Sstevel@tonic-gate     kret = EINVAL;
1677c478bd9Sstevel@tonic-gate     /* Read our magic number */
1687c478bd9Sstevel@tonic-gate     if (krb5_ser_unpack_int32(&ibuf, &bp, &remain))
1697c478bd9Sstevel@tonic-gate 	ibuf = 0;
1707c478bd9Sstevel@tonic-gate     if (ibuf == KV5M_CCACHE) {
1717c478bd9Sstevel@tonic-gate 	kret = ENOMEM;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/* Get the length of the ccache name */
1747c478bd9Sstevel@tonic-gate 	kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (!kret &&
1777c478bd9Sstevel@tonic-gate 	    (ccname = (char *) malloc((size_t) (ibuf+1))) &&
1787c478bd9Sstevel@tonic-gate 	    !(kret = krb5_ser_unpack_bytes((krb5_octet *) ccname,
1797c478bd9Sstevel@tonic-gate 					   (size_t) ibuf,
1807c478bd9Sstevel@tonic-gate 					   &bp, &remain))) {
1817c478bd9Sstevel@tonic-gate 	    ccname[ibuf] = '\0';
1827c478bd9Sstevel@tonic-gate 	    if (!(kret = krb5_cc_resolve(kcontext, ccname, &ccache)) &&
1837c478bd9Sstevel@tonic-gate 		!(kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain)) &&
1847c478bd9Sstevel@tonic-gate 		(ibuf == KV5M_CCACHE)) {
1857c478bd9Sstevel@tonic-gate 		*buffer = bp;
1867c478bd9Sstevel@tonic-gate 		*lenremain = remain;
1877c478bd9Sstevel@tonic-gate 		*argp = (krb5_pointer) ccache;
1887c478bd9Sstevel@tonic-gate 	    }
1897c478bd9Sstevel@tonic-gate 	    free(ccname);
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate     }
1927c478bd9Sstevel@tonic-gate     return(kret);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /*
1967c478bd9Sstevel@tonic-gate  * Register the ccache serializer.
1977c478bd9Sstevel@tonic-gate  */
198505d05c7Sgtb krb5_error_code KRB5_CALLCONV
krb5_ser_ccache_init(krb5_context kcontext)199505d05c7Sgtb krb5_ser_ccache_init(krb5_context kcontext)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate     return(krb5_register_serializer(kcontext, &krb5_ccache_ser_entry));
2027c478bd9Sstevel@tonic-gate }
203