summaryrefslogtreecommitdiffstats
path: root/src/kadmin/dbutil/ovload.c
blob: fd512073635a55d60e2b8950508fd6c5aca73880 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include    <unistd.h>
#include    <string.h>
#include    <stdlib.h>
#include    "autoconf.h"
#ifdef HAVE_MEMORY_H
#include    <memory.h>
#endif

#include    <k5-int.h>
#include <kadm5/admin.h>
#include <kadm5/server_internal.h>
#include    <kdb.h>
#include    "import_err.h"
#include    "kdb5_util.h"
#include    "nstrtok.h"

#define LINESIZE	32768 /* XXX */

static int parse_pw_hist_ent(current, hist)
   char *current;
   osa_pw_hist_ent *hist;
{
     int tmp, i, j, ret;
     char *cp;

     ret = 0;
     hist->n_key_data = 1;

     hist->key_data = (krb5_key_data *) malloc(hist->n_key_data *
					       sizeof(krb5_key_data));
     if (hist->key_data == NULL)
	  return ENOMEM;
     memset(hist->key_data, 0, sizeof(krb5_key_data)*hist->n_key_data);

     for (i = 0; i < hist->n_key_data; i++) {
	  krb5_key_data *key_data = &hist->key_data[i];

	  key_data->key_data_ver = 1;
	  
	  if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
	       com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
	       ret = IMPORT_FAILED;
	       goto done;
	  }
	  key_data->key_data_type[0] = atoi(cp);

	  if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
	       com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
	       ret =  IMPORT_FAILED;
	       goto done;
	  }
	  key_data->key_data_length[0] = atoi(cp);
	  
	  if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
	       com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
	       ret = IMPORT_FAILED;
	       goto done;
	  }
	  if(!(key_data->key_data_contents[0] =
	       (krb5_octet *) malloc(key_data->key_data_length[0]+1))) {
	       ret = ENOMEM;
	       goto done;
	  }
	  for(j = 0; j < key_data->key_data_length[0]; j++) {
	       if(sscanf(cp, "%02x", &tmp) != 1) {
		    com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
		    ret = IMPORT_FAILED;
		    goto done;
	       }
	       key_data->key_data_contents[0][j] = tmp;
	       cp = strchr(cp, ' ') + 1;
	  }
     }
     
done:
     return ret;
}

/*
 * Function: parse_principal
 * 
 * Purpose: parse principal line in db dump file
 *
 * Arguments:
 * 	<return value>	0 on success, error code on failure
 *
 * Requires:
 *	principal database to be opened.
 *	nstrtok(3) to have a valid buffer in memory.
 * 
 * Effects:
 *	[effects]
 *
 * Modifies:
 *	[modifies]
 * 
 */
int process_ov_principal(fname, kcontext, filep, verbose, linenop)
    char		*fname;
    krb5_context	kcontext;
    FILE		*filep;
    int			verbose;
    int			*linenop;
{
    XDR			    xdrs;
    osa_princ_ent_t	    rec;
    krb5_error_code	    ret;
    krb5_tl_data	    tl_data;
    krb5_principal	    princ;
    krb5_db_entry	    kdb;
    char		    *current = 0;
    char		    *cp;
    int			    x, one;
    krb5_boolean	    more;
    char		    line[LINESIZE];

    if (fgets(line, LINESIZE, filep) == (char *) NULL) {
	 return IMPORT_BAD_FILE;
    }
    if((cp = nstrtok(line, "\t")) == NULL)
	return IMPORT_BAD_FILE;
    if((rec = (osa_princ_ent_t) malloc(sizeof(osa_princ_ent_rec))) == NULL)
	return ENOMEM;
    memset(rec, 0, sizeof(osa_princ_ent_rec));
    if((ret = krb5_parse_name(kcontext, cp, &princ))) 
	goto done;
    krb5_unparse_name(kcontext, princ, &current);
    if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
	com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
	ret =  IMPORT_FAILED;
	goto done;
    } else {
	if(strcmp(cp, "")) {
	    if((rec->policy = (char *) malloc(strlen(cp)+1)) == NULL)  {
		ret = ENOMEM;
		goto done;
	    }
	    strcpy(rec->policy, cp);
	} else rec->policy = NULL;
    }
    if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
	com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
	ret = IMPORT_FAILED;
	goto done;
    }
    rec->aux_attributes = strtol(cp, (char  **)NULL, 16);
    if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
	com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
	ret = IMPORT_FAILED;
	goto done;
    }
    rec->old_key_len = atoi(cp);
    if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
	com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
	ret = IMPORT_FAILED;
	goto done;
    }
    rec->old_key_next = atoi(cp);
    if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
	com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
	ret = IMPORT_FAILED;
	goto done;
    }
    rec->admin_history_kvno = atoi(cp);
    if (! rec->old_key_len) {
       rec->old_keys = NULL;
    } else {
       if(!(rec->old_keys = (osa_pw_hist_ent *)
	    malloc(sizeof(osa_pw_hist_ent) * rec->old_key_len))) {
	  ret = ENOMEM;
	  goto done;
       }
       memset(rec->old_keys,0,
	      sizeof(osa_pw_hist_ent) * rec->old_key_len);
       for(x = 0; x < rec->old_key_len; x++)
	    parse_pw_hist_ent(current, &rec->old_keys[x]);
    }

    xdralloc_create(&xdrs, XDR_ENCODE);
    if (! xdr_osa_princ_ent_rec(&xdrs, rec)) {
	 xdr_destroy(&xdrs);
	 ret = KADM5_XDR_FAILURE;
	 goto done;
    }

    tl_data.tl_data_type = KRB5_TL_KADM_DATA;
    tl_data.tl_data_length = xdr_getpos(&xdrs);
    tl_data.tl_data_contents = (krb5_octet *) xdralloc_getdata(&xdrs);

    one = 1;
    ret = krb5_db_get_principal(kcontext, princ, &kdb, &one, &more);
    if (ret)
	 goto done;
    
    ret = krb5_dbe_update_tl_data(kcontext, &kdb, &tl_data);
    if (ret)
	 goto done;

    ret = krb5_db_put_principal(kcontext, &kdb, &one);
    if (ret)
	 goto done;

    xdr_destroy(&xdrs);

    (*linenop)++;

done:
    free(current);
    krb5_free_principal(kcontext, princ);
    osa_free_princ_ent(rec);
    return ret;
}