xref: /illumos-gate/usr/src/cmd/krb5/kadmin/dbutil/ovload.c (revision a5f69788de7ac07553de47f7fec8c05a9a94c105)
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    <memory.h>
25 
26 #include    <kadm5/adb.h>
27 #include    "import_err.h"
28 
29 #define	LINESIZE	32768	/* XXX */
30 #define	PLURAL(count)	(((count) == 1) ? \
31 	error_message(IMPORT_SINGLE_RECORD) : \
32 	error_message(IMPORT_PLURAL_RECORDS))
33 
34 int
35 parse_pw_hist_ent(current, hist)
36 char *current;
37 osa_pw_hist_ent *hist;
38 {
39 	int tmp, i, j, ret;
40 	char *cp;
41 
42 	ret = 0;
43 	hist->n_key_data = 1;
44 
45 	hist->key_data = (krb5_key_data *) malloc(hist->n_key_data *
46 	    sizeof (krb5_key_data));
47 	if (hist->key_data == NULL)
48 		return (ENOMEM);
49 	memset(hist->key_data, 0, sizeof (krb5_key_data) * hist->n_key_data);
50 
51 	for (i = 0; i < hist->n_key_data; i++) {
52 		krb5_key_data *key_data = &hist->key_data[i];
53 
54 		key_data->key_data_ver = 1;
55 
56 		if ((cp = strtok((char *) NULL, "\t")) == NULL) {
57 			com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
58 			ret = IMPORT_FAILED;
59 			goto done;
60 		}
61 		key_data->key_data_type[0] = atoi(cp);
62 
63 		if ((cp = strtok((char *) NULL, "\t")) == NULL) {
64 			com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
65 			ret = IMPORT_FAILED;
66 			goto done;
67 		}
68 		key_data->key_data_length[0] = atoi(cp);
69 
70 		if ((cp = strtok((char *) NULL, "\t")) == NULL) {
71 			com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
72 			ret = IMPORT_FAILED;
73 			goto done;
74 		}
75 		if (!(key_data->key_data_contents[0] = (krb5_octet *)
76 				malloc(key_data->key_data_length[0] + 1))) {
77 			ret = ENOMEM;
78 			goto done;
79 		}
80 		for (j = 0; j < key_data->key_data_length[0]; j++) {
81 			if (sscanf(cp, "%02x", &tmp) != 1) {
82 				com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
83 				ret = IMPORT_FAILED;
84 				goto done;
85 			}
86 			key_data->key_data_contents[0][j] = tmp;
87 			cp = strchr(cp, ' ') + 1;
88 		}
89 	}
90 
91 done:
92 	return (ret);
93 }
94 
95 /*
96  * Function: parse_principal
97  *
98  * Purpose: parse principal line in db dump file
99  *
100  * Arguments:
101  * 	<return value>	0 on sucsess, error code on failure
102  *
103  * Requires:
104  *	principal database to be opened.
105  *	strtok(3) to have a valid buffer in memory.
106  *
107  * Effects:
108  *	[effects]
109  *
110  * Modifies:
111  *	[modifies]
112  *
113  */
114 int
115 process_ov_principal(fname, kcontext, filep, verbose, linenop, pol_db)
116 char *fname;
117 krb5_context kcontext;
118 FILE *filep;
119 int verbose;
120 int *linenop;
121 void *pol_db;
122 {
123 	XDR xdrs;
124 	osa_princ_ent_t rec;
125 	osa_adb_ret_t ret;
126 	krb5_tl_data tl_data;
127 	krb5_principal princ;
128 	krb5_db_entry kdb;
129 	char *current;
130 	char *cp;
131 	int tmp, x, i, one;
132 	unsigned int more;
133 	char line[LINESIZE];
134 
135 	if (fgets(line, LINESIZE, filep) == (char *) NULL) {
136 		return (IMPORT_BAD_FILE);
137 	}
138 	if ((cp = strtok(line, "\t")) == NULL)
139 		return (IMPORT_BAD_FILE);
140 	if ((rec = (osa_princ_ent_t)
141 		    malloc(sizeof (osa_princ_ent_rec))) == NULL)
142 		return (ENOMEM);
143 	memset(rec, 0, sizeof (osa_princ_ent_rec));
144 	if ((ret = krb5_parse_name(kcontext, cp, &princ)))
145 		goto done;
146 	krb5_unparse_name(kcontext, princ, &current);
147 	if ((cp = strtok((char *) NULL, "\t")) == NULL) {
148 		com_err(NULL, IMPORT_BAD_RECORD, "%s", current);
149 		ret = IMPORT_FAILED;
150 		goto done;
151 	} else {
152 		if (strcmp(cp, "")) {
153 			if ((rec->policy = (char *)
154 					malloc(strlen(cp) + 1)) == NULL) {
155 				ret = ENOMEM;
156 				goto done;
157 			}
158 			strcpy(rec->policy, cp);
159 		} else
160 			rec->policy = NULL;
161 	}
162 	if ((cp = strtok((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 = strtok((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 = strtok((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 = strtok((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 = OSA_ADB_XDR_FAILURE;
204 		goto done;
205 	}
206 	tl_data.tl_data_type = KRB5_TL_KADM_DATA;
207 	tl_data.tl_data_length = xdr_getpos(&xdrs);
208 	tl_data.tl_data_contents = (krb5_octet *) xdralloc_getdata(&xdrs);
209 
210 	one = 1;
211 	ret = krb5_db_get_principal(kcontext, princ, &kdb, &one,
212 	    &more);
213 	if (ret)
214 		goto done;
215 
216 	if (ret = krb5_dbe_update_tl_data(kcontext, &kdb,
217 		&tl_data))
218 		goto done;
219 
220 	if (ret = krb5_db_put_principal(kcontext, &kdb, &one))
221 		goto done;
222 
223 	xdr_destroy(&xdrs);
224 
225 	(*linenop)++;
226 
227 done:
228 	free(current);
229 	krb5_free_principal(kcontext, princ);
230 	osa_free_princ_ent(rec);
231 	return (ret);
232 }
233