2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
14 /* This modified from the code in kerberos/src/lib/krb/tf_util.c. */
17 * This file contains routines for manipulating the ticket cache file.
19 * The ticket file is in the following format:
21 * principal's name (null-terminated string)
22 * principal's instance (null-terminated string)
29 * Where "CREDENTIAL_x" consists of the following fixed-length
30 * fields from the CREDENTIALS structure (see "krb.h"):
32 * char service[ANAME_SZ]
33 * char instance[INST_SZ]
34 * char realm[REALM_SZ]
39 * afs_int32 issue_date
44 /* Inspite of what the above comment suggests the fields are not fixed length
45 but null terminated as you might figure, except for the ticket which is
46 preceded by a 4 byte length. All fields in host order. 890306 */
47 #include <afsconfig.h>
48 #include <afs/param.h>
58 #include "kauth_internal.h"
61 krb_write_ticket_file(char *realm
)
68 struct ktc_principal client
, server
;
69 struct ktc_token token
;
71 if ((strlen(realm
) >= sizeof(client
.cell
)))
73 strcpy(server
.name
, KA_TGS_NAME
);
74 strcpy(server
.instance
, realm
);
75 lcstring(server
.cell
, realm
, sizeof(server
.cell
));
77 code
= ktc_GetToken(&server
, &token
, sizeof(struct ktc_token
), &client
);
81 /* Use the KRBTKFILE environment variable if it exists, otherwise fall
82 * back upon /tmp/tkt(uid}.
84 if ((tf_name
= (char *)getenv("KRBTKFILE")))
85 fd
= open(tf_name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0700);
87 count
= asprintf(&tf_name
, "%s/tkt%d", gettmpdir(), getuid());
88 if (count
< 0 || tf_name
== NULL
)
90 fd
= open(tf_name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0700);
97 /* write client name as file header */
99 count
= strlen(client
.name
) + 1;
100 if (write(fd
, client
.name
, count
) != count
)
103 count
= strlen(client
.instance
) + 1;
104 if (write(fd
, client
.instance
, count
) != count
)
107 /* Write the ticket and associated data */
109 count
= strlen(server
.name
) + 1;
110 if (write(fd
, server
.name
, count
) != count
)
113 count
= strlen(server
.instance
) + 1;
114 if (write(fd
, server
.instance
, count
) != count
)
117 ucstring(server
.cell
, server
.cell
, sizeof(server
.cell
));
118 count
= strlen(server
.cell
) + 1;
119 if (write(fd
, server
.cell
, count
) != count
)
122 if (write(fd
, (char *)&token
.sessionKey
, 8) != 8)
125 lifetime
= time_to_life(token
.startTime
, token
.endTime
);
126 if (write(fd
, (char *)&lifetime
, sizeof(int)) != sizeof(int))
130 if (write(fd
, (char *)&kvno
, sizeof(int)) != sizeof(int))
133 if (write(fd
, (char *)&(token
.ticketLen
), sizeof(int)) != sizeof(int))
136 count
= token
.ticketLen
;
137 if (write(fd
, (char *)(token
.ticket
), count
) != count
)
140 if (write(fd
, (char *)&(token
.startTime
), sizeof(afs_int32
))
141 != sizeof(afs_int32
))