key_call.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * Copyright (c) 2010, Oracle America, Inc.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following
  12. * disclaimer in the documentation and/or other materials
  13. * provided with the distribution.
  14. * * Neither the name of the "Oracle America, 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
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  27. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * The original source is from the RPCSRC 4.0 package from Sun Microsystems.
  33. * The Interface to keyserver protocoll 2, RPC over AF_UNIX and Linux/doors
  34. * was added by Thorsten Kukuk <kukuk@suse.de>
  35. * Since the Linux/doors project was stopped, I doubt that this code will
  36. * ever be useful <kukuk@suse.de>.
  37. */
  38. #include <stdio.h>
  39. #include <errno.h>
  40. #include <fcntl.h>
  41. #include <signal.h>
  42. #include <unistd.h>
  43. #include <string.h>
  44. #include <rpc/rpc.h>
  45. #include <rpc/auth.h>
  46. #include <sys/wait.h>
  47. #include <sys/param.h>
  48. #include <sys/socket.h>
  49. #include <rpc/key_prot.h>
  50. #include <libc-lock.h>
  51. #include <shlib-compat.h>
  52. #define KEY_TIMEOUT 5 /* per-try timeout in seconds */
  53. #define KEY_NRETRY 12 /* number of retries */
  54. #define debug(msg) /* turn off debugging */
  55. #ifndef SO_PASSCRED
  56. extern int _openchild (const char *command, FILE **fto, FILE **ffrom);
  57. #endif
  58. static int key_call (u_long, xdrproc_t xdr_arg, char *,
  59. xdrproc_t xdr_rslt, char *);
  60. static const struct timeval trytimeout = {KEY_TIMEOUT, 0};
  61. static const struct timeval tottimeout = {KEY_TIMEOUT *KEY_NRETRY, 0};
  62. int
  63. key_setsecret (char *secretkey)
  64. {
  65. keystatus status;
  66. if (!key_call ((u_long) KEY_SET, (xdrproc_t) xdr_keybuf, secretkey,
  67. (xdrproc_t) xdr_keystatus, (char *) &status))
  68. return -1;
  69. if (status != KEY_SUCCESS)
  70. {
  71. debug ("set status is nonzero");
  72. return -1;
  73. }
  74. return 0;
  75. }
  76. libc_hidden_nolink_sunrpc (key_setsecret, GLIBC_2_1)
  77. /* key_secretkey_is_set() returns 1 if the keyserver has a secret key
  78. * stored for the caller's effective uid; it returns 0 otherwise
  79. *
  80. * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't
  81. * be using it, because it allows them to get the user's secret key.
  82. */
  83. int
  84. key_secretkey_is_set (void)
  85. {
  86. struct key_netstres kres;
  87. memset (&kres, 0, sizeof (kres));
  88. if (key_call ((u_long) KEY_NET_GET, (xdrproc_t) xdr_void,
  89. (char *) NULL, (xdrproc_t) xdr_key_netstres,
  90. (char *) &kres) &&
  91. (kres.status == KEY_SUCCESS) &&
  92. (kres.key_netstres_u.knet.st_priv_key[0] != 0))
  93. {
  94. /* avoid leaving secret key in memory */
  95. memset (kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES);
  96. return 1;
  97. }
  98. return 0;
  99. }
  100. #ifdef EXPORT_RPC_SYMBOLS
  101. libc_hidden_def (key_secretkey_is_set)
  102. #else
  103. libc_hidden_nolink_sunrpc (key_secretkey_is_set, GLIBC_2_1)
  104. #endif
  105. int
  106. key_encryptsession (char *remotename, des_block *deskey)
  107. {
  108. cryptkeyarg arg;
  109. cryptkeyres res;
  110. arg.remotename = remotename;
  111. arg.deskey = *deskey;
  112. if (!key_call ((u_long) KEY_ENCRYPT, (xdrproc_t) xdr_cryptkeyarg,
  113. (char *) &arg, (xdrproc_t) xdr_cryptkeyres,
  114. (char *) &res))
  115. return -1;
  116. if (res.status != KEY_SUCCESS)
  117. {
  118. debug ("encrypt status is nonzero");
  119. return -1;
  120. }
  121. *deskey = res.cryptkeyres_u.deskey;
  122. return 0;
  123. }
  124. libc_hidden_nolink_sunrpc (key_encryptsession, GLIBC_2_1)
  125. int
  126. key_decryptsession (char *remotename, des_block *deskey)
  127. {
  128. cryptkeyarg arg;
  129. cryptkeyres res;
  130. arg.remotename = remotename;
  131. arg.deskey = *deskey;
  132. if (!key_call ((u_long) KEY_DECRYPT, (xdrproc_t) xdr_cryptkeyarg,
  133. (char *) &arg, (xdrproc_t) xdr_cryptkeyres,
  134. (char *) &res))
  135. return -1;
  136. if (res.status != KEY_SUCCESS)
  137. {
  138. debug ("decrypt status is nonzero");
  139. return -1;
  140. }
  141. *deskey = res.cryptkeyres_u.deskey;
  142. return 0;
  143. }
  144. libc_hidden_nolink_sunrpc (key_decryptsession, GLIBC_2_1)
  145. int
  146. key_encryptsession_pk (char *remotename, netobj *remotekey,
  147. des_block *deskey)
  148. {
  149. cryptkeyarg2 arg;
  150. cryptkeyres res;
  151. arg.remotename = remotename;
  152. arg.remotekey = *remotekey;
  153. arg.deskey = *deskey;
  154. if (!key_call ((u_long) KEY_ENCRYPT_PK, (xdrproc_t) xdr_cryptkeyarg2,
  155. (char *) &arg, (xdrproc_t) xdr_cryptkeyres,
  156. (char *) &res))
  157. return -1;
  158. if (res.status != KEY_SUCCESS)
  159. {
  160. debug ("encrypt status is nonzero");
  161. return -1;
  162. }
  163. *deskey = res.cryptkeyres_u.deskey;
  164. return 0;
  165. }
  166. libc_hidden_nolink_sunrpc (key_encryptsession_pk, GLIBC_2_1)
  167. int
  168. key_decryptsession_pk (char *remotename, netobj *remotekey,
  169. des_block *deskey)
  170. {
  171. cryptkeyarg2 arg;
  172. cryptkeyres res;
  173. arg.remotename = remotename;
  174. arg.remotekey = *remotekey;
  175. arg.deskey = *deskey;
  176. if (!key_call ((u_long) KEY_DECRYPT_PK, (xdrproc_t) xdr_cryptkeyarg2,
  177. (char *) &arg, (xdrproc_t) xdr_cryptkeyres,
  178. (char *) &res))
  179. return -1;
  180. if (res.status != KEY_SUCCESS)
  181. {
  182. debug ("decrypt status is nonzero");
  183. return -1;
  184. }
  185. *deskey = res.cryptkeyres_u.deskey;
  186. return 0;
  187. }
  188. libc_hidden_nolink_sunrpc (key_decryptsession_pk, GLIBC_2_1)
  189. int
  190. key_gendes (des_block *key)
  191. {
  192. struct sockaddr_in sin;
  193. CLIENT *client;
  194. int socket;
  195. enum clnt_stat stat;
  196. sin.sin_family = AF_INET;
  197. sin.sin_port = 0;
  198. sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  199. memset (sin.sin_zero, 0, sizeof (sin.sin_zero));
  200. socket = RPC_ANYSOCK;
  201. client = clntudp_bufcreate (&sin, (u_long) KEY_PROG, (u_long) KEY_VERS,
  202. trytimeout, &socket, RPCSMALLMSGSIZE,
  203. RPCSMALLMSGSIZE);
  204. if (client == NULL)
  205. return -1;
  206. stat = clnt_call (client, KEY_GEN, (xdrproc_t) xdr_void, NULL,
  207. (xdrproc_t) xdr_des_block, (caddr_t) key,
  208. tottimeout);
  209. clnt_destroy (client);
  210. __close (socket);
  211. if (stat != RPC_SUCCESS)
  212. return -1;
  213. return 0;
  214. }
  215. #ifdef EXPORT_RPC_SYMBOLS
  216. libc_hidden_def (key_gendes)
  217. #else
  218. libc_hidden_nolink_sunrpc (key_gendes, GLIBC_2_1)
  219. #endif
  220. int
  221. key_setnet (struct key_netstarg *arg)
  222. {
  223. keystatus status;
  224. if (!key_call ((u_long) KEY_NET_PUT, (xdrproc_t) xdr_key_netstarg,
  225. (char *) arg,(xdrproc_t) xdr_keystatus,
  226. (char *) &status))
  227. return -1;
  228. if (status != KEY_SUCCESS)
  229. {
  230. debug ("key_setnet status is nonzero");
  231. return -1;
  232. }
  233. return 1;
  234. }
  235. libc_hidden_nolink_sunrpc (key_setnet, GLIBC_2_1)
  236. int
  237. key_get_conv (char *pkey, des_block *deskey)
  238. {
  239. cryptkeyres res;
  240. if (!key_call ((u_long) KEY_GET_CONV, (xdrproc_t) xdr_keybuf, pkey,
  241. (xdrproc_t) xdr_cryptkeyres, (char *) &res))
  242. return -1;
  243. if (res.status != KEY_SUCCESS)
  244. {
  245. debug ("get_conv status is nonzero");
  246. return -1;
  247. }
  248. *deskey = res.cryptkeyres_u.deskey;
  249. return 0;
  250. }
  251. libc_hidden_nolink_sunrpc (key_get_conv, GLIBC_2_1)
  252. /*
  253. * Hack to allow the keyserver to use AUTH_DES (for authenticated
  254. * NIS+ calls, for example). The only functions that get called
  255. * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes.
  256. *
  257. * The approach is to have the keyserver fill in pointers to local
  258. * implementations of these functions, and to call those in key_call().
  259. */
  260. cryptkeyres *(*__key_encryptsession_pk_LOCAL) (uid_t, char *);
  261. cryptkeyres *(*__key_decryptsession_pk_LOCAL) (uid_t, char *);
  262. des_block *(*__key_gendes_LOCAL) (uid_t, char *);
  263. #ifndef SO_PASSCRED
  264. static int
  265. key_call_keyenvoy (u_long proc, xdrproc_t xdr_arg, char *arg,
  266. xdrproc_t xdr_rslt, char *rslt)
  267. {
  268. XDR xdrargs;
  269. XDR xdrrslt;
  270. FILE *fargs;
  271. FILE *frslt;
  272. sigset_t oldmask, mask;
  273. int status;
  274. int pid;
  275. int success;
  276. uid_t ruid;
  277. uid_t euid;
  278. static const char MESSENGER[] = "/usr/etc/keyenvoy";
  279. success = 1;
  280. sigemptyset (&mask);
  281. sigaddset (&mask, SIGCHLD);
  282. __sigprocmask (SIG_BLOCK, &mask, &oldmask);
  283. /*
  284. * We are going to exec a set-uid program which makes our effective uid
  285. * zero, and authenticates us with our real uid. We need to make the
  286. * effective uid be the real uid for the setuid program, and
  287. * the real uid be the effective uid so that we can change things back.
  288. */
  289. euid = __geteuid ();
  290. ruid = __getuid ();
  291. __setreuid (euid, ruid);
  292. pid = _openchild (MESSENGER, &fargs, &frslt);
  293. __setreuid (ruid, euid);
  294. if (pid < 0)
  295. {
  296. debug ("open_streams");
  297. __sigprocmask (SIG_SETMASK, &oldmask, NULL);
  298. return (0);
  299. }
  300. xdrstdio_create (&xdrargs, fargs, XDR_ENCODE);
  301. xdrstdio_create (&xdrrslt, frslt, XDR_DECODE);
  302. if (!xdr_u_long (&xdrargs, &proc) || !(*xdr_arg) (&xdrargs, arg))
  303. {
  304. debug ("xdr args");
  305. success = 0;
  306. }
  307. fclose (fargs);
  308. if (success && !(*xdr_rslt) (&xdrrslt, rslt))
  309. {
  310. debug ("xdr rslt");
  311. success = 0;
  312. }
  313. fclose(frslt);
  314. wait_again:
  315. if (__wait4 (pid, &status, 0, NULL) < 0)
  316. {
  317. if (errno == EINTR)
  318. goto wait_again;
  319. debug ("wait4");
  320. if (errno == ECHILD || errno == ESRCH)
  321. perror ("wait");
  322. else
  323. success = 0;
  324. }
  325. else
  326. if (status != 0)
  327. {
  328. debug ("wait4 1");
  329. success = 0;
  330. }
  331. __sigprocmask (SIG_SETMASK, &oldmask, NULL);
  332. return success;
  333. }
  334. #endif
  335. struct key_call_private {
  336. CLIENT *client; /* Client handle */
  337. pid_t pid; /* process-id at moment of creation */
  338. uid_t uid; /* user-id at last authorization */
  339. };
  340. #define key_call_private_main RPC_THREAD_VARIABLE(key_call_private_s)
  341. __libc_lock_define_initialized (static, keycall_lock)
  342. /*
  343. * Keep the handle cached. This call may be made quite often.
  344. */
  345. static CLIENT *
  346. getkeyserv_handle (int vers)
  347. {
  348. struct key_call_private *kcp = key_call_private_main;
  349. struct timeval wait_time;
  350. int fd;
  351. struct sockaddr_un name;
  352. socklen_t namelen = sizeof(struct sockaddr_un);
  353. #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */
  354. #define TOTAL_TRIES 5 /* Number of tries */
  355. if (kcp == (struct key_call_private *)NULL)
  356. {
  357. kcp = (struct key_call_private *)malloc (sizeof (*kcp));
  358. if (kcp == (struct key_call_private *)NULL)
  359. return (CLIENT *) NULL;
  360. key_call_private_main = kcp;
  361. kcp->client = NULL;
  362. }
  363. /* if pid has changed, destroy client and rebuild */
  364. if (kcp->client != NULL && kcp->pid != __getpid ())
  365. {
  366. auth_destroy (kcp->client->cl_auth);
  367. clnt_destroy (kcp->client);
  368. kcp->client = NULL;
  369. }
  370. if (kcp->client != NULL)
  371. {
  372. /* if other side closed socket, build handle again */
  373. clnt_control (kcp->client, CLGET_FD, (char *)&fd);
  374. if (__getpeername (fd,(struct sockaddr *)&name,&namelen) == -1)
  375. {
  376. auth_destroy (kcp->client->cl_auth);
  377. clnt_destroy (kcp->client);
  378. kcp->client = NULL;
  379. }
  380. }
  381. if (kcp->client != NULL)
  382. {
  383. /* if uid has changed, build client handle again */
  384. if (kcp->uid != __geteuid ())
  385. {
  386. kcp->uid = __geteuid ();
  387. auth_destroy (kcp->client->cl_auth);
  388. kcp->client->cl_auth =
  389. authunix_create ((char *)"", kcp->uid, 0, 0, NULL);
  390. if (kcp->client->cl_auth == NULL)
  391. {
  392. clnt_destroy (kcp->client);
  393. kcp->client = NULL;
  394. return ((CLIENT *) NULL);
  395. }
  396. }
  397. /* Change the version number to the new one */
  398. clnt_control (kcp->client, CLSET_VERS, (void *)&vers);
  399. return kcp->client;
  400. }
  401. if ((kcp->client == (CLIENT *) NULL))
  402. /* Use the AF_UNIX transport */
  403. kcp->client = clnt_create ("/var/run/keyservsock", KEY_PROG, vers, "unix");
  404. if (kcp->client == (CLIENT *) NULL)
  405. return (CLIENT *) NULL;
  406. kcp->uid = __geteuid ();
  407. kcp->pid = __getpid ();
  408. kcp->client->cl_auth = authunix_create ((char *)"", kcp->uid, 0, 0, NULL);
  409. if (kcp->client->cl_auth == NULL)
  410. {
  411. clnt_destroy (kcp->client);
  412. kcp->client = NULL;
  413. return (CLIENT *) NULL;
  414. }
  415. wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
  416. wait_time.tv_usec = 0;
  417. clnt_control (kcp->client, CLSET_RETRY_TIMEOUT,
  418. (char *)&wait_time);
  419. if (clnt_control (kcp->client, CLGET_FD, (char *)&fd))
  420. __fcntl (fd, F_SETFD, FD_CLOEXEC); /* make it "close on exec" */
  421. return kcp->client;
  422. }
  423. /* returns 0 on failure, 1 on success */
  424. static int
  425. key_call_socket (u_long proc, xdrproc_t xdr_arg, char *arg,
  426. xdrproc_t xdr_rslt, char *rslt)
  427. {
  428. CLIENT *clnt;
  429. struct timeval wait_time;
  430. int result = 0;
  431. __libc_lock_lock (keycall_lock);
  432. if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) ||
  433. (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) ||
  434. (proc == KEY_GET_CONV))
  435. clnt = getkeyserv_handle(2); /* talk to version 2 */
  436. else
  437. clnt = getkeyserv_handle(1); /* talk to version 1 */
  438. if (clnt != NULL)
  439. {
  440. wait_time.tv_sec = TOTAL_TIMEOUT;
  441. wait_time.tv_usec = 0;
  442. if (clnt_call (clnt, proc, xdr_arg, arg, xdr_rslt, rslt,
  443. wait_time) == RPC_SUCCESS)
  444. result = 1;
  445. }
  446. __libc_lock_unlock (keycall_lock);
  447. return result;
  448. }
  449. /* returns 0 on failure, 1 on success */
  450. static int
  451. key_call (u_long proc, xdrproc_t xdr_arg, char *arg,
  452. xdrproc_t xdr_rslt, char *rslt)
  453. {
  454. #ifndef SO_PASSCRED
  455. static int use_keyenvoy;
  456. #endif
  457. if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL)
  458. {
  459. cryptkeyres *res;
  460. res = (*__key_encryptsession_pk_LOCAL) (__geteuid (), arg);
  461. *(cryptkeyres *) rslt = *res;
  462. return 1;
  463. }
  464. else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL)
  465. {
  466. cryptkeyres *res;
  467. res = (*__key_decryptsession_pk_LOCAL) (__geteuid (), arg);
  468. *(cryptkeyres *) rslt = *res;
  469. return 1;
  470. }
  471. else if (proc == KEY_GEN && __key_gendes_LOCAL)
  472. {
  473. des_block *res;
  474. res = (*__key_gendes_LOCAL) (__geteuid (), 0);
  475. *(des_block *) rslt = *res;
  476. return 1;
  477. }
  478. #ifdef SO_PASSCRED
  479. return key_call_socket (proc, xdr_arg, arg, xdr_rslt, rslt);
  480. #else
  481. if (!use_keyenvoy)
  482. {
  483. if (key_call_socket (proc, xdr_arg, arg, xdr_rslt, rslt))
  484. return 1;
  485. use_keyenvoy = 1;
  486. }
  487. return key_call_keyenvoy (proc, xdr_arg, arg, xdr_rslt, rslt);
  488. #endif
  489. }
  490. void
  491. __rpc_thread_key_cleanup (void)
  492. {
  493. struct key_call_private *kcp = RPC_THREAD_VARIABLE(key_call_private_s);
  494. if (kcp) {
  495. if (kcp->client) {
  496. if (kcp->client->cl_auth)
  497. auth_destroy (kcp->client->cl_auth);
  498. clnt_destroy(kcp->client);
  499. }
  500. free (kcp);
  501. }
  502. }