/crypto/heimdal/lib/gssapi/gss.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 205 lines · 115 code · 34 blank · 56 comment · 22 complexity · b06f72de80f9ead7fb4692ca641e26f3 MD5 · raw file

  1. /*
  2. * Copyright (c) 2006 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 KTH nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software without
  19. * specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
  22. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  28. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  31. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #ifdef HAVE_CONFIG_H
  34. #include <config.h>
  35. #endif
  36. #include <stdio.h>
  37. #include <gssapi.h>
  38. #include <err.h>
  39. #include <roken.h>
  40. #include <getarg.h>
  41. #include <rtbl.h>
  42. #include <gss-commands.h>
  43. #include <krb5.h>
  44. RCSID("$Id: gss.c 19922 2007-01-16 09:32:03Z lha $");
  45. static int version_flag = 0;
  46. static int help_flag = 0;
  47. static struct getargs args[] = {
  48. {"version", 0, arg_flag, &version_flag, "print version", NULL },
  49. {"help", 0, arg_flag, &help_flag, NULL, NULL }
  50. };
  51. static void
  52. usage (int ret)
  53. {
  54. arg_printusage (args, sizeof(args)/sizeof(*args),
  55. NULL, "service@host");
  56. exit (ret);
  57. }
  58. #define COL_OID "OID"
  59. #define COL_NAME "Name"
  60. int
  61. supported_mechanisms(void *argptr, int argc, char **argv)
  62. {
  63. OM_uint32 maj_stat, min_stat;
  64. gss_OID_set mechs;
  65. rtbl_t ct;
  66. size_t i;
  67. maj_stat = gss_indicate_mechs(&min_stat, &mechs);
  68. if (maj_stat != GSS_S_COMPLETE)
  69. errx(1, "gss_indicate_mechs failed");
  70. printf("Supported mechanisms:\n");
  71. ct = rtbl_create();
  72. if (ct == NULL)
  73. errx(1, "rtbl_create");
  74. rtbl_set_separator(ct, " ");
  75. rtbl_add_column(ct, COL_OID, 0);
  76. rtbl_add_column(ct, COL_NAME, 0);
  77. for (i = 0; i < mechs->count; i++) {
  78. gss_buffer_desc name;
  79. maj_stat = gss_oid_to_str(&min_stat, &mechs->elements[i], &name);
  80. if (maj_stat != GSS_S_COMPLETE)
  81. errx(1, "gss_oid_to_str failed");
  82. rtbl_add_column_entryv(ct, COL_OID, "%.*s",
  83. (int)name.length, (char *)name.value);
  84. gss_release_buffer(&min_stat, &name);
  85. if (gss_oid_equal(&mechs->elements[i], GSS_KRB5_MECHANISM))
  86. rtbl_add_column_entry(ct, COL_NAME, "Kerberos 5");
  87. else if (gss_oid_equal(&mechs->elements[i], GSS_SPNEGO_MECHANISM))
  88. rtbl_add_column_entry(ct, COL_NAME, "SPNEGO");
  89. else if (gss_oid_equal(&mechs->elements[i], GSS_NTLM_MECHANISM))
  90. rtbl_add_column_entry(ct, COL_NAME, "NTLM");
  91. }
  92. gss_release_oid_set(&min_stat, &mechs);
  93. rtbl_format(ct, stdout);
  94. rtbl_destroy(ct);
  95. return 0;
  96. }
  97. #if 0
  98. /*
  99. *
  100. */
  101. #define DOVEDOT_MAJOR_VERSION 1
  102. #define DOVEDOT_MINOR_VERSION 0
  103. /*
  104. S: MECH mech mech-parameters
  105. S: MECH mech mech-parameters
  106. S: VERSION major minor
  107. S: CPID pid
  108. S: CUID pid
  109. S: ...
  110. S: DONE
  111. C: VERSION major minor
  112. C: CPID pid
  113. C: AUTH id method service= resp=
  114. C: CONT id message
  115. S: OK id user=
  116. S: FAIL id reason=
  117. S: CONTINUE id message
  118. */
  119. int
  120. dovecot_server(void *argptr, int argc, char **argv)
  121. {
  122. krb5_storage *sp;
  123. int fd = 0;
  124. sp = krb5_storage_from_fd(fd);
  125. if (sp == NULL)
  126. errx(1, "krb5_storage_from_fd");
  127. krb5_store_stringnl(sp, "MECH\tGSSAPI");
  128. krb5_store_stringnl(sp, "VERSION\t1\t0");
  129. krb5_store_stringnl(sp, "DONE");
  130. while (1) {
  131. char *cmd;
  132. if (krb5_ret_stringnl(sp, &cmd) != 0)
  133. break;
  134. printf("cmd: %s\n", cmd);
  135. free(cmd);
  136. }
  137. return 0;
  138. }
  139. #endif
  140. /*
  141. *
  142. */
  143. int
  144. help(void *opt, int argc, char **argv)
  145. {
  146. sl_slc_help(commands, argc, argv);
  147. return 0;
  148. }
  149. int
  150. main(int argc, char **argv)
  151. {
  152. int optidx = 0;
  153. setprogname(argv[0]);
  154. if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
  155. usage(1);
  156. if (help_flag)
  157. usage (0);
  158. if(version_flag){
  159. print_version(NULL);
  160. exit(0);
  161. }
  162. argc -= optidx;
  163. argv += optidx;
  164. if (argc == 0) {
  165. help(NULL, argc, argv);
  166. return 1;
  167. }
  168. return sl_command (commands, argc, argv);
  169. }