]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - crypto/heimdal/lib/gssapi/gss.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / crypto / heimdal / lib / gssapi / gss.c
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
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <stdio.h>
39 #include <gssapi.h>
40 #include <err.h>
41 #include <roken.h>
42 #include <getarg.h>
43 #include <rtbl.h>
44 #include <gss-commands.h>
45 #include <krb5.h>
46
47 RCSID("$Id: gss.c 19922 2007-01-16 09:32:03Z lha $");
48
49 static int version_flag = 0;
50 static int help_flag    = 0;
51
52 static struct getargs args[] = {
53     {"version", 0,      arg_flag,       &version_flag, "print version", NULL },
54     {"help",    0,      arg_flag,       &help_flag,  NULL, NULL }
55 };
56
57 static void
58 usage (int ret)
59 {
60     arg_printusage (args, sizeof(args)/sizeof(*args),
61                     NULL, "service@host");
62     exit (ret);
63 }
64
65 #define COL_OID         "OID"
66 #define COL_NAME        "Name"
67
68 int
69 supported_mechanisms(void *argptr, int argc, char **argv)
70 {
71     OM_uint32 maj_stat, min_stat;
72     gss_OID_set mechs;
73     rtbl_t ct;
74     size_t i;
75
76     maj_stat = gss_indicate_mechs(&min_stat, &mechs);
77     if (maj_stat != GSS_S_COMPLETE)
78         errx(1, "gss_indicate_mechs failed");
79
80     printf("Supported mechanisms:\n");
81
82     ct = rtbl_create();
83     if (ct == NULL)
84         errx(1, "rtbl_create");
85
86     rtbl_set_separator(ct, "  ");
87     rtbl_add_column(ct, COL_OID, 0);
88     rtbl_add_column(ct, COL_NAME, 0);
89
90     for (i = 0; i < mechs->count; i++) {
91         gss_buffer_desc name;
92
93         maj_stat = gss_oid_to_str(&min_stat, &mechs->elements[i], &name);
94         if (maj_stat != GSS_S_COMPLETE)
95             errx(1, "gss_oid_to_str failed");
96
97         rtbl_add_column_entryv(ct, COL_OID, "%.*s",
98                                (int)name.length, (char *)name.value);
99         gss_release_buffer(&min_stat, &name);
100
101         if (gss_oid_equal(&mechs->elements[i], GSS_KRB5_MECHANISM))
102             rtbl_add_column_entry(ct, COL_NAME, "Kerberos 5");
103         else if (gss_oid_equal(&mechs->elements[i], GSS_SPNEGO_MECHANISM))
104             rtbl_add_column_entry(ct, COL_NAME, "SPNEGO");
105         else if (gss_oid_equal(&mechs->elements[i], GSS_NTLM_MECHANISM))
106             rtbl_add_column_entry(ct, COL_NAME, "NTLM");
107     }
108     gss_release_oid_set(&min_stat, &mechs);
109
110     rtbl_format(ct, stdout);
111     rtbl_destroy(ct);
112
113     return 0;
114 }
115
116 #if 0
117 /*
118  *
119  */
120
121 #define DOVEDOT_MAJOR_VERSION 1
122 #define DOVEDOT_MINOR_VERSION 0
123
124 /*
125         S: MECH mech mech-parameters
126         S: MECH mech mech-parameters
127         S: VERSION major minor
128         S: CPID pid
129         S: CUID pid
130         S: ...
131         S: DONE
132         C: VERSION major minor
133         C: CPID pid
134
135         C: AUTH id method service= resp=
136         C: CONT id message
137
138         S: OK id user=
139         S: FAIL id reason=
140         S: CONTINUE id message
141 */
142
143 int
144 dovecot_server(void *argptr, int argc, char **argv)
145 {
146     krb5_storage *sp;
147     int fd = 0;
148
149     sp = krb5_storage_from_fd(fd);
150     if (sp == NULL)
151         errx(1, "krb5_storage_from_fd");
152
153     krb5_store_stringnl(sp, "MECH\tGSSAPI");
154     krb5_store_stringnl(sp, "VERSION\t1\t0");
155     krb5_store_stringnl(sp, "DONE");
156
157     while (1) {
158         char *cmd;
159         if (krb5_ret_stringnl(sp, &cmd) != 0)
160             break;
161         printf("cmd: %s\n", cmd);
162         free(cmd);
163     }
164     return 0;
165 }
166 #endif
167
168 /*
169  *
170  */
171
172 int
173 help(void *opt, int argc, char **argv)
174 {
175     sl_slc_help(commands, argc, argv);
176     return 0;
177 }
178
179 int
180 main(int argc, char **argv)
181 {
182     int optidx = 0;
183
184     setprogname(argv[0]);
185     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
186         usage(1);
187     
188     if (help_flag)
189         usage (0);
190
191     if(version_flag){
192         print_version(NULL);
193         exit(0);
194     }
195
196     argc -= optidx;
197     argv += optidx;
198
199     if (argc == 0) {
200         help(NULL, argc, argv);
201         return 1;
202     }
203
204     return sl_command (commands, argc, argv);
205 }