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