Merge from vendor branch BSDTAR:
[dragonfly.git] / crypto / heimdal / kdc / v4_dump.c
1 /*
2  * Copyright (c) 2000 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include "hprop.h"
35
36 RCSID("$Id: v4_dump.c,v 1.4 2001/01/26 15:55:07 joda Exp $");
37
38 static time_t
39 time_parse(const char *cp)
40 {
41     char wbuf[5];
42     struct tm tp;
43     int local;
44
45     memset(&tp, 0, sizeof(tp)); /* clear out the struct */
46     
47     /* new format is YYYYMMDDHHMM UTC,
48        old format is YYMMDDHHMM local time */
49     if (strlen(cp) > 10) {              /* new format */
50         strlcpy(wbuf, cp, sizeof(wbuf));
51         tp.tm_year = atoi(wbuf) - 1900;
52         cp += 4;
53         local = 0;
54     } else {
55         wbuf[0] = *cp++;
56         wbuf[1] = *cp++;
57         wbuf[2] = '\0';
58         tp.tm_year = atoi(wbuf);
59         if(tp.tm_year < 38)
60             tp.tm_year += 100;
61         local = 1;
62     }
63
64     wbuf[0] = *cp++;
65     wbuf[1] = *cp++;
66     wbuf[2] = 0;
67     tp.tm_mon = atoi(wbuf) - 1;
68
69     wbuf[0] = *cp++;
70     wbuf[1] = *cp++;
71     tp.tm_mday = atoi(wbuf);
72     
73     wbuf[0] = *cp++;
74     wbuf[1] = *cp++;
75     tp.tm_hour = atoi(wbuf);
76     
77     wbuf[0] = *cp++;
78     wbuf[1] = *cp++;
79     tp.tm_min = atoi(wbuf);
80     
81     return(tm2time(tp, local));
82 }
83
84 /* convert a version 4 dump file */
85 int
86 v4_prop_dump(void *arg, const char *file)
87 {
88     char buf [1024];
89     FILE *f;
90     int lineno = 0;
91
92     f = fopen(file, "r");
93     if(f == NULL)
94         return errno;
95     
96     while(fgets(buf, sizeof(buf), f)) {
97         int ret;
98         unsigned long key[2]; /* yes, long */
99         char exp_date[64], mod_date[64];
100         struct v4_principal pr;
101         int attributes;
102     
103         memset(&pr, 0, sizeof(pr));
104         errno = 0;
105         lineno++;
106         ret = sscanf(buf, "%s %s %d %d %d %d %lx %lx %s %s %s %s",
107                      pr.name, pr.instance,
108                      &pr.max_life, &pr.mkvno, &pr.kvno,
109                      &attributes,
110                      &key[0], &key[1],
111                      exp_date, mod_date,
112                      pr.mod_name, pr.mod_instance);
113         if(ret != 12){
114             warnx("Line %d malformed (ignored)", lineno);
115             continue;
116         }
117         if(attributes != 0) {
118             warnx("Line %d (%s.%s) has non-zero attributes - skipping", 
119                   lineno, pr.name, pr.instance);
120             continue;
121         }
122         pr.key[0] = (key[0] >> 24) & 0xff;
123         pr.key[1] = (key[0] >> 16) & 0xff;
124         pr.key[2] = (key[0] >> 8) & 0xff;
125         pr.key[3] = (key[0] >> 0) & 0xff;
126         pr.key[4] = (key[1] >> 24) & 0xff;
127         pr.key[5] = (key[1] >> 16) & 0xff;
128         pr.key[6] = (key[1] >> 8) & 0xff;
129         pr.key[7] = (key[1] >> 0) & 0xff;
130         pr.exp_date = time_parse(exp_date);
131         pr.mod_date = time_parse(mod_date);
132         if (pr.instance[0] == '*')
133             pr.instance[0] = '\0';
134         if (pr.mod_name[0] == '*')
135             pr.mod_name[0] = '\0';
136         if (pr.mod_instance[0] == '*')
137             pr.mod_instance[0] = '\0';
138         v4_prop(arg, &pr);
139         memset(&pr, 0, sizeof(pr));
140     }
141     return 0;
142 }