~ubuntu-branches/ubuntu/maverick/krb5/maverick

« back to all changes in this revision

Viewing changes to src/lib/krb4/in_tkt.c

  • Committer: Bazaar Package Importer
  • Author(s): Sam Hartman
  • Date: 2009-05-07 16:16:34 UTC
  • mfrom: (13.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20090507161634-xqyk0s9na0le4flj
Tags: 1.7dfsg~beta1-4
When  decrypting the TGS response fails with the subkey, try with the
session key to work around Heimdal bug, Closes: #527353 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * lib/krb4/in_tkt.c
3
 
 *
4
 
 * Copyright 1985, 1986, 1987, 1988, 2000, 2001 by the Massachusetts
5
 
 * Institute of Technology.  All Rights Reserved.
6
 
 *
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.
11
 
 * 
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.  Furthermore if you modify this software you must label
20
 
 * your software as modified software and not distribute it in such a
21
 
 * fashion that it might be confused with the original M.I.T. software.
22
 
 * M.I.T. makes no representations about the suitability of
23
 
 * this software for any purpose.  It is provided "as is" without express
24
 
 * or implied warranty.
25
 
 */
26
 
 
27
 
#include <stdio.h>
28
 
#include <string.h>
29
 
#include <errno.h>
30
 
#include "krb.h"
31
 
#include <fcntl.h>
32
 
#include <sys/stat.h>
33
 
#include "autoconf.h"
34
 
#ifdef TKT_SHMEM
35
 
#include <sys/param.h>
36
 
#endif
37
 
#ifdef HAVE_UNISTD_H
38
 
#include <unistd.h>
39
 
#endif
40
 
 
41
 
extern int krb_debug;
42
 
 
43
 
/*
44
 
 * in_tkt() is used to initialize the ticket store.  It creates the
45
 
 * file to contain the tickets and writes the given user's name "pname"
46
 
 * and instance "pinst" in the file.  in_tkt() returns KSUCCESS on
47
 
 * success, or KFAILURE if something goes wrong.
48
 
 */
49
 
 
50
 
#include "k5-util.h"
51
 
#define do_seteuid krb5_seteuid
52
 
 
53
 
#ifndef O_SYNC
54
 
#define O_SYNC 0
55
 
#endif
56
 
 
57
 
int KRB5_CALLCONV
58
 
in_tkt(pname,pinst)
59
 
    char *pname;
60
 
    char *pinst;
61
 
{
62
 
    int tktfile;
63
 
    uid_t me, metoo, getuid(), geteuid();
64
 
    struct stat statpre, statpost;
65
 
    int count;
66
 
    const char *file = TKT_FILE;
67
 
    int fd;
68
 
    register int i;
69
 
    char charbuf[BUFSIZ];
70
 
    mode_t mask;
71
 
#ifdef TKT_SHMEM
72
 
    char shmidname[MAXPATHLEN];
73
 
#endif /* TKT_SHMEM */
74
 
 
75
 
    /* If ticket cache selector is null, use default cache.  */
76
 
    if (file == 0)
77
 
        file = tkt_string();
78
 
 
79
 
    me = getuid ();
80
 
    metoo = geteuid();
81
 
    if (lstat(file, &statpre) == 0) {
82
 
        if (statpre.st_uid != me || !(statpre.st_mode & S_IFREG)
83
 
            || statpre.st_nlink != 1 || statpre.st_mode & 077) {
84
 
            if (krb_debug)
85
 
                fprintf(stderr,"Error initializing %s",file);
86
 
            return(KFAILURE);
87
 
        }
88
 
        /*
89
 
         * Yes, we do uid twiddling here.  It's not optimal, but some
90
 
         * applications may expect that the ruid is what should really
91
 
         * own the ticket file, e.g. setuid applications.
92
 
         */
93
 
        if (me != metoo && do_seteuid(me) < 0)
94
 
            return KFAILURE;
95
 
        /* file already exists, and permissions appear ok, so nuke it */
96
 
        fd = open(file, O_RDWR|O_SYNC, 0);
97
 
        (void)unlink(file);
98
 
        if (me != metoo && do_seteuid(metoo) < 0)
99
 
            return KFAILURE;
100
 
        if (fd < 0) {
101
 
            goto out; /* can't zero it, but we can still try truncating it */
102
 
        }
103
 
 
104
 
        /*
105
 
         * Do some additional paranoid things.  The worst-case
106
 
         * situation is that a user may be fooled into opening a
107
 
         * non-regular file briefly if the file is in a directory with
108
 
         * improper permissions.
109
 
         */
110
 
        if (fstat(fd, &statpost) < 0) {
111
 
            (void)close(fd);
112
 
            goto out;
113
 
        }
114
 
        if (statpre.st_dev != statpost.st_dev
115
 
            || statpre.st_ino != statpost.st_ino) {
116
 
            (void)close(fd);
117
 
            errno = 0;
118
 
            goto out;
119
 
        }
120
 
 
121
 
        memset(charbuf, 0, sizeof(charbuf));
122
 
 
123
 
        for (i = 0; i < statpost.st_size; i += sizeof(charbuf))
124
 
            if (write(fd, charbuf, sizeof(charbuf)) != sizeof(charbuf)) {
125
 
#ifndef NO_FSYNC
126
 
                (void) fsync(fd);
127
 
#endif
128
 
                (void) close(fd);
129
 
                goto out;
130
 
            }
131
 
        
132
 
#ifndef NO_FSYNC
133
 
        (void) fsync(fd);
134
 
#endif
135
 
        (void) close(fd);
136
 
    }
137
 
 out:
138
 
    /* arrange so the file is owned by the ruid
139
 
       (swap real & effective uid if necessary).
140
 
       This isn't a security problem, since the ticket file, if it already
141
 
       exists, has the right uid (== ruid) and mode. */
142
 
    if (me != metoo) {
143
 
        if (do_seteuid(me) < 0) {
144
 
            /* can't switch??? barf! */
145
 
            if (krb_debug)
146
 
                perror("in_tkt: seteuid");
147
 
            return(KFAILURE);
148
 
        } else
149
 
            if (krb_debug)
150
 
                printf("swapped UID's %d and %d\n",(int) metoo, (int) me);
151
 
    }
152
 
    /* Set umask to ensure that we have write access on the created
153
 
       ticket file.  */
154
 
    mask = umask(077);
155
 
    tktfile = open(file, O_RDWR|O_SYNC|O_CREAT|O_EXCL, 0600);
156
 
    umask(mask);
157
 
    if (me != metoo) {
158
 
        if (do_seteuid(metoo) < 0) {
159
 
            /* can't switch??? barf! */
160
 
            if (krb_debug)
161
 
                perror("in_tkt: seteuid2");
162
 
            return(KFAILURE);
163
 
        } else
164
 
            if (krb_debug)
165
 
                printf("swapped UID's %d and %d\n", (int) me, (int) metoo);
166
 
    }
167
 
    if (tktfile < 0) {
168
 
        if (krb_debug)
169
 
            fprintf(stderr,"Error initializing %s",TKT_FILE);
170
 
        return(KFAILURE);
171
 
    }
172
 
    count = strlen(pname)+1;
173
 
    if (write(tktfile,pname,count) != count) {
174
 
        (void) close(tktfile);
175
 
        return(KFAILURE);
176
 
    }
177
 
    count = strlen(pinst)+1;
178
 
    if (write(tktfile,pinst,count) != count) {
179
 
        (void) close(tktfile);
180
 
        return(KFAILURE);
181
 
    }
182
 
    (void) close(tktfile);
183
 
#ifdef TKT_SHMEM
184
 
    (void) strncpy(shmidname, file, sizeof(shmidname) - 1);
185
 
    shmidname[sizeof(shmidname) - 1] = '\0';
186
 
    (void) strncat(shmidname, ".shm", sizeof(shmidname) - 1 - strlen(shmidname));
187
 
    return(krb_shm_create(shmidname));
188
 
#else /* !TKT_SHMEM */
189
 
    return(KSUCCESS);
190
 
#endif /* TKT_SHMEM */
191
 
}
192
 
 
193
 
int KRB5_CALLCONV
194
 
krb_in_tkt(pname, pinst, prealm)
195
 
    char *pname;
196
 
    char *pinst;
197
 
    char *prealm;
198
 
{
199
 
    return in_tkt(pname, pinst);
200
 
}