summaryrefslogtreecommitdiffstats
path: root/src/kadmin/export/export.c
blob: 3d41c4d9d7334e3414bde576c6dadb3eb162a21b (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved
 *
 * $Header$
 */

#if !defined(lint) && !defined(__CODECENTER__)
static char *rcsid = "$Header$";
#endif

#include    <sys/time.h>
#include    <stdio.h>
#include    <malloc.h>
#include    <unistd.h>

#include    <kadm5/adb.h>
#include    "export_err.h"
#include    "local.h"

extern	int errno;

void print_key_data(FILE *f, krb5_key_data *key_data)
{
     int c;
     
     fprintf(f, "%d\t%d\t", key_data->key_data_type[0],
	     key_data->key_data_length[0]);
     for(c = 0; c < key_data->key_data_length[0]; c++) 
	  fprintf(f, "%02x ",
		  key_data->key_data_contents[0][c]);
}

/*
 * Function: print_princ
 * 
 * Purpose: output osa_adb_princ_ent data in a human
 *	    readable format (which is a format suitable for
 *	    ovsec_adm_import consumption)
 *
 * Arguments:
 *	data		(input) pointer to a structure containing a FILE *
 *			        and a record counter.
 *	entry		(input) entry to get dumped.
 * 	<return value>	void
 *
 * Requires:
 *	nuttin
 * 
 * Effects:
 *	writes data to the specified file pointerp.
 *
 * Modifies:
 *	nuttin
 * 
 */
krb5_error_code print_princ(krb5_pointer data, krb5_db_entry *kdb)
{
    char *princstr;
    int	x, y, foundcrc, ret;
    struct retdata *d;
    krb5_tl_data tl_data;
    osa_princ_ent_rec adb;
    XDR xdrs;

    d = (struct retdata *) data;

    /*
     * XXX Currently, lookup_tl_data always returns zero; it sets
     * tl_data->tl_data_length to zero if the type isn't found.
     * This should be fixed...
     */
    /*
     * XXX Should this function do nothing for a principal with no
     * admin data, or print a record of "default" values?   See
     * comment in server_kdb.c to help decide.
     */
    tl_data.tl_data_type = KRB5_TL_KADM_DATA;
    if ((ret = krb5_dbe_lookup_tl_data(d->context, kdb, &tl_data))
	|| (tl_data.tl_data_length == 0))
	 return(0);

    memset(&adb, 0, sizeof(adb));
    xdrmem_create(&xdrs, tl_data.tl_data_contents,
		  tl_data.tl_data_length, XDR_DECODE);
    if (! xdr_osa_princ_ent_rec(&xdrs, &adb)) {
	 xdr_destroy(&xdrs);
	 return(OSA_ADB_XDR_FAILURE);
    }
    xdr_destroy(&xdrs);
    
    krb5_unparse_name(d->context, kdb->princ, &princstr);
    fprintf(d->fp, "princ\t%s\t", princstr);
    if(adb.policy == NULL)
	fputc('\t', d->fp);
    else
	fprintf(d->fp, "%s\t", adb.policy);
    fprintf(d->fp, "%x\t%d\t%d\t%d", adb.aux_attributes,
	    adb.old_key_len,adb.old_key_next, adb.admin_history_kvno);

    for (x = 0; x < adb.old_key_len; x++) {
	 if (! d->ovsec_compat)
	      fprintf(d->fp, "\t%d", adb.old_keys[x].n_key_data);

	 foundcrc = 0;
	 for (y = 0; y < adb.old_keys[x].n_key_data; y++) {
	      krb5_key_data *key_data = &adb.old_keys[x].key_data[y];

	      if (d->ovsec_compat) {
		   if (key_data->key_data_type[0] != ENCTYPE_DES_CBC_CRC)
			continue;
		   if (foundcrc) {
			fprintf(stderr, error_message(EXPORT_DUP_DESCRC), 
				princstr);
			continue;
		   }
		   foundcrc++;
	      }
	      fputc('\t', d->fp);
	      print_key_data(d->fp, key_data);
	 }
	 if (d->ovsec_compat && !foundcrc)
	      fprintf(stderr, error_message(EXPORT_NO_DESCRC), princstr);
    }

    d->count++;
    fputc('\n', d->fp);
    free(princstr);
    return(0);
}

/*
 * Function: print_policy
 * 
 * Purpose: Print the contents of a policy entry in a human readable format.
 *	    This format is also suitable for consumption for dbimport.
 *
 * Arguments:
 *	data		(input) a pointer to a structure containing a FILE *
 *			        and a record counter.
 *	entry		(input) policy entry
 * 	<return value>	void
 *
 * Requires:
 *	nuttin
 * 
 * Effects:
 *	writes data to file
 *
 * Modifies:
 *	nuttin
 * 
 */

void
print_policy(void *data, osa_policy_ent_t entry)
{
    struct  retdata *d;

    d = (struct retdata *) data;
    fprintf(d->fp, "policy\t%s\t%d\t%d\t%d\t%d\t%d\t%d\n", entry->name,
	    entry->pw_min_life, entry->pw_max_life, entry->pw_min_length,
	    entry->pw_min_classes, entry->pw_history_num,
	    entry->policy_refcnt);
    d->count++;
    return;
}

/*
 * Function: export_principal
 * 
 * Purpose:  interates through the principal database with the
 *	     osa_adb_iter_princ function which calls the print_princ
 *	     routine with the FILE * of our filename. If the file
 *	     name that gets passed in is NULL then we use stdout.
 *
 * Arguments:
 *	d		(input) pointer to retdata.
 * 	<return value>	error code. 0 if sucsessful.
 *
 * Requires:
 *	nuttin
 * 
 * Effects:
 *	calls osa_adb_iter_princ which calls print_princ
 *
 * Modifies:
 *	nuttin
 * 
 */
osa_adb_ret_t
export_principal(struct retdata *d, kadm5_config_params *params)
{
     int ret;

     if (ret = krb5_db_set_name(d->context, params->dbname))
	  return ret;

     if (ret = krb5_db_init(d->context))
	  return ret;

     if (ret = krb5_dbm_db_iterate(d->context, print_princ, d))
	  return ret;

     if (ret = krb5_db_fini(d->context))
	  return ret;

     return 0;
}

/*
 * Function: export_policy
 * 
 * Purpose: iterates through the policy database with the
 *	    osa_adb_iter_policy function which calls the print_policy
 *	    routine with the FILE * of our filename. If the file name
 *	    that gets passed in is NULL then we use stdout.
 *
 * Arguments:
 *	d		(input) a pointer to retdata
 * 	<return value>	error code 0 if sucsessfull.
 *
 * Requires:
 *	nuttin
 * 
 * Effects:
 *	calls osa_adb_iter_policy which calls print_policy
 *
 * Modifies:
 *	nuttin
 * 
 */
osa_adb_ret_t
export_policy(struct retdata *d, osa_adb_policy_t db)
{
    osa_adb_ret_t	ret;
    
    if((ret = osa_adb_iter_policy(db, print_policy, (void *) d))
       != OSA_ADB_OK) {
	return ret;
    }
    return OSA_ADB_OK;
}