1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/ 5 * Authors: Doug Rabson <dfr@rabson.org> 6 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org> 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 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 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 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/kernel.h> 35 #include <sys/kobj.h> 36 #include <sys/lock.h> 37 #include <sys/malloc.h> 38 #include <sys/module.h> 39 #include <sys/mutex.h> 40 #include <sys/priv.h> 41 #include <sys/syscall.h> 42 #include <sys/sysent.h> 43 #include <sys/sysproto.h> 44 45 #include <kgssapi/gssapi.h> 46 #include <kgssapi/gssapi_impl.h> 47 #include <rpc/rpc.h> 48 #include <rpc/rpc_com.h> 49 #include <rpc/rpcsec_gss.h> 50 51 #include "gssd.h" 52 #include "kgss_if.h" 53 54 MALLOC_DEFINE(M_GSSAPI, "GSS-API", "GSS-API"); 55 56 /* 57 * Syscall hooks 58 */ 59 static int gssd_syscall_offset = SYS_gssd_syscall; 60 static struct sysent gssd_syscall_prev_sysent; 61 MAKE_SYSENT(gssd_syscall); 62 static bool_t gssd_syscall_registered = FALSE; 63 64 struct kgss_mech_list kgss_mechs; 65 CLIENT *kgss_gssd_handle; 66 struct mtx kgss_gssd_lock; 67 68 static void 69 kgss_init(void *dummy) 70 { 71 int error; 72 73 LIST_INIT(&kgss_mechs); 74 error = syscall_register(&gssd_syscall_offset, &gssd_syscall_sysent, 75 &gssd_syscall_prev_sysent, SY_THR_STATIC_KLD); 76 if (error) 77 printf("Can't register GSSD syscall\n"); 78 else 79 gssd_syscall_registered = TRUE; 80 } 81 SYSINIT(kgss_init, SI_SUB_LOCK, SI_ORDER_FIRST, kgss_init, NULL); 82 83 static void 84 kgss_uninit(void *dummy) 85 { 86 87 if (gssd_syscall_registered) 88 syscall_deregister(&gssd_syscall_offset, 89 &gssd_syscall_prev_sysent); 90 } 91 SYSUNINIT(kgss_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, kgss_uninit, NULL); 92 93 int 94 sys_gssd_syscall(struct thread *td, struct gssd_syscall_args *uap) 95 { 96 struct sockaddr_un sun; 97 struct netconfig *nconf; 98 char path[MAXPATHLEN]; 99 int error; 100 CLIENT *cl, *oldcl; 101 102 error = priv_check(td, PRIV_NFS_DAEMON); 103 if (error) 104 return (error); 105 106 error = copyinstr(uap->path, path, sizeof(path), NULL); 107 if (error) 108 return (error); 109 if (strlen(path) + 1 > sizeof(sun.sun_path)) 110 return (EINVAL); 111 112 if (path[0] != '\0') { 113 sun.sun_family = AF_LOCAL; 114 strlcpy(sun.sun_path, path, sizeof(sun.sun_path)); 115 sun.sun_len = SUN_LEN(&sun); 116 117 nconf = getnetconfigent("local"); 118 cl = clnt_reconnect_create(nconf, 119 (struct sockaddr *) &sun, GSSD, GSSDVERS, 120 RPC_MAXDATASIZE, RPC_MAXDATASIZE); 121 } else 122 cl = NULL; 123 124 mtx_lock(&kgss_gssd_lock); 125 oldcl = kgss_gssd_handle; 126 kgss_gssd_handle = cl; 127 mtx_unlock(&kgss_gssd_lock); 128 129 if (oldcl != NULL) { 130 CLNT_CLOSE(oldcl); 131 CLNT_RELEASE(oldcl); 132 } 133 134 return (0); 135 } 136 137 int 138 kgss_oid_equal(const gss_OID oid1, const gss_OID oid2) 139 { 140 141 if (oid1 == oid2) 142 return (1); 143 if (!oid1 || !oid2) 144 return (0); 145 if (oid1->length != oid2->length) 146 return (0); 147 if (memcmp(oid1->elements, oid2->elements, oid1->length)) 148 return (0); 149 return (1); 150 } 151 152 void 153 kgss_install_mech(gss_OID mech_type, const char *name, struct kobj_class *cls) 154 { 155 struct kgss_mech *km; 156 157 km = malloc(sizeof(struct kgss_mech), M_GSSAPI, M_WAITOK); 158 km->km_mech_type = mech_type; 159 km->km_mech_name = name; 160 km->km_class = cls; 161 LIST_INSERT_HEAD(&kgss_mechs, km, km_link); 162 } 163 164 void 165 kgss_uninstall_mech(gss_OID mech_type) 166 { 167 struct kgss_mech *km; 168 169 LIST_FOREACH(km, &kgss_mechs, km_link) { 170 if (kgss_oid_equal(km->km_mech_type, mech_type)) { 171 LIST_REMOVE(km, km_link); 172 free(km, M_GSSAPI); 173 return; 174 } 175 } 176 } 177 178 gss_OID 179 kgss_find_mech_by_name(const char *name) 180 { 181 struct kgss_mech *km; 182 183 LIST_FOREACH(km, &kgss_mechs, km_link) { 184 if (!strcmp(km->km_mech_name, name)) { 185 return (km->km_mech_type); 186 } 187 } 188 return (GSS_C_NO_OID); 189 } 190 191 const char * 192 kgss_find_mech_by_oid(const gss_OID oid) 193 { 194 struct kgss_mech *km; 195 196 LIST_FOREACH(km, &kgss_mechs, km_link) { 197 if (kgss_oid_equal(km->km_mech_type, oid)) { 198 return (km->km_mech_name); 199 } 200 } 201 return (NULL); 202 } 203 204 gss_ctx_id_t 205 kgss_create_context(gss_OID mech_type) 206 { 207 struct kgss_mech *km; 208 gss_ctx_id_t ctx; 209 210 LIST_FOREACH(km, &kgss_mechs, km_link) { 211 if (kgss_oid_equal(km->km_mech_type, mech_type)) 212 break; 213 } 214 if (!km) 215 return (NULL); 216 217 ctx = (gss_ctx_id_t) kobj_create(km->km_class, M_GSSAPI, M_WAITOK); 218 KGSS_INIT(ctx); 219 220 return (ctx); 221 } 222 223 void 224 kgss_delete_context(gss_ctx_id_t ctx, gss_buffer_t output_token) 225 { 226 227 KGSS_DELETE(ctx, output_token); 228 kobj_delete((kobj_t) ctx, M_GSSAPI); 229 } 230 231 OM_uint32 232 kgss_transfer_context(gss_ctx_id_t ctx) 233 { 234 struct export_sec_context_res res; 235 struct export_sec_context_args args; 236 enum clnt_stat stat; 237 OM_uint32 maj_stat; 238 239 if (!kgss_gssd_handle) 240 return (GSS_S_FAILURE); 241 242 args.ctx = ctx->handle; 243 bzero(&res, sizeof(res)); 244 stat = gssd_export_sec_context_1(&args, &res, kgss_gssd_handle); 245 if (stat != RPC_SUCCESS) { 246 return (GSS_S_FAILURE); 247 } 248 249 maj_stat = KGSS_IMPORT(ctx, res.format, &res.interprocess_token); 250 ctx->handle = 0; 251 252 xdr_free((xdrproc_t) xdr_export_sec_context_res, &res); 253 254 return (maj_stat); 255 } 256 257 void 258 kgss_copy_buffer(const gss_buffer_t from, gss_buffer_t to) 259 { 260 to->length = from->length; 261 if (from->length) { 262 to->value = malloc(from->length, M_GSSAPI, M_WAITOK); 263 bcopy(from->value, to->value, from->length); 264 } else { 265 to->value = NULL; 266 } 267 } 268 269 /* 270 * Acquire the kgss_gssd_handle and return it with a reference count, 271 * if it is available. 272 */ 273 CLIENT * 274 kgss_gssd_client(void) 275 { 276 CLIENT *cl; 277 278 mtx_lock(&kgss_gssd_lock); 279 cl = kgss_gssd_handle; 280 if (cl != NULL) 281 CLNT_ACQUIRE(cl); 282 mtx_unlock(&kgss_gssd_lock); 283 return (cl); 284 } 285 286 /* 287 * Kernel module glue 288 */ 289 static int 290 kgssapi_modevent(module_t mod, int type, void *data) 291 { 292 int error = 0; 293 294 switch (type) { 295 case MOD_LOAD: 296 rpc_gss_entries.rpc_gss_refresh_auth = rpc_gss_refresh_auth; 297 rpc_gss_entries.rpc_gss_secfind = rpc_gss_secfind; 298 rpc_gss_entries.rpc_gss_secpurge = rpc_gss_secpurge; 299 rpc_gss_entries.rpc_gss_seccreate = rpc_gss_seccreate; 300 rpc_gss_entries.rpc_gss_set_defaults = rpc_gss_set_defaults; 301 rpc_gss_entries.rpc_gss_max_data_length = 302 rpc_gss_max_data_length; 303 rpc_gss_entries.rpc_gss_get_error = rpc_gss_get_error; 304 rpc_gss_entries.rpc_gss_mech_to_oid = rpc_gss_mech_to_oid; 305 rpc_gss_entries.rpc_gss_oid_to_mech = rpc_gss_oid_to_mech; 306 rpc_gss_entries.rpc_gss_qop_to_num = rpc_gss_qop_to_num; 307 rpc_gss_entries.rpc_gss_get_mechanisms = rpc_gss_get_mechanisms; 308 rpc_gss_entries.rpc_gss_get_versions = rpc_gss_get_versions; 309 rpc_gss_entries.rpc_gss_is_installed = rpc_gss_is_installed; 310 rpc_gss_entries.rpc_gss_set_svc_name = rpc_gss_set_svc_name; 311 rpc_gss_entries.rpc_gss_clear_svc_name = rpc_gss_clear_svc_name; 312 rpc_gss_entries.rpc_gss_getcred = rpc_gss_getcred; 313 rpc_gss_entries.rpc_gss_set_callback = rpc_gss_set_callback; 314 rpc_gss_entries.rpc_gss_clear_callback = rpc_gss_clear_callback; 315 rpc_gss_entries.rpc_gss_get_principal_name = 316 rpc_gss_get_principal_name; 317 rpc_gss_entries.rpc_gss_svc_max_data_length = 318 rpc_gss_svc_max_data_length; 319 mtx_init(&kgss_gssd_lock, "kgss_gssd_lock", NULL, MTX_DEF); 320 break; 321 case MOD_UNLOAD: 322 /* 323 * Unloading of the kgssapi module is not currently supported. 324 * If somebody wants this, we would need to keep track of 325 * currently executing threads and make sure the count is 0. 326 */ 327 /* FALLTHROUGH */ 328 default: 329 error = EOPNOTSUPP; 330 } 331 return (error); 332 } 333 static moduledata_t kgssapi_mod = { 334 "kgssapi", 335 kgssapi_modevent, 336 NULL, 337 }; 338 DECLARE_MODULE(kgssapi, kgssapi_mod, SI_SUB_VFS, SI_ORDER_ANY); 339 MODULE_DEPEND(kgssapi, krpc, 1, 1, 1); 340 MODULE_VERSION(kgssapi, 1); 341