xref: /titanic_50/usr/src/cmd/krb5/kadmin/dbutil/ovload.c (revision fc80c0dfb0c877aee828d778ea32b77fcf7b1ef4)
1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 
3 /*
4  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
5  *
6  *	Openvision retains the copyright to derivative works of
7  *	this source code.  Do *NOT* create a derivative of this
8  *	source code before consulting with your legal department.
9  *	Do *NOT* integrate *ANY* of this source code into another
10  *	product before consulting with your legal department.
11  *
12  *	For further information, read the top-level Openvision
13  *	copyright which is contained in the top-level MIT Kerberos
14  *	copyright.
15  *
16  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
17  *
18  */
19 
20 
21 #include    <unistd.h>
22 #include    <string.h>
23 #include    <stdlib.h>
24 #include    "autoconf.h"
25 #ifdef HAVE_MEMORY_H
26 #include    <memory.h>
27 #endif
28 
29 #include    <k5-int.h>
30 #include <kadm5/admin.h>
31 #include <kadm5/server_internal.h>
32 #include    <kdb.h>
33 #include    "import_err.h"
34 #include    "kdb5_util.h"
35 #include    "nstrtok.h"
36 
37 #define LINESIZE	32768 /* XXX */
38 #define PLURAL(count)	(((count) == 1) ? error_message(IMPORT_SINGLE_RECORD) : error_message(IMPORT_PLURAL_RECORDS))
39 
40 static int parse_pw_hist_ent(current, hist)
41    char *current;
42    osa_pw_hist_ent *hist;
43 {
44      int tmp, i, j, ret;
45      char *cp;
46 
47      ret = 0;
48      hist->n_key_data = 1;
49 
50      hist->key_data = (krb5_key_data *) malloc(hist->n_key_data *
51 					       sizeof(krb5_key_data));
52      if (hist->key_data == NULL)
53 	  return ENOMEM;
54      memset(hist->key_data, 0, sizeof(krb5_key_data)*hist->n_key_data);
55 
56      for (i = 0; i < hist->n_key_data; i++) {
57 	  krb5_key_data *key_data = &hist->key_data[i];
58 
59 	  key_data->key_data_ver = 1;
60 
61 	  if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
62 	       com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
63 	       ret = IMPORT_FAILED;
64 	       goto done;
65 	  }
66 	  key_data->key_data_type[0] = atoi(cp);
67 
68 	  if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
69 	       com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
70 	       ret =  IMPORT_FAILED;
71 	       goto done;
72 	  }
73 	  key_data->key_data_length[0] = atoi(cp);
74 
75 	  if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
76 	       com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
77 	       ret = IMPORT_FAILED;
78 	       goto done;
79 	  }
80 	  if(!(key_data->key_data_contents[0] =
81 	       (krb5_octet *) malloc(key_data->key_data_length[0]+1))) {
82 	       ret = ENOMEM;
83 	       goto done;
84 	  }
85 	  for(j = 0; j < key_data->key_data_length[0]; j++) {
86 	       if(sscanf(cp, "%02x", &tmp) != 1) {
87 		    com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
88 		    ret = IMPORT_FAILED;
89 		    goto done;
90 	       }
91 	       key_data->key_data_contents[0][j] = tmp;
92 	       cp = strchr(cp, ' ') + 1;
93 	  }
94      }
95 
96 done:
97      return ret;
98 }
99 
100 /*
101  * Function: parse_principal
102  *
103  * Purpose: parse principal line in db dump file
104  *
105  * Arguments:
106  * 	<return value>	0 on success, error code on failure
107  *
108  * Requires:
109  *	principal database to be opened.
110  *	nstrtok(3) to have a valid buffer in memory.
111  *
112  * Effects:
113  *	[effects]
114  *
115  * Modifies:
116  *	[modifies]
117  *
118  */
119 int process_ov_principal(fname, kcontext, filep, verbose, linenop)
120     char		*fname;
121     krb5_context	kcontext;
122     FILE		*filep;
123     int			verbose;
124     int			*linenop;
125 {
126     XDR			    xdrs;
127     osa_princ_ent_t	    rec;
128     krb5_error_code	    ret;
129     krb5_tl_data	    tl_data;
130     krb5_principal	    princ;
131     krb5_db_entry	    kdb;
132     char		    *current;
133     char		    *cp;
134     int			    x, one;
135     krb5_boolean	    more;
136     char		    line[LINESIZE];
137 
138     if (fgets(line, LINESIZE, filep) == (char *) NULL) {
139 	 return IMPORT_BAD_FILE;
140     }
141     if((cp = nstrtok(line, "\t")) == NULL)
142 	return IMPORT_BAD_FILE;
143     if((rec = (osa_princ_ent_t) malloc(sizeof(osa_princ_ent_rec))) == NULL)
144 	return ENOMEM;
145     memset(rec, 0, sizeof(osa_princ_ent_rec));
146     if((ret = krb5_parse_name(kcontext, cp, &princ)))
147 	goto done;
148     krb5_unparse_name(kcontext, princ, &current);
149     if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
150 	com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
151 	ret =  IMPORT_FAILED;
152 	goto done;
153     } else {
154 	if(strcmp(cp, "")) {
155 	    if((rec->policy = (char *) malloc(strlen(cp)+1)) == NULL)  {
156 		ret = ENOMEM;
157 		goto done;
158 	    }
159 	    strcpy(rec->policy, cp);
160 	} else rec->policy = NULL;
161     }
162     if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
163 	com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
164 	ret = IMPORT_FAILED;
165 	goto done;
166     }
167     rec->aux_attributes = strtol(cp, (char  **)NULL, 16);
168     if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
169 	com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
170 	ret = IMPORT_FAILED;
171 	goto done;
172     }
173     rec->old_key_len = atoi(cp);
174     if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
175 	com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
176 	ret = IMPORT_FAILED;
177 	goto done;
178     }
179     rec->old_key_next = atoi(cp);
180     if((cp = nstrtok((char *) NULL, "\t")) == NULL) {
181 	com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
182 	ret = IMPORT_FAILED;
183 	goto done;
184     }
185     rec->admin_history_kvno = atoi(cp);
186     if (! rec->old_key_len) {
187        rec->old_keys = NULL;
188     } else {
189        if(!(rec->old_keys = (osa_pw_hist_ent *)
190 	    malloc(sizeof(osa_pw_hist_ent) * rec->old_key_len))) {
191 	  ret = ENOMEM;
192 	  goto done;
193        }
194        memset(rec->old_keys,0,
195 	      sizeof(osa_pw_hist_ent) * rec->old_key_len);
196        for(x = 0; x < rec->old_key_len; x++)
197 	    parse_pw_hist_ent(current, &rec->old_keys[x]);
198     }
199 
200     xdralloc_create(&xdrs, XDR_ENCODE);
201     if (! xdr_osa_princ_ent_rec(&xdrs, rec)) {
202 	 xdr_destroy(&xdrs);
203 	 ret = KADM5_XDR_FAILURE;
204 	 goto done;
205     }
206 
207     tl_data.tl_data_type = KRB5_TL_KADM_DATA;
208     tl_data.tl_data_length = xdr_getpos(&xdrs);
209     tl_data.tl_data_contents = (krb5_octet *) xdralloc_getdata(&xdrs);
210 
211     one = 1;
212     ret = krb5_db_get_principal(kcontext, princ, &kdb, &one, &more);
213     if (ret)
214 	 goto done;
215 
216     ret = krb5_dbe_update_tl_data(kcontext, &kdb, &tl_data);
217     if (ret)
218 	 goto done;
219 
220     ret = krb5_db_put_principal(kcontext, &kdb, &one);
221     if (ret)
222 	 goto done;
223 
224     xdr_destroy(&xdrs);
225 
226     (*linenop)++;
227 
228 done:
229     free(current);
230     krb5_free_principal(kcontext, princ);
231     osa_free_princ_ent(rec);
232     return ret;
233 }
234