/ kadmin / dump.c
dump.c
  1  /*
  2   * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
  3   * (Royal Institute of Technology, Stockholm, Sweden).
  4   * All rights reserved.
  5   *
  6   * Redistribution and use in source and binary forms, with or without
  7   * modification, are permitted provided that the following conditions
  8   * are met:
  9   *
 10   * 1. Redistributions of source code must retain the above copyright
 11   *    notice, this list of conditions and the following disclaimer.
 12   *
 13   * 2. Redistributions in binary form must reproduce the above copyright
 14   *    notice, this list of conditions and the following disclaimer in the
 15   *    documentation and/or other materials provided with the distribution.
 16   *
 17   * 3. Neither the name of the Institute nor the names of its contributors
 18   *    may be used to endorse or promote products derived from this software
 19   *    without specific prior written permission.
 20   *
 21   * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 24   * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 25   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 27   * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 28   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 29   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 30   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 31   * SUCH DAMAGE.
 32   */
 33  
 34  #include "kadmin_locl.h"
 35  #include "kadmin-commands.h"
 36  #include <kadm5/private.h>
 37  
 38  #ifdef __APPLE__
 39  #include <HeimODAdmin.h>
 40  #endif
 41  
 42  extern int local_flag;
 43  
 44  #ifdef __APPLE__
 45  
 46  static krb5_error_code
 47  od_dump_entry(krb5_context kcontext, HDB *db, hdb_entry_ex *entry, void *data)
 48  {
 49      CFErrorRef error = NULL;
 50      CFDictionaryRef dict;
 51      CFStringRef fn, uuidstr;
 52      CFUUIDRef uuid;
 53      CFURLRef url;
 54  
 55      dict = HeimODDumpHdbEntry(&entry->entry, &error);
 56      if (dict == NULL) {
 57  	if (error)
 58  	    CFRelease(error);
 59  	return 0;
 60      }
 61  
 62      uuid = CFUUIDCreate(NULL);
 63      if (uuid == NULL) {
 64  	krb5_warnx(kcontext, "out of memory");
 65          CFRelease(dict);
 66  	return 0;
 67      }
 68      
 69      uuidstr = CFUUIDCreateString(NULL, uuid);
 70      CFRelease(uuid);
 71      if (uuidstr == NULL) {
 72  	krb5_warnx(kcontext, "out of memory");
 73          CFRelease(dict);
 74  	return 0;
 75      }
 76  
 77      fn = CFStringCreateWithFormat(NULL, NULL, CFSTR("%s/%@.plist"), (char *)data, uuidstr);
 78      CFRelease(uuidstr);
 79      if (fn == NULL) {
 80  	krb5_warnx(kcontext, "out of memory");
 81          CFRelease(dict);
 82  	return 0;
 83      }
 84  
 85      url = CFURLCreateWithFileSystemPath(NULL, fn,  kCFURLPOSIXPathStyle, false);
 86      CFRelease(fn);
 87      if (url == NULL) {
 88  	krb5_warnx(kcontext, "out of memory");
 89          CFRelease(dict);
 90  	return 0;
 91      }
 92  
 93      CFDataRef xmldata = CFPropertyListCreateData(NULL, dict, kCFPropertyListXMLFormat_v1_0, 0, NULL);
 94      CFRelease(dict);
 95      if (xmldata == NULL) {
 96  	CFRelease(url);
 97  	krb5_warnx(kcontext, "out of memory");
 98  	return 0;
 99      }
100  
101      CFWriteStreamRef stream = CFWriteStreamCreateWithFile(NULL, url);
102      if (stream) {
103  	if (CFWriteStreamOpen(stream))
104  	    CFWriteStreamWrite(stream, CFDataGetBytePtr(xmldata), CFDataGetLength(xmldata));
105  	CFWriteStreamClose(stream);
106  	CFRelease(stream);
107      }
108  
109      CFRelease(url);
110      CFRelease(xmldata);
111  
112      return 0;
113  }
114  #endif
115  
116  int
117  dump(struct dump_options *opt, int argc, char **argv)
118  {
119      krb5_error_code (*func)(krb5_context, HDB *, hdb_entry_ex *, void *);
120      krb5_error_code ret;
121      void *arg;
122      const char *format = "heimdal";
123      FILE *f = NULL;
124  
125      if (opt->format_string)
126  	format = opt->format_string;
127  
128      if (strcasecmp(format, "heimdal") == 0) {
129  	func = hdb_print_entry;
130  
131  	if (argc == 0) {
132  	    arg = stdout;
133  	} else {
134  	    arg = f = fopen(argv[0], "w");
135  	    if (f == NULL) {
136  		krb5_warn(context, errno, "failed to open %s", argv[0]);
137  		return 0;
138  	    }
139  	}
140  #ifdef __APPLE__
141      } else if (strcasecmp(format, "od") == 0) {
142  	
143  	func = od_dump_entry;
144  	if (argc == 0)
145  	    arg = rk_UNCONST(".");
146  	else
147  	    arg = argv[0];
148  #endif
149      } else {
150  	krb5_warnx(context, "unknown dump format: %s", format);
151  	return 0;
152      }
153  
154      if (opt->mit_dump_file_string) {
155  	ret = hdb_mit_dump(context, opt->mit_dump_file_string,
156  			   func, arg);
157  	if (ret)
158  	    krb5_warn(context, ret, "hdb_mit_dump");
159  
160      } else {
161  	HDB *db = NULL;
162  
163  	if (!local_flag) {
164  	    krb5_warnx(context, "od-dump is only available in local (-l) mode");
165  	    return 0;
166  	}
167  
168  	db = _kadm5_s_get_db(kadm_handle);
169  
170  	ret = db->hdb_open(context, db, O_RDONLY, 0600);
171  	if (ret) {
172  	    krb5_warn(context, ret, "hdb_open");
173  	    goto out;
174  	}
175  
176  	ret = hdb_foreach(context, db, opt->decrypt_flag ? HDB_F_DECRYPT : 0,
177  			  func, arg);
178  	if (ret)
179  	    krb5_warn(context, ret, "hdb_foreach");
180  
181  	db->hdb_close(context, db);
182      }
183      if (f)
184  	fclose(f);
185  out:
186      return ret != 0;
187  }
188  
189  int
190  od_dump(struct od_dump_options *opt, int argc, char **argv)
191  {
192      struct dump_options dumpopt;
193  
194      memset(&dumpopt, 0, sizeof(dumpopt));
195      dumpopt.decrypt_flag = opt->decrypt_flag;
196      dumpopt.format_string = "od";
197  
198      return dump(&dumpopt, argc, argv);
199  }