2 * lib/krb5/ccache/stdio/scc_gennew.c
4 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. M.I.T. makes no representations about the suitability of
20 * this software for any purpose. It is provided "as is" without express
21 * or implied warranty.
24 * This file contains the source code for krb5_scc_generate_new.
30 extern krb5_cc_ops krb5_scc_ops;
34 * Creates a new file cred cache whose name is guaranteed to be
35 * unique. The name begins with the string TKT_ROOT (from scc.h).
36 * The cache is not opened, but the new filename is reserved.
39 * The filled in krb5_ccache id.
42 * KRB5_CC_NOMEM - there was insufficient memory to allocate the
43 * krb5_ccache. id is undefined.
44 * system errors (from open)
47 krb5_scc_generate_new (context, id)
53 krb5_error_code retcode = 0;
54 char scratch[sizeof(TKT_ROOT)+6+1]; /* +6 for the scratch part, +1 for
58 lid = (krb5_ccache) malloc(sizeof(struct _krb5_ccache));
62 lid->ops = &krb5_scc_ops;
64 (void) strcpy(scratch, TKT_ROOT);
65 (void) strcat(scratch, "XXXXXX");
68 lid->data = (krb5_pointer) malloc(sizeof(krb5_scc_data));
69 if (lid->data == NULL) {
74 ((krb5_scc_data *) lid->data)->filename = (char *)
75 malloc(strlen(scratch) + 1);
76 if (((krb5_scc_data *) lid->data)->filename == NULL) {
77 krb5_xfree(((krb5_scc_data *) lid->data));
82 ((krb5_scc_data *) lid->data)->flags = 0;
83 ((krb5_scc_data *) lid->data)->file = 0;
85 /* Set up the filename */
86 strcpy(((krb5_scc_data *) lid->data)->filename, scratch);
88 /* Make sure the file name is useable */
89 #if defined(__STDC__) || defined(_WINDOWS)
90 f = fopen (((krb5_scc_data *) lid->data)->filename, "wb+");
92 f = fopen (((krb5_scc_data *) lid->data)->filename, "w+");
95 retcode = krb5_scc_interpret (context, errno);
98 unsigned char scc_fvno[2];
100 scc_fvno[0] = (unsigned char) (KRB5_SCC_DEFAULT_FVNO >> 8);
101 scc_fvno[1] = (unsigned char) (KRB5_SCC_DEFAULT_FVNO & 0xFF);
103 if (!fwrite((char *)scc_fvno, sizeof(scc_fvno), 1, f)) {
104 retcode = krb5_scc_interpret(context, errno);
106 (void) remove(((krb5_scc_data *) lid->data)->filename);
109 if (fclose(f) == EOF) {
110 retcode = krb5_scc_interpret(context, errno);
111 (void) remove(((krb5_scc_data *) lid->data)->filename);
118 krb5_xfree(((krb5_scc_data *) lid->data)->filename);
119 krb5_xfree(((krb5_scc_data *) lid->data));