9bd3eae5210f5bbccf52e36b19093d8512e79845
[openafs.git] / src / kauth / krb_tf.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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
8  */
9
10 /*
11  * ALL RIGHTS RESERVED
12  */
13
14 /* This modified from the code in kerberos/src/lib/krb/tf_util.c. */
15
16 /*
17  * This file contains routines for manipulating the ticket cache file.
18  *
19  * The ticket file is in the following format:
20  *
21  *      principal's name        (null-terminated string)
22  *      principal's instance    (null-terminated string)
23  *      CREDENTIAL_1
24  *      CREDENTIAL_2
25  *      ...
26  *      CREDENTIAL_n
27  *      EOF
28  *
29  *      Where "CREDENTIAL_x" consists of the following fixed-length
30  *      fields from the CREDENTIALS structure (see "krb.h"):
31  *
32  *              char            service[ANAME_SZ]
33  *              char            instance[INST_SZ]
34  *              char            realm[REALM_SZ]
35  *              C_Block         session
36  *              int             lifetime
37  *              int             kvno
38  *              KTEXT_ST        ticket_st
39  *              afs_int32            issue_date
40  *
41  * . . .
42  */
43
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>
49
50 #include <roken.h>
51
52 #include <rx/xdr.h>
53 #include <afs/auth.h>
54
55 #include "kauth.h"
56 #include "kautils.h"
57 #include "kauth_internal.h"
58
59 afs_int32
60 krb_write_ticket_file(char *realm)
61 {
62     char ticket_file[AFSDIR_PATH_MAX];
63     int fd;
64     int count;
65     afs_int32 code;
66     int lifetime, kvno;
67     char *tf_name;
68     struct ktc_principal client, server;
69     struct ktc_token token;
70
71     if ((strlen(realm) >= sizeof(client.cell)))
72         return KABADNAME;
73     strcpy(server.name, KA_TGS_NAME);
74     strcpy(server.instance, realm);
75     lcstring(server.cell, realm, sizeof(server.cell));
76
77     code = ktc_GetToken(&server, &token, sizeof(struct ktc_token), &client);
78     if (code)
79         return code;
80
81     /* Use the KRBTKFILE environment variable if it exists, otherwise fall
82      * back upon /tmp/tkt(uid}.
83      */
84     if ((tf_name = (char *)getenv("KRBTKFILE")))
85         (void)sprintf(ticket_file, "%s", tf_name);
86     else
87         (void)sprintf(ticket_file, "%s/tkt%d", gettmpdir(), getuid());
88     fd = open(ticket_file, O_WRONLY + O_CREAT + O_TRUNC, 0700);
89     if (fd <= 0)
90         return errno;
91
92     /* write client name as file header */
93
94     count = strlen(client.name) + 1;
95     if (write(fd, client.name, count) != count)
96         goto bad;
97
98     count = strlen(client.instance) + 1;
99     if (write(fd, client.instance, count) != count)
100         goto bad;
101
102     /* Write the ticket and associated data */
103     /* Service */
104     count = strlen(server.name) + 1;
105     if (write(fd, server.name, count) != count)
106         goto bad;
107     /* Instance */
108     count = strlen(server.instance) + 1;
109     if (write(fd, server.instance, count) != count)
110         goto bad;
111     /* Realm */
112     ucstring(server.cell, server.cell, sizeof(server.cell));
113     count = strlen(server.cell) + 1;
114     if (write(fd, server.cell, count) != count)
115         goto bad;
116     /* Session key */
117     if (write(fd, (char *)&token.sessionKey, 8) != 8)
118         goto bad;
119     /* Lifetime */
120     lifetime = time_to_life(token.startTime, token.endTime);
121     if (write(fd, (char *)&lifetime, sizeof(int)) != sizeof(int))
122         goto bad;
123     /* Key vno */
124     kvno = token.kvno;
125     if (write(fd, (char *)&kvno, sizeof(int)) != sizeof(int))
126         goto bad;
127     /* Tkt length */
128     if (write(fd, (char *)&(token.ticketLen), sizeof(int)) != sizeof(int))
129         goto bad;
130     /* Ticket */
131     count = token.ticketLen;
132     if (write(fd, (char *)(token.ticket), count) != count)
133         goto bad;
134     /* Issue date */
135     if (write(fd, (char *)&(token.startTime), sizeof(afs_int32))
136         != sizeof(afs_int32))
137         goto bad;
138     close(fd);
139     return 0;
140
141   bad:
142     close(fd);
143     return -1;
144 }