1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2009, Sun Microsystems, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 9 * - Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * - Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * - Neither the name of Sun Microsystems, Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 /* 31 * Copyright (c) 1986-1991 by Sun Microsystems Inc. 32 */ 33 34 #ident "@(#)key_call.c 1.25 94/04/24 SMI" 35 /* 36 * key_call.c, Interface to keyserver 37 * 38 * setsecretkey(key) - set your secret key 39 * encryptsessionkey(agent, deskey) - encrypt a session key to talk to agent 40 * decryptsessionkey(agent, deskey) - decrypt ditto 41 * gendeskey(deskey) - generate a secure des key 42 */ 43 44 #include "namespace.h" 45 #include "reentrant.h" 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <unistd.h> 49 #include <errno.h> 50 #include <rpc/rpc.h> 51 #include <rpc/auth.h> 52 #include <rpc/auth_unix.h> 53 #include <rpc/key_prot.h> 54 #include <string.h> 55 #include <netconfig.h> 56 #include <sys/utsname.h> 57 #include <stdlib.h> 58 #include <signal.h> 59 #include <sys/wait.h> 60 #include <sys/fcntl.h> 61 #include "un-namespace.h" 62 #include "mt_misc.h" 63 64 65 #define KEY_TIMEOUT 5 /* per-try timeout in seconds */ 66 #define KEY_NRETRY 12 /* number of retries */ 67 68 #ifdef DEBUG 69 #define debug(msg) (void) fprintf(stderr, "%s\n", msg); 70 #else 71 #define debug(msg) 72 #endif /* DEBUG */ 73 74 /* 75 * Hack to allow the keyserver to use AUTH_DES (for authenticated 76 * NIS+ calls, for example). The only functions that get called 77 * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes. 78 * 79 * The approach is to have the keyserver fill in pointers to local 80 * implementations of these functions, and to call those in key_call(). 81 */ 82 83 cryptkeyres *(*__key_encryptsession_pk_LOCAL)(uid_t, void *arg) = 0; 84 cryptkeyres *(*__key_decryptsession_pk_LOCAL)(uid_t, void *arg) = 0; 85 des_block *(*__key_gendes_LOCAL)(uid_t, void *) = 0; 86 87 static int key_call( u_long, xdrproc_t, void *, xdrproc_t, void *); 88 89 int 90 key_setsecret(const char *secretkey) 91 { 92 keystatus status; 93 94 if (!key_call((u_long) KEY_SET, (xdrproc_t)xdr_keybuf, 95 (void *)secretkey, 96 (xdrproc_t)xdr_keystatus, &status)) { 97 return (-1); 98 } 99 if (status != KEY_SUCCESS) { 100 debug("set status is nonzero"); 101 return (-1); 102 } 103 return (0); 104 } 105 106 107 /* key_secretkey_is_set() returns 1 if the keyserver has a secret key 108 * stored for the caller's effective uid; it returns 0 otherwise 109 * 110 * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't 111 * be using it, because it allows them to get the user's secret key. 112 */ 113 114 int 115 key_secretkey_is_set(void) 116 { 117 struct key_netstres kres; 118 119 memset((void*)&kres, 0, sizeof (kres)); 120 if (key_call((u_long) KEY_NET_GET, (xdrproc_t)xdr_void, NULL, 121 (xdrproc_t)xdr_key_netstres, &kres) && 122 (kres.status == KEY_SUCCESS) && 123 (kres.key_netstres_u.knet.st_priv_key[0] != 0)) { 124 /* avoid leaving secret key in memory */ 125 memset(kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES); 126 return (1); 127 } 128 return (0); 129 } 130 131 int 132 key_encryptsession_pk(char *remotename, netobj *remotekey, des_block *deskey) 133 { 134 cryptkeyarg2 arg; 135 cryptkeyres res; 136 137 arg.remotename = remotename; 138 arg.remotekey = *remotekey; 139 arg.deskey = *deskey; 140 if (!key_call((u_long)KEY_ENCRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg, 141 (xdrproc_t)xdr_cryptkeyres, &res)) { 142 return (-1); 143 } 144 if (res.status != KEY_SUCCESS) { 145 debug("encrypt status is nonzero"); 146 return (-1); 147 } 148 *deskey = res.cryptkeyres_u.deskey; 149 return (0); 150 } 151 152 int 153 key_decryptsession_pk(char *remotename, netobj *remotekey, des_block *deskey) 154 { 155 cryptkeyarg2 arg; 156 cryptkeyres res; 157 158 arg.remotename = remotename; 159 arg.remotekey = *remotekey; 160 arg.deskey = *deskey; 161 if (!key_call((u_long)KEY_DECRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg, 162 (xdrproc_t)xdr_cryptkeyres, &res)) { 163 return (-1); 164 } 165 if (res.status != KEY_SUCCESS) { 166 debug("decrypt status is nonzero"); 167 return (-1); 168 } 169 *deskey = res.cryptkeyres_u.deskey; 170 return (0); 171 } 172 173 int 174 key_encryptsession(const char *remotename, des_block *deskey) 175 { 176 cryptkeyarg arg; 177 cryptkeyres res; 178 179 arg.remotename = (char *) remotename; 180 arg.deskey = *deskey; 181 if (!key_call((u_long)KEY_ENCRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg, 182 (xdrproc_t)xdr_cryptkeyres, &res)) { 183 return (-1); 184 } 185 if (res.status != KEY_SUCCESS) { 186 debug("encrypt status is nonzero"); 187 return (-1); 188 } 189 *deskey = res.cryptkeyres_u.deskey; 190 return (0); 191 } 192 193 int 194 key_decryptsession(const char *remotename, des_block *deskey) 195 { 196 cryptkeyarg arg; 197 cryptkeyres res; 198 199 arg.remotename = (char *) remotename; 200 arg.deskey = *deskey; 201 if (!key_call((u_long)KEY_DECRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg, 202 (xdrproc_t)xdr_cryptkeyres, &res)) { 203 return (-1); 204 } 205 if (res.status != KEY_SUCCESS) { 206 debug("decrypt status is nonzero"); 207 return (-1); 208 } 209 *deskey = res.cryptkeyres_u.deskey; 210 return (0); 211 } 212 213 int 214 key_gendes(des_block *key) 215 { 216 if (!key_call((u_long)KEY_GEN, (xdrproc_t)xdr_void, NULL, 217 (xdrproc_t)xdr_des_block, key)) { 218 return (-1); 219 } 220 return (0); 221 } 222 223 int 224 key_setnet(struct key_netstarg *arg) 225 { 226 keystatus status; 227 228 229 if (!key_call((u_long) KEY_NET_PUT, (xdrproc_t)xdr_key_netstarg, arg, 230 (xdrproc_t)xdr_keystatus, &status)){ 231 return (-1); 232 } 233 234 if (status != KEY_SUCCESS) { 235 debug("key_setnet status is nonzero"); 236 return (-1); 237 } 238 return (1); 239 } 240 241 242 int 243 key_get_conv(char *pkey, des_block *deskey) 244 { 245 cryptkeyres res; 246 247 if (!key_call((u_long) KEY_GET_CONV, (xdrproc_t)xdr_keybuf, pkey, 248 (xdrproc_t)xdr_cryptkeyres, &res)) { 249 return (-1); 250 } 251 if (res.status != KEY_SUCCESS) { 252 debug("get_conv status is nonzero"); 253 return (-1); 254 } 255 *deskey = res.cryptkeyres_u.deskey; 256 return (0); 257 } 258 259 struct key_call_private { 260 CLIENT *client; /* Client handle */ 261 pid_t pid; /* process-id at moment of creation */ 262 uid_t uid; /* user-id at last authorization */ 263 }; 264 static struct key_call_private *key_call_private_main = NULL; 265 static thread_key_t key_call_key; 266 static once_t key_call_once = ONCE_INITIALIZER; 267 static int key_call_key_error; 268 269 static void 270 key_call_destroy(void *vp) 271 { 272 struct key_call_private *kcp = (struct key_call_private *)vp; 273 274 if (kcp) { 275 if (kcp->client) 276 clnt_destroy(kcp->client); 277 free(kcp); 278 } 279 } 280 281 static void 282 key_call_init(void) 283 { 284 285 key_call_key_error = thr_keycreate(&key_call_key, key_call_destroy); 286 } 287 288 /* 289 * Keep the handle cached. This call may be made quite often. 290 */ 291 static CLIENT * 292 getkeyserv_handle(int vers) 293 { 294 void *localhandle; 295 struct netconfig *nconf; 296 struct netconfig *tpconf; 297 struct key_call_private *kcp; 298 struct timeval wait_time; 299 struct utsname u; 300 int main_thread; 301 int fd; 302 303 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */ 304 #define TOTAL_TRIES 5 /* Number of tries */ 305 306 if ((main_thread = thr_main())) { 307 kcp = key_call_private_main; 308 } else { 309 if (thr_once(&key_call_once, key_call_init) != 0 || 310 key_call_key_error != 0) 311 return ((CLIENT *) NULL); 312 kcp = (struct key_call_private *)thr_getspecific(key_call_key); 313 } 314 if (kcp == (struct key_call_private *)NULL) { 315 kcp = (struct key_call_private *)malloc(sizeof (*kcp)); 316 if (kcp == (struct key_call_private *)NULL) { 317 return ((CLIENT *) NULL); 318 } 319 if (main_thread) 320 key_call_private_main = kcp; 321 else 322 thr_setspecific(key_call_key, (void *) kcp); 323 kcp->client = NULL; 324 } 325 326 /* if pid has changed, destroy client and rebuild */ 327 if (kcp->client != NULL && kcp->pid != getpid()) { 328 clnt_destroy(kcp->client); 329 kcp->client = NULL; 330 } 331 332 if (kcp->client != NULL) { 333 /* if uid has changed, build client handle again */ 334 if (kcp->uid != geteuid()) { 335 kcp->uid = geteuid(); 336 auth_destroy(kcp->client->cl_auth); 337 kcp->client->cl_auth = 338 authsys_create("", kcp->uid, 0, 0, NULL); 339 if (kcp->client->cl_auth == NULL) { 340 clnt_destroy(kcp->client); 341 kcp->client = NULL; 342 return ((CLIENT *) NULL); 343 } 344 } 345 /* Change the version number to the new one */ 346 clnt_control(kcp->client, CLSET_VERS, (void *)&vers); 347 return (kcp->client); 348 } 349 if (!(localhandle = setnetconfig())) { 350 return ((CLIENT *) NULL); 351 } 352 tpconf = NULL; 353 #if defined(__FreeBSD__) 354 if (uname(&u) == -1) 355 #else 356 #if defined(i386) 357 if (_nuname(&u) == -1) 358 #elif defined(sparc) 359 if (_uname(&u) == -1) 360 #else 361 #error Unknown architecture! 362 #endif 363 #endif 364 { 365 endnetconfig(localhandle); 366 return ((CLIENT *) NULL); 367 } 368 while ((nconf = getnetconfig(localhandle)) != NULL) { 369 if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) { 370 /* 371 * We use COTS_ORD here so that the caller can 372 * find out immediately if the server is dead. 373 */ 374 if (nconf->nc_semantics == NC_TPI_COTS_ORD) { 375 kcp->client = clnt_tp_create(u.nodename, 376 KEY_PROG, vers, nconf); 377 if (kcp->client) 378 break; 379 } else { 380 tpconf = nconf; 381 } 382 } 383 } 384 if ((kcp->client == (CLIENT *) NULL) && (tpconf)) 385 /* Now, try the CLTS or COTS loopback transport */ 386 kcp->client = clnt_tp_create(u.nodename, 387 KEY_PROG, vers, tpconf); 388 endnetconfig(localhandle); 389 390 if (kcp->client == (CLIENT *) NULL) { 391 return ((CLIENT *) NULL); 392 } 393 kcp->uid = geteuid(); 394 kcp->pid = getpid(); 395 kcp->client->cl_auth = authsys_create("", kcp->uid, 0, 0, NULL); 396 if (kcp->client->cl_auth == NULL) { 397 clnt_destroy(kcp->client); 398 kcp->client = NULL; 399 return ((CLIENT *) NULL); 400 } 401 402 wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES; 403 wait_time.tv_usec = 0; 404 (void) clnt_control(kcp->client, CLSET_RETRY_TIMEOUT, 405 (char *)&wait_time); 406 if (clnt_control(kcp->client, CLGET_FD, (char *)&fd)) 407 _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */ 408 409 return (kcp->client); 410 } 411 412 /* returns 0 on failure, 1 on success */ 413 414 static int 415 key_call(u_long proc, xdrproc_t xdr_arg, void *arg, xdrproc_t xdr_rslt, 416 void *rslt) 417 { 418 CLIENT *clnt; 419 struct timeval wait_time; 420 421 if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL) { 422 cryptkeyres *res; 423 res = (*__key_encryptsession_pk_LOCAL)(geteuid(), arg); 424 *(cryptkeyres*)rslt = *res; 425 return (1); 426 } else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL) { 427 cryptkeyres *res; 428 res = (*__key_decryptsession_pk_LOCAL)(geteuid(), arg); 429 *(cryptkeyres*)rslt = *res; 430 return (1); 431 } else if (proc == KEY_GEN && __key_gendes_LOCAL) { 432 des_block *res; 433 res = (*__key_gendes_LOCAL)(geteuid(), 0); 434 *(des_block*)rslt = *res; 435 return (1); 436 } 437 438 if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) || 439 (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) || 440 (proc == KEY_GET_CONV)) 441 clnt = getkeyserv_handle(2); /* talk to version 2 */ 442 else 443 clnt = getkeyserv_handle(1); /* talk to version 1 */ 444 445 if (clnt == NULL) { 446 return (0); 447 } 448 449 wait_time.tv_sec = TOTAL_TIMEOUT; 450 wait_time.tv_usec = 0; 451 452 if (clnt_call(clnt, proc, xdr_arg, arg, xdr_rslt, rslt, 453 wait_time) == RPC_SUCCESS) { 454 return (1); 455 } else { 456 return (0); 457 } 458 } 459