/crypto/heimdal/kadmin/stash.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 151 lines · 105 code · 11 blank · 35 comment · 37 complexity · 351bee0dfad629736696f09e88fd9615 MD5 · raw file

  1. /*
  2. * Copyright (c) 2004 Kungliga Tekniska Hรถgskolan
  3. * (Royal Institute of Technology, Stockholm, Sweden).
  4. * All rights reserved.
  5. *
  6. * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * 3. Neither the name of the Institute nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. */
  35. #include "kadmin_locl.h"
  36. #include "kadmin-commands.h"
  37. extern int local_flag;
  38. int
  39. stash(struct stash_options *opt, int argc, char **argv)
  40. {
  41. char buf[1024];
  42. krb5_error_code ret;
  43. krb5_enctype enctype;
  44. hdb_master_key mkey;
  45. if(!local_flag) {
  46. krb5_warnx(context, "stash is only available in local (-l) mode");
  47. return 0;
  48. }
  49. ret = krb5_string_to_enctype(context, opt->enctype_string, &enctype);
  50. if(ret) {
  51. krb5_warn(context, ret, "%s", opt->enctype_string);
  52. return 0;
  53. }
  54. if(opt->key_file_string == NULL) {
  55. asprintf(&opt->key_file_string, "%s/m-key", hdb_db_dir(context));
  56. if (opt->key_file_string == NULL)
  57. errx(1, "out of memory");
  58. }
  59. ret = hdb_read_master_key(context, opt->key_file_string, &mkey);
  60. if(ret && ret != ENOENT) {
  61. krb5_warn(context, ret, "reading master key from %s",
  62. opt->key_file_string);
  63. return 0;
  64. }
  65. if (opt->convert_file_flag) {
  66. if (ret)
  67. krb5_warn(context, ret, "reading master key from %s",
  68. opt->key_file_string);
  69. return 0;
  70. } else {
  71. krb5_keyblock key;
  72. krb5_salt salt;
  73. salt.salttype = KRB5_PW_SALT;
  74. /* XXX better value? */
  75. salt.saltvalue.data = NULL;
  76. salt.saltvalue.length = 0;
  77. if(opt->master_key_fd_integer != -1) {
  78. ssize_t n;
  79. n = read(opt->master_key_fd_integer, buf, sizeof(buf));
  80. if(n == 0)
  81. krb5_warnx(context, "end of file reading passphrase");
  82. else if(n < 0) {
  83. krb5_warn(context, errno, "reading passphrase");
  84. n = 0;
  85. }
  86. buf[n] = '\0';
  87. buf[strcspn(buf, "\r\n")] = '\0';
  88. } else if (opt->random_password_flag) {
  89. random_password (buf, sizeof(buf));
  90. printf("Using random master stash password: %s\n", buf);
  91. } else {
  92. if(UI_UTIL_read_pw_string(buf, sizeof(buf), "Master key: ", 1)) {
  93. hdb_free_master_key(context, mkey);
  94. return 0;
  95. }
  96. }
  97. ret = krb5_string_to_key_salt(context, enctype, buf, salt, &key);
  98. ret = hdb_add_master_key(context, &key, &mkey);
  99. krb5_free_keyblock_contents(context, &key);
  100. }
  101. {
  102. char *new, *old;
  103. asprintf(&old, "%s.old", opt->key_file_string);
  104. asprintf(&new, "%s.new", opt->key_file_string);
  105. if(old == NULL || new == NULL) {
  106. ret = ENOMEM;
  107. goto out;
  108. }
  109. if(unlink(new) < 0 && errno != ENOENT) {
  110. ret = errno;
  111. goto out;
  112. }
  113. krb5_warnx(context, "writing key to \"%s\"", opt->key_file_string);
  114. ret = hdb_write_master_key(context, new, mkey);
  115. if(ret)
  116. unlink(new);
  117. else {
  118. unlink(old);
  119. #ifndef NO_POSIX_LINKS
  120. if(link(opt->key_file_string, old) < 0 && errno != ENOENT) {
  121. ret = errno;
  122. unlink(new);
  123. } else {
  124. #endif
  125. if(rename(new, opt->key_file_string) < 0) {
  126. ret = errno;
  127. }
  128. #ifndef NO_POSIX_LINKS
  129. }
  130. #endif
  131. }
  132. out:
  133. free(old);
  134. free(new);
  135. if(ret)
  136. krb5_warn(context, errno, "writing master key file");
  137. }
  138. hdb_free_master_key(context, mkey);
  139. return 0;
  140. }