xref: /freebsd/crypto/krb5/src/kadmin/dbutil/ovload.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert #include    <k5-int.h>
3*7f2fe78bSCy Schubert #include    <unistd.h>
4*7f2fe78bSCy Schubert 
5*7f2fe78bSCy Schubert #include <kadm5/admin.h>
6*7f2fe78bSCy Schubert #include <kadm5/server_internal.h>
7*7f2fe78bSCy Schubert #include    <kdb.h>
8*7f2fe78bSCy Schubert #include    "import_err.h"
9*7f2fe78bSCy Schubert #include    "kdb5_util.h"
10*7f2fe78bSCy Schubert #include    "nstrtok.h"
11*7f2fe78bSCy Schubert 
12*7f2fe78bSCy Schubert #define LINESIZE        32768 /* XXX */
13*7f2fe78bSCy Schubert 
parse_pw_hist_ent(current,hist)14*7f2fe78bSCy Schubert static int parse_pw_hist_ent(current, hist)
15*7f2fe78bSCy Schubert     char *current;
16*7f2fe78bSCy Schubert     osa_pw_hist_ent *hist;
17*7f2fe78bSCy Schubert {
18*7f2fe78bSCy Schubert     int tmp, i, j, ret;
19*7f2fe78bSCy Schubert     char *cp;
20*7f2fe78bSCy Schubert 
21*7f2fe78bSCy Schubert     ret = 0;
22*7f2fe78bSCy Schubert     hist->n_key_data = 1;
23*7f2fe78bSCy Schubert 
24*7f2fe78bSCy Schubert     hist->key_data = (krb5_key_data *) malloc(hist->n_key_data *
25*7f2fe78bSCy Schubert                                               sizeof(krb5_key_data));
26*7f2fe78bSCy Schubert     if (hist->key_data == NULL)
27*7f2fe78bSCy Schubert         return ENOMEM;
28*7f2fe78bSCy Schubert     memset(hist->key_data, 0, sizeof(krb5_key_data)*hist->n_key_data);
29*7f2fe78bSCy Schubert 
30*7f2fe78bSCy Schubert     for (i = 0; i < hist->n_key_data; i++) {
31*7f2fe78bSCy Schubert         krb5_key_data *key_data = &hist->key_data[i];
32*7f2fe78bSCy Schubert 
33*7f2fe78bSCy Schubert         key_data->key_data_ver = 1;
34*7f2fe78bSCy Schubert 
35*7f2fe78bSCy Schubert         if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
36*7f2fe78bSCy Schubert             com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
37*7f2fe78bSCy Schubert             ret = IMPORT_FAILED;
38*7f2fe78bSCy Schubert             goto done;
39*7f2fe78bSCy Schubert         }
40*7f2fe78bSCy Schubert         key_data->key_data_type[0] = atoi(cp);
41*7f2fe78bSCy Schubert 
42*7f2fe78bSCy Schubert         if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
43*7f2fe78bSCy Schubert             com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
44*7f2fe78bSCy Schubert             ret =  IMPORT_FAILED;
45*7f2fe78bSCy Schubert             goto done;
46*7f2fe78bSCy Schubert         }
47*7f2fe78bSCy Schubert         key_data->key_data_length[0] = atoi(cp);
48*7f2fe78bSCy Schubert 
49*7f2fe78bSCy Schubert         if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
50*7f2fe78bSCy Schubert             com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
51*7f2fe78bSCy Schubert             ret = IMPORT_FAILED;
52*7f2fe78bSCy Schubert             goto done;
53*7f2fe78bSCy Schubert         }
54*7f2fe78bSCy Schubert         if(!(key_data->key_data_contents[0] =
55*7f2fe78bSCy Schubert              (krb5_octet *) malloc(key_data->key_data_length[0]+1))) {
56*7f2fe78bSCy Schubert             ret = ENOMEM;
57*7f2fe78bSCy Schubert             goto done;
58*7f2fe78bSCy Schubert         }
59*7f2fe78bSCy Schubert         for(j = 0; j < key_data->key_data_length[0]; j++) {
60*7f2fe78bSCy Schubert             if(sscanf(cp, "%02x", &tmp) != 1) {
61*7f2fe78bSCy Schubert                 com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
62*7f2fe78bSCy Schubert                 ret = IMPORT_FAILED;
63*7f2fe78bSCy Schubert                 goto done;
64*7f2fe78bSCy Schubert             }
65*7f2fe78bSCy Schubert             key_data->key_data_contents[0][j] = tmp;
66*7f2fe78bSCy Schubert             cp = strchr(cp, ' ') + 1;
67*7f2fe78bSCy Schubert         }
68*7f2fe78bSCy Schubert     }
69*7f2fe78bSCy Schubert 
70*7f2fe78bSCy Schubert done:
71*7f2fe78bSCy Schubert     return ret;
72*7f2fe78bSCy Schubert }
73*7f2fe78bSCy Schubert 
74*7f2fe78bSCy Schubert /*
75*7f2fe78bSCy Schubert  * Function: parse_principal
76*7f2fe78bSCy Schubert  *
77*7f2fe78bSCy Schubert  * Purpose: parse principal line in db dump file
78*7f2fe78bSCy Schubert  *
79*7f2fe78bSCy Schubert  * Arguments:
80*7f2fe78bSCy Schubert  *      <return value>  0 on success, error code on failure
81*7f2fe78bSCy Schubert  *
82*7f2fe78bSCy Schubert  * Requires:
83*7f2fe78bSCy Schubert  *      principal database to be opened.
84*7f2fe78bSCy Schubert  *      nstrtok(3) to have a valid buffer in memory.
85*7f2fe78bSCy Schubert  *
86*7f2fe78bSCy Schubert  * Effects:
87*7f2fe78bSCy Schubert  *      [effects]
88*7f2fe78bSCy Schubert  *
89*7f2fe78bSCy Schubert  * Modifies:
90*7f2fe78bSCy Schubert  *      [modifies]
91*7f2fe78bSCy Schubert  *
92*7f2fe78bSCy Schubert  */
process_ov_principal(kcontext,fname,filep,verbose,linenop)93*7f2fe78bSCy Schubert int process_ov_principal(kcontext, fname, filep, verbose, linenop)
94*7f2fe78bSCy Schubert     krb5_context        kcontext;
95*7f2fe78bSCy Schubert     const char          *fname;
96*7f2fe78bSCy Schubert     FILE                *filep;
97*7f2fe78bSCy Schubert     krb5_boolean        verbose;
98*7f2fe78bSCy Schubert     int                 *linenop;
99*7f2fe78bSCy Schubert {
100*7f2fe78bSCy Schubert     XDR                     xdrs;
101*7f2fe78bSCy Schubert     osa_princ_ent_t         rec;
102*7f2fe78bSCy Schubert     krb5_error_code         ret;
103*7f2fe78bSCy Schubert     krb5_tl_data            tl_data;
104*7f2fe78bSCy Schubert     krb5_principal          princ;
105*7f2fe78bSCy Schubert     krb5_db_entry           *kdb = NULL;
106*7f2fe78bSCy Schubert     char                    *current = 0;
107*7f2fe78bSCy Schubert     char                    *cp;
108*7f2fe78bSCy Schubert     unsigned int            x;
109*7f2fe78bSCy Schubert     char                    line[LINESIZE];
110*7f2fe78bSCy Schubert 
111*7f2fe78bSCy Schubert     if (fgets(line, LINESIZE, filep) == (char *) NULL) {
112*7f2fe78bSCy Schubert         return IMPORT_BAD_FILE;
113*7f2fe78bSCy Schubert     }
114*7f2fe78bSCy Schubert     if((cp = nstrtok(line, "\t")) == NULL)
115*7f2fe78bSCy Schubert         return IMPORT_BAD_FILE;
116*7f2fe78bSCy Schubert     if((rec = (osa_princ_ent_t) malloc(sizeof(osa_princ_ent_rec))) == NULL)
117*7f2fe78bSCy Schubert         return ENOMEM;
118*7f2fe78bSCy Schubert     memset(rec, 0, sizeof(osa_princ_ent_rec));
119*7f2fe78bSCy Schubert     if((ret = krb5_parse_name(kcontext, cp, &princ)))
120*7f2fe78bSCy Schubert         goto done;
121*7f2fe78bSCy Schubert     krb5_unparse_name(kcontext, princ, &current);
122*7f2fe78bSCy Schubert     if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
123*7f2fe78bSCy Schubert         com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
124*7f2fe78bSCy Schubert         ret =  IMPORT_FAILED;
125*7f2fe78bSCy Schubert         goto done;
126*7f2fe78bSCy Schubert     } else {
127*7f2fe78bSCy Schubert         if(strcmp(cp, "")) {
128*7f2fe78bSCy Schubert             if((rec->policy = strdup(cp)) == NULL)  {
129*7f2fe78bSCy Schubert                 ret = ENOMEM;
130*7f2fe78bSCy Schubert                 goto done;
131*7f2fe78bSCy Schubert             }
132*7f2fe78bSCy Schubert         } else rec->policy = NULL;
133*7f2fe78bSCy Schubert     }
134*7f2fe78bSCy Schubert     if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
135*7f2fe78bSCy Schubert         com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
136*7f2fe78bSCy Schubert         ret = IMPORT_FAILED;
137*7f2fe78bSCy Schubert         goto done;
138*7f2fe78bSCy Schubert     }
139*7f2fe78bSCy Schubert     rec->aux_attributes = strtol(cp, (char  **)NULL, 16);
140*7f2fe78bSCy Schubert     if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
141*7f2fe78bSCy Schubert         com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
142*7f2fe78bSCy Schubert         ret = IMPORT_FAILED;
143*7f2fe78bSCy Schubert         goto done;
144*7f2fe78bSCy Schubert     }
145*7f2fe78bSCy Schubert     rec->old_key_len = atoi(cp);
146*7f2fe78bSCy Schubert     if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
147*7f2fe78bSCy Schubert         com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
148*7f2fe78bSCy Schubert         ret = IMPORT_FAILED;
149*7f2fe78bSCy Schubert         goto done;
150*7f2fe78bSCy Schubert     }
151*7f2fe78bSCy Schubert     rec->old_key_next = atoi(cp);
152*7f2fe78bSCy Schubert     if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
153*7f2fe78bSCy Schubert         com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
154*7f2fe78bSCy Schubert         ret = IMPORT_FAILED;
155*7f2fe78bSCy Schubert         goto done;
156*7f2fe78bSCy Schubert     }
157*7f2fe78bSCy Schubert     rec->admin_history_kvno = atoi(cp);
158*7f2fe78bSCy Schubert     if (! rec->old_key_len) {
159*7f2fe78bSCy Schubert         rec->old_keys = NULL;
160*7f2fe78bSCy Schubert     } else {
161*7f2fe78bSCy Schubert         if(!(rec->old_keys = (osa_pw_hist_ent *)
162*7f2fe78bSCy Schubert              malloc(sizeof(osa_pw_hist_ent) * rec->old_key_len))) {
163*7f2fe78bSCy Schubert             ret = ENOMEM;
164*7f2fe78bSCy Schubert             goto done;
165*7f2fe78bSCy Schubert         }
166*7f2fe78bSCy Schubert         memset(rec->old_keys,0,
167*7f2fe78bSCy Schubert                sizeof(osa_pw_hist_ent) * rec->old_key_len);
168*7f2fe78bSCy Schubert         for(x = 0; x < rec->old_key_len; x++)
169*7f2fe78bSCy Schubert             parse_pw_hist_ent(current, &rec->old_keys[x]);
170*7f2fe78bSCy Schubert     }
171*7f2fe78bSCy Schubert 
172*7f2fe78bSCy Schubert     xdralloc_create(&xdrs, XDR_ENCODE);
173*7f2fe78bSCy Schubert     if (! xdr_osa_princ_ent_rec(&xdrs, rec)) {
174*7f2fe78bSCy Schubert         xdr_destroy(&xdrs);
175*7f2fe78bSCy Schubert         ret = KADM5_XDR_FAILURE;
176*7f2fe78bSCy Schubert         goto done;
177*7f2fe78bSCy Schubert     }
178*7f2fe78bSCy Schubert 
179*7f2fe78bSCy Schubert     tl_data.tl_data_type = KRB5_TL_KADM_DATA;
180*7f2fe78bSCy Schubert     tl_data.tl_data_length = xdr_getpos(&xdrs);
181*7f2fe78bSCy Schubert     tl_data.tl_data_contents = (krb5_octet *) xdralloc_getdata(&xdrs);
182*7f2fe78bSCy Schubert 
183*7f2fe78bSCy Schubert     ret = krb5_db_get_principal(kcontext, princ, 0, &kdb);
184*7f2fe78bSCy Schubert     if (ret)
185*7f2fe78bSCy Schubert         goto done;
186*7f2fe78bSCy Schubert 
187*7f2fe78bSCy Schubert     ret = krb5_dbe_update_tl_data(kcontext, kdb, &tl_data);
188*7f2fe78bSCy Schubert     if (ret)
189*7f2fe78bSCy Schubert         goto done;
190*7f2fe78bSCy Schubert 
191*7f2fe78bSCy Schubert     ret = krb5_db_put_principal(kcontext, kdb);
192*7f2fe78bSCy Schubert     if (ret)
193*7f2fe78bSCy Schubert         goto done;
194*7f2fe78bSCy Schubert 
195*7f2fe78bSCy Schubert     xdr_destroy(&xdrs);
196*7f2fe78bSCy Schubert 
197*7f2fe78bSCy Schubert     (*linenop)++;
198*7f2fe78bSCy Schubert 
199*7f2fe78bSCy Schubert done:
200*7f2fe78bSCy Schubert     free(current);
201*7f2fe78bSCy Schubert     krb5_free_principal(kcontext, princ);
202*7f2fe78bSCy Schubert     osa_free_princ_ent(rec);
203*7f2fe78bSCy Schubert     krb5_db_free_principal(kcontext, kdb);
204*7f2fe78bSCy Schubert     return ret;
205*7f2fe78bSCy Schubert }
206