Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / lib / krb / dest_tkt.c
1 /* 
2   Copyright (C) 1989 by the Massachusetts Institute of Technology
3
4    Export of this software from the United States of America is assumed
5    to require a specific license from the United States Government.
6    It is the responsibility of any person or organization contemplating
7    export to obtain such a license before exporting.
8
9 WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
10 distribute this software and its documentation for any purpose and
11 without fee is hereby granted, provided that the above copyright
12 notice appear in all copies and that both that copyright notice and
13 this permission notice appear in supporting documentation, and that
14 the name of M.I.T. not be used in advertising or publicity pertaining
15 to distribution of the software without specific, written prior
16 permission.  M.I.T. makes no representations about the suitability of
17 this software for any purpose.  It is provided "as is" without express
18 or implied warranty.
19
20   */
21
22 #include "krb_locl.h"
23
24 RCSID("$Id: dest_tkt.c,v 1.11.14.2 2000/10/18 20:26:42 assar Exp $");
25
26 #ifndef O_BINARY
27 #define O_BINARY 0
28 #endif
29
30 /*
31  * dest_tkt() is used to destroy the ticket store upon logout.
32  * If the ticket file does not exist, dest_tkt() returns RET_TKFIL.
33  * Otherwise the function returns RET_OK on success, KFAILURE on
34  * failure.
35  *
36  * The ticket file (TKT_FILE) is defined in "krb.h".
37  */
38
39 int
40 dest_tkt(void)
41 {
42     const char *filename = TKT_FILE;
43     int i, fd;
44     struct stat sb1, sb2;
45     char buf[BUFSIZ];
46     int error = 0;
47
48     if (lstat (filename, &sb1) < 0) {
49         error = errno;
50         goto out;
51     }
52
53     fd = open (filename, O_RDWR | O_BINARY);
54     if (fd < 0) {
55         error = errno;
56         goto out;
57     }
58
59     if (unlink (filename) < 0) {
60         error = errno;
61         close(fd);
62         goto out;
63     }
64
65     if (fstat (fd, &sb2) < 0) {
66         error = errno;
67         close(fd);
68         goto out;
69     }
70
71     if (sb1.st_dev != sb2.st_dev || sb1.st_ino != sb2.st_ino) {
72         close (fd);
73         error = EPERM;
74         goto out;
75     }
76
77     if (sb2.st_nlink != 0) {
78         close (fd);
79         error = EPERM;
80         goto out;
81     }
82
83     for (i = 0; i < sb2.st_size; i += sizeof(buf)) {
84         int ret;
85         
86         ret = write(fd, buf, sizeof(buf));
87         if (ret != sizeof(buf)) {
88             if (ret < 0)
89                 error = errno;
90             else
91                 error = EINVAL;
92             fsync(fd);
93             close(fd);
94             goto out;
95         }
96     }
97
98     fsync(fd);
99     close(fd);
100     
101 out:
102     if (error == ENOENT)
103         return RET_TKFIL;
104     else if (error != 0)
105         return KFAILURE;
106     else
107         return(KSUCCESS);
108 }