cbe296b38711b6df49c4740ec401ef5943db40c6
[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 <afs/param.h>
48 #include <afsconfig.h>
49
50 RCSID("$Header$");
51
52 #ifdef AFS_NT40_ENV
53 #include <fcntl.h>
54 #include <io.h>
55 #else
56 #include <sys/file.h>
57 #endif
58 #include <sys/types.h>
59 #include <rx/xdr.h>
60 #include <errno.h>
61 #include <afs/auth.h>
62 #include "kauth.h"
63 #include "kautils.h"
64
65 afs_int32 krb_write_ticket_file (realm)
66   char *realm;
67 {   char  ticket_file[AFSDIR_PATH_MAX];
68     int   fd;
69     int   count;
70     afs_int32  code;
71     int   lifetime, kvno;
72     char *tf_name;
73     struct ktc_principal client, server;
74     struct ktc_token token;
75
76     if ((strlen(realm) >= sizeof(client.cell))) return KABADNAME;
77     strcpy (server.name, KA_TGS_NAME);
78     strcpy (server.instance, realm);
79     lcstring (server.cell, realm, sizeof(server.cell));
80
81     code = ktc_GetToken (&server, &token, sizeof(struct ktc_token), &client);
82     if (code) return code;
83
84     /* Use the KRBTKFILE environment variable if it exists, otherwise fall
85      * back upon /tmp/tkt(uid}. 
86      */
87     if (tf_name = (char *) getenv("KRBTKFILE"))
88         (void) sprintf(ticket_file, "%s", tf_name);
89     else
90         (void) sprintf(ticket_file, "%s/tkt%d", gettmpdir(), getuid());
91     fd = open (ticket_file, O_WRONLY+O_CREAT+O_TRUNC, 0700);
92     if (fd <= 0) return errno;
93
94     /* write client name as file header */
95
96     count = strlen(client.name) + 1;
97     if (write(fd, client.name, count) != count)
98         goto bad;
99
100     count = strlen(client.instance) + 1;
101     if (write(fd, client.instance, count) != count)
102         goto bad;
103
104     /* Write the ticket and associated data */
105     /* Service */
106     count = strlen(server.name) + 1;
107     if (write(fd, server.name, count) != count)
108         goto bad;
109     /* Instance */
110     count = strlen(server.instance) + 1;
111     if (write(fd, server.instance, count) != count)
112         goto bad;
113     /* Realm */
114     ucstring (server.cell, server.cell, sizeof(server.cell));
115     count = strlen(server.cell) + 1;
116     if (write(fd, server.cell, count) != count)
117         goto bad;
118     /* Session key */
119     if (write(fd, (char *) &token.sessionKey, 8) != 8)
120         goto bad;
121     /* Lifetime */
122     lifetime = time_to_life (token.startTime, token.endTime);
123     if (write(fd, (char *) &lifetime, sizeof(int)) != sizeof(int))
124         goto bad;
125     /* Key vno */
126     kvno = token.kvno;
127     if (write(fd, (char *) &kvno, sizeof(int)) != sizeof(int))
128         goto bad;
129     /* Tkt length */
130     if (write(fd, (char *) &(token.ticketLen), sizeof(int)) !=
131         sizeof(int))
132         goto bad;
133     /* Ticket */
134     count = token.ticketLen;
135     if (write(fd, (char *) (token.ticket), count) != count)
136         goto bad;
137     /* Issue date */
138     if (write(fd, (char *) &token.startTime, sizeof(afs_int32))
139         != sizeof(afs_int32))
140         goto bad;
141     close (fd);
142     return 0;
143
144   bad:
145     close (fd);
146     return -1;
147 }