4 #include <gssapi/gssapi.h>
8 static gss_OID_desc gss_mech_krb5_desc
=
9 { 9, (void *)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02" };
10 static gss_OID
const gss_mech_krb5
= &gss_mech_krb5_desc
;
12 typedef struct uxSsh_gss_ctx
{
18 int ssh_gss_init(void)
20 /* On Windows this tries to load the SSPI library functions. On
21 Unix we assume we have GSSAPI at runtime if we were linked with
26 Ssh_gss_stat
ssh_gss_indicate_mech(Ssh_gss_buf
*mech
)
28 /* Copy constant into mech */
29 mech
->len
= gss_mech_krb5
->length
;
30 mech
->data
= gss_mech_krb5
->elements
;
35 Ssh_gss_stat
ssh_gss_import_name(char *host
,
36 Ssh_gss_name
*srv_name
)
38 OM_uint32 min_stat
,maj_stat
;
39 gss_buffer_desc host_buf
;
42 pStr
= dupcat("host@", host
, NULL
);
44 host_buf
.value
= pStr
;
45 host_buf
.length
= strlen(pStr
);
47 maj_stat
= gss_import_name(&min_stat
, &host_buf
,
48 GSS_C_NT_HOSTBASED_SERVICE
,
49 (gss_name_t
*)srv_name
);
52 if (maj_stat
== GSS_S_COMPLETE
) return SSH_GSS_OK
;
53 return SSH_GSS_FAILURE
;
56 Ssh_gss_stat
ssh_gss_acquire_cred(Ssh_gss_ctx
*ctx
)
58 uxSsh_gss_ctx
*uxctx
= snew(uxSsh_gss_ctx
);
60 uxctx
->maj_stat
= uxctx
->min_stat
= GSS_S_COMPLETE
;
61 uxctx
->ctx
= GSS_C_NO_CONTEXT
;
62 *ctx
= (Ssh_gss_ctx
) uxctx
;
67 Ssh_gss_stat
ssh_gss_init_sec_context(Ssh_gss_ctx
*ctx
,
68 Ssh_gss_name srv_name
,
70 Ssh_gss_buf
*recv_tok
,
71 Ssh_gss_buf
*send_tok
)
73 uxSsh_gss_ctx
*uxctx
= (uxSsh_gss_ctx
*) *ctx
;
76 if (to_deleg
) to_deleg
= GSS_C_DELEG_FLAG
;
77 uxctx
->maj_stat
= gss_init_sec_context(&uxctx
->min_stat
,
80 (gss_name_t
) srv_name
,
81 (gss_OID
) gss_mech_krb5
,
83 GSS_C_INTEG_FLAG
| to_deleg
,
85 NULL
, /* no channel bindings */
86 (gss_buffer_desc
*)recv_tok
,
87 NULL
, /* ignore mech type */
88 (gss_buffer_desc
*)send_tok
,
90 NULL
); /* ignore time_rec */
92 if (uxctx
->maj_stat
== GSS_S_COMPLETE
) return SSH_GSS_S_COMPLETE
;
93 if (uxctx
->maj_stat
== GSS_S_CONTINUE_NEEDED
) return SSH_GSS_S_CONTINUE_NEEDED
;
94 return SSH_GSS_FAILURE
;
97 Ssh_gss_stat
ssh_gss_display_status(Ssh_gss_ctx ctx
, Ssh_gss_buf
*buf
)
99 uxSsh_gss_ctx
*uxctx
= (uxSsh_gss_ctx
*) ctx
;
102 gss_buffer_desc msg_maj
=GSS_C_EMPTY_BUFFER
;
103 gss_buffer_desc msg_min
=GSS_C_EMPTY_BUFFER
;
105 /* Return empty buffer in case of failure */
106 SSH_GSS_CLEAR_BUF(buf
);
108 /* get first mesg from GSS */
110 lmax
=gss_display_status(&lmin
,uxctx
->maj_stat
,GSS_C_GSS_CODE
,(gss_OID
) gss_mech_krb5
,&ccc
,&msg_maj
);
112 if (lmax
!= GSS_S_COMPLETE
) return SSH_GSS_FAILURE
;
114 /* get first mesg from Kerberos */
116 lmax
=gss_display_status(&lmin
,uxctx
->min_stat
,GSS_C_MECH_CODE
,(gss_OID
) gss_mech_krb5
,&ccc
,&msg_min
);
118 if (lmax
!= GSS_S_COMPLETE
) {
119 gss_release_buffer(&lmin
, &msg_maj
);
120 return SSH_GSS_FAILURE
;
123 /* copy data into buffer */
124 buf
->len
= msg_maj
.length
+ msg_min
.length
+ 1;
125 buf
->data
= snewn(buf
->len
+ 1, char);
128 memcpy(buf
->data
, msg_maj
.value
, msg_maj
.length
);
129 buf
->data
[msg_maj
.length
] = ' ';
130 memcpy(buf
->data
+ msg_maj
.length
+ 1, msg_min
.value
, msg_min
.length
);
131 buf
->data
[buf
->len
] = 0;
132 /* free mem & exit */
133 gss_release_buffer(&lmin
, &msg_maj
);
134 gss_release_buffer(&lmin
, &msg_min
);
138 Ssh_gss_stat
ssh_gss_free_tok(Ssh_gss_buf
*send_tok
)
140 OM_uint32 min_stat
,maj_stat
;
141 maj_stat
= gss_release_buffer(&min_stat
, (gss_buffer_desc
*)send_tok
);
143 if (maj_stat
== GSS_S_COMPLETE
) return SSH_GSS_OK
;
144 return SSH_GSS_FAILURE
;
147 Ssh_gss_stat
ssh_gss_release_cred(Ssh_gss_ctx
*ctx
)
149 uxSsh_gss_ctx
*uxctx
= (uxSsh_gss_ctx
*) *ctx
;
151 OM_uint32 maj_stat
=GSS_S_COMPLETE
;
153 if (uxctx
== NULL
) return SSH_GSS_FAILURE
;
154 if (uxctx
->ctx
!= GSS_C_NO_CONTEXT
)
155 maj_stat
= gss_delete_sec_context(&min_stat
,&uxctx
->ctx
,GSS_C_NO_BUFFER
);
158 if (maj_stat
== GSS_S_COMPLETE
) return SSH_GSS_OK
;
159 return SSH_GSS_FAILURE
;
163 Ssh_gss_stat
ssh_gss_release_name(Ssh_gss_name
*srv_name
)
165 OM_uint32 min_stat
,maj_stat
;
166 maj_stat
= gss_release_name(&min_stat
, (gss_name_t
*) srv_name
);
168 if (maj_stat
== GSS_S_COMPLETE
) return SSH_GSS_OK
;
169 return SSH_GSS_FAILURE
;
172 Ssh_gss_stat
ssh_gss_get_mic(Ssh_gss_ctx ctx
, Ssh_gss_buf
*buf
,
175 uxSsh_gss_ctx
*uxctx
= (uxSsh_gss_ctx
*) ctx
;
176 if (uxctx
== NULL
) return SSH_GSS_FAILURE
;
177 return gss_get_mic(&(uxctx
->min_stat
),
180 (gss_buffer_desc
*)buf
,
181 (gss_buffer_desc
*)hash
);
184 Ssh_gss_stat
ssh_gss_free_mic(Ssh_gss_buf
*hash
)
186 /* On Unix this is the same freeing process as ssh_gss_free_tok. */
187 return ssh_gss_free_tok(hash
);
192 /* Dummy function so this source file defines something if NO_GSSAPI
195 int ssh_gss_init(void)