/crypto/heimdal/kadmin/kadmin.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 286 lines · 216 code · 33 blank · 37 comment · 46 complexity · 2ed3332364cae5a0e1d14f92ff1697c9 MD5 · raw file

  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. #include "kadmin_locl.h"
  34. #include "kadmin-commands.h"
  35. #include <sl.h>
  36. static char *config_file;
  37. static char *keyfile;
  38. int local_flag;
  39. static int ad_flag;
  40. static int help_flag;
  41. static int version_flag;
  42. static char *realm;
  43. static char *admin_server;
  44. static int server_port = 0;
  45. static char *client_name;
  46. static char *keytab;
  47. static char *check_library = NULL;
  48. static char *check_function = NULL;
  49. static getarg_strings policy_libraries = { 0, NULL };
  50. static struct getargs args[] = {
  51. { "principal", 'p', arg_string, &client_name,
  52. "principal to authenticate as", NULL },
  53. { "keytab", 'K', arg_string, &keytab,
  54. "keytab for authentication principal", NULL },
  55. {
  56. "config-file", 'c', arg_string, &config_file,
  57. "location of config file", "file"
  58. },
  59. {
  60. "key-file", 'k', arg_string, &keyfile,
  61. "location of master key file", "file"
  62. },
  63. {
  64. "realm", 'r', arg_string, &realm,
  65. "realm to use", "realm"
  66. },
  67. {
  68. "admin-server", 'a', arg_string, &admin_server,
  69. "server to contact", "host"
  70. },
  71. {
  72. "server-port", 's', arg_integer, &server_port,
  73. "port to use", "port number"
  74. },
  75. { "ad", 0, arg_flag, &ad_flag, "active directory admin mode",
  76. NULL },
  77. #ifdef HAVE_DLOPEN
  78. { "check-library", 0, arg_string, &check_library,
  79. "library to load password check function from", "library" },
  80. { "check-function", 0, arg_string, &check_function,
  81. "password check function to load", "function" },
  82. { "policy-libraries", 0, arg_strings, &policy_libraries,
  83. "password check function to load", "function" },
  84. #endif
  85. { "local", 'l', arg_flag, &local_flag, "local admin mode", NULL },
  86. { "help", 'h', arg_flag, &help_flag, NULL, NULL },
  87. { "version", 'v', arg_flag, &version_flag, NULL, NULL }
  88. };
  89. static int num_args = sizeof(args) / sizeof(args[0]);
  90. krb5_context context;
  91. void *kadm_handle;
  92. int
  93. help(void *opt, int argc, char **argv)
  94. {
  95. sl_slc_help(commands, argc, argv);
  96. return 0;
  97. }
  98. static int exit_seen = 0;
  99. int
  100. exit_kadmin (void *opt, int argc, char **argv)
  101. {
  102. exit_seen = 1;
  103. return 0;
  104. }
  105. static void
  106. usage(int ret)
  107. {
  108. arg_printusage (args, num_args, NULL, "[command]");
  109. exit (ret);
  110. }
  111. int
  112. get_privs(void *opt, int argc, char **argv)
  113. {
  114. uint32_t privs;
  115. char str[128];
  116. kadm5_ret_t ret;
  117. ret = kadm5_get_privs(kadm_handle, &privs);
  118. if(ret)
  119. krb5_warn(context, ret, "kadm5_get_privs");
  120. else{
  121. ret =_kadm5_privs_to_string(privs, str, sizeof(str));
  122. if (ret == 0)
  123. printf("%s\n", str);
  124. else
  125. printf("privs: 0x%x\n", (unsigned int)privs);
  126. }
  127. return 0;
  128. }
  129. int
  130. main(int argc, char **argv)
  131. {
  132. krb5_error_code ret;
  133. char **files;
  134. kadm5_config_params conf;
  135. int optidx = 0;
  136. int exit_status = 0;
  137. setprogname(argv[0]);
  138. ret = krb5_init_context(&context);
  139. if (ret)
  140. errx (1, "krb5_init_context failed: %d", ret);
  141. if(getarg(args, num_args, argc, argv, &optidx))
  142. usage(1);
  143. if (help_flag)
  144. usage (0);
  145. if (version_flag) {
  146. print_version(NULL);
  147. exit(0);
  148. }
  149. argc -= optidx;
  150. argv += optidx;
  151. if (config_file == NULL) {
  152. asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context));
  153. if (config_file == NULL)
  154. errx(1, "out of memory");
  155. }
  156. ret = krb5_prepend_config_files_default(config_file, &files);
  157. if (ret)
  158. krb5_err(context, 1, ret, "getting configuration files");
  159. ret = krb5_set_config_files(context, files);
  160. krb5_free_config_files(files);
  161. if(ret)
  162. krb5_err(context, 1, ret, "reading configuration files");
  163. memset(&conf, 0, sizeof(conf));
  164. if(realm) {
  165. krb5_set_default_realm(context, realm); /* XXX should be fixed
  166. some other way */
  167. conf.realm = realm;
  168. conf.mask |= KADM5_CONFIG_REALM;
  169. }
  170. if (admin_server) {
  171. conf.admin_server = admin_server;
  172. conf.mask |= KADM5_CONFIG_ADMIN_SERVER;
  173. }
  174. if (server_port) {
  175. conf.kadmind_port = htons(server_port);
  176. conf.mask |= KADM5_CONFIG_KADMIND_PORT;
  177. }
  178. if (keyfile) {
  179. conf.stash_file = keyfile;
  180. conf.mask |= KADM5_CONFIG_STASH_FILE;
  181. }
  182. if(local_flag) {
  183. int i;
  184. kadm5_setup_passwd_quality_check (context,
  185. check_library, check_function);
  186. for (i = 0; i < policy_libraries.num_strings; i++) {
  187. ret = kadm5_add_passwd_quality_verifier(context,
  188. policy_libraries.strings[i]);
  189. if (ret)
  190. krb5_err(context, 1, ret, "kadm5_add_passwd_quality_verifier");
  191. }
  192. ret = kadm5_add_passwd_quality_verifier(context, NULL);
  193. if (ret)
  194. krb5_err(context, 1, ret, "kadm5_add_passwd_quality_verifier");
  195. ret = kadm5_s_init_with_password_ctx(context,
  196. KADM5_ADMIN_SERVICE,
  197. NULL,
  198. KADM5_ADMIN_SERVICE,
  199. &conf, 0, 0,
  200. &kadm_handle);
  201. } else if (ad_flag) {
  202. if (client_name == NULL)
  203. krb5_errx(context, 1, "keytab mode require principal name");
  204. ret = kadm5_ad_init_with_password_ctx(context,
  205. client_name,
  206. NULL,
  207. KADM5_ADMIN_SERVICE,
  208. &conf, 0, 0,
  209. &kadm_handle);
  210. } else if (keytab) {
  211. if (client_name == NULL)
  212. krb5_errx(context, 1, "keytab mode require principal name");
  213. ret = kadm5_c_init_with_skey_ctx(context,
  214. client_name,
  215. keytab,
  216. KADM5_ADMIN_SERVICE,
  217. &conf, 0, 0,
  218. &kadm_handle);
  219. } else
  220. ret = kadm5_c_init_with_password_ctx(context,
  221. client_name,
  222. NULL,
  223. KADM5_ADMIN_SERVICE,
  224. &conf, 0, 0,
  225. &kadm_handle);
  226. if(ret)
  227. krb5_err(context, 1, ret, "kadm5_init_with_password");
  228. signal(SIGINT, SIG_IGN); /* ignore signals for now, the sl command
  229. parser will handle SIGINT its own way;
  230. we should really take care of this in
  231. each function, f.i `get' might be
  232. interruptable, but not `create' */
  233. if (argc != 0) {
  234. ret = sl_command (commands, argc, argv);
  235. if(ret == -1)
  236. krb5_warnx (context, "unrecognized command: %s", argv[0]);
  237. else if (ret == -2)
  238. ret = 0;
  239. if(ret != 0)
  240. exit_status = 1;
  241. } else {
  242. while(!exit_seen) {
  243. ret = sl_command_loop(commands, "kadmin> ", NULL);
  244. if (ret == -2)
  245. exit_seen = 1;
  246. else if (ret != 0)
  247. exit_status = 1;
  248. }
  249. }
  250. kadm5_destroy(kadm_handle);
  251. krb5_free_context(context);
  252. return exit_status;
  253. }