svcauth_des.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. * svcauth_des.c, server-side des authentication
  32. *
  33. * We insure for the service the following:
  34. * (1) The timestamp microseconds do not exceed 1 million.
  35. * (2) The timestamp plus the window is less than the current time.
  36. * (3) The timestamp is not less than the one previously
  37. * seen in the current session.
  38. *
  39. * It is up to the server to determine if the window size is
  40. * too small .
  41. *
  42. */
  43. #include <limits.h>
  44. #include <string.h>
  45. #include <stdint.h>
  46. #include <sys/param.h>
  47. #include <netinet/in.h>
  48. #include <rpc/rpc.h>
  49. #include <rpc/xdr.h>
  50. #include <rpc/auth.h>
  51. #include <rpc/auth_des.h>
  52. #include <rpc/svc_auth.h>
  53. #include <rpc/svc.h>
  54. #include <rpc/des_crypt.h>
  55. #include <shlib-compat.h>
  56. #define debug(msg) /*printf("svcauth_des: %s\n", msg) */
  57. #define USEC_PER_SEC ((uint32_t) 1000000L)
  58. #define BEFORE(t1, t2) timercmp(t1, t2, <)
  59. /*
  60. * LRU cache of conversation keys and some other useful items.
  61. */
  62. #define AUTHDES_CACHESZ 64
  63. struct cache_entry
  64. {
  65. des_block key; /* conversation key */
  66. char *rname; /* client's name */
  67. u_int window; /* credential lifetime window */
  68. struct rpc_timeval laststamp; /* detect replays of creds */
  69. char *localcred; /* generic local credential */
  70. };
  71. #define authdes_cache RPC_THREAD_VARIABLE(authdes_cache_s)
  72. #define authdes_lru RPC_THREAD_VARIABLE(authdes_lru_s)
  73. static void cache_init (void); /* initialize the cache */
  74. static short cache_spot (des_block *, char *, struct rpc_timeval *);
  75. /* find an entry in the cache */
  76. static void cache_ref (uint32_t sid); /* note that sid was ref'd */
  77. static void invalidate (char *cred); /* invalidate entry in cache */
  78. /* Cache statistics. Accidental historic export without a matching
  79. declaration in any header file. */
  80. #ifndef SHARED
  81. static
  82. #endif
  83. struct
  84. {
  85. u_long ncachehits; /* times cache hit, and is not replay */
  86. u_long ncachereplays; /* times cache hit, and is replay */
  87. u_long ncachemisses; /* times cache missed */
  88. }
  89. svcauthdes_stats __attribute__ ((nocommon));
  90. #ifdef SHARED
  91. compat_symbol (libc, svcauthdes_stats, svcauthdes_stats, GLIBC_2_0);
  92. #endif
  93. /*
  94. * Service side authenticator for AUTH_DES
  95. */
  96. enum auth_stat
  97. _svcauth_des (register struct svc_req *rqst, register struct rpc_msg *msg)
  98. {
  99. register uint32_t *ixdr;
  100. des_block cryptbuf[2];
  101. register struct authdes_cred *cred;
  102. struct authdes_verf verf;
  103. int status;
  104. register struct cache_entry *entry;
  105. uint32_t sid = 0;
  106. des_block *sessionkey;
  107. des_block ivec;
  108. u_int window;
  109. struct rpc_timeval timestamp;
  110. uint32_t namelen;
  111. struct area
  112. {
  113. struct authdes_cred area_cred;
  114. char area_netname[MAXNETNAMELEN + 1];
  115. }
  116. *area;
  117. if (authdes_cache == NULL)
  118. cache_init ();
  119. if (authdes_cache == NULL) /* No free memory */
  120. return AUTH_FAILED;
  121. area = (struct area *) rqst->rq_clntcred;
  122. cred = (struct authdes_cred *) &area->area_cred;
  123. /*
  124. * Get the credential
  125. */
  126. if (msg->rm_call.cb_cred.oa_length <= 0 ||
  127. msg->rm_call.cb_cred.oa_length > MAX_AUTH_BYTES)
  128. return AUTH_BADCRED;
  129. ixdr = (uint32_t *) msg->rm_call.cb_cred.oa_base;
  130. cred->adc_namekind = IXDR_GET_ENUM (ixdr, enum authdes_namekind);
  131. switch (cred->adc_namekind)
  132. {
  133. case ADN_FULLNAME:
  134. namelen = IXDR_GET_U_INT32 (ixdr);
  135. if (namelen > MAXNETNAMELEN)
  136. {
  137. return AUTH_BADCRED;
  138. }
  139. cred->adc_fullname.name = area->area_netname;
  140. memcpy (cred->adc_fullname.name, (char *) ixdr, namelen);
  141. cred->adc_fullname.name[namelen] = 0;
  142. ixdr += (RNDUP (namelen) / BYTES_PER_XDR_UNIT);
  143. cred->adc_fullname.key.key.high = *ixdr++;
  144. cred->adc_fullname.key.key.low = *ixdr++;
  145. cred->adc_fullname.window = *ixdr++;
  146. break;
  147. case ADN_NICKNAME:
  148. cred->adc_nickname = *ixdr++;
  149. break;
  150. default:
  151. return AUTH_BADCRED;
  152. }
  153. /*
  154. * Get the verifier
  155. */
  156. if (msg->rm_call.cb_verf.oa_length <= 0 ||
  157. msg->rm_call.cb_verf.oa_length > MAX_AUTH_BYTES)
  158. return AUTH_BADCRED;
  159. ixdr = (uint32_t *) msg->rm_call.cb_verf.oa_base;
  160. verf.adv_xtimestamp.key.high = *ixdr++;
  161. verf.adv_xtimestamp.key.low = *ixdr++;
  162. verf.adv_int_u = *ixdr++;
  163. /*
  164. * Get the conversation key
  165. */
  166. if (cred->adc_namekind == ADN_FULLNAME)
  167. {
  168. netobj pkey;
  169. char pkey_data[1024];
  170. sessionkey = &cred->adc_fullname.key;
  171. if (!getpublickey (cred->adc_fullname.name, pkey_data))
  172. {
  173. debug("getpublickey");
  174. return AUTH_BADCRED;
  175. }
  176. pkey.n_bytes = pkey_data;
  177. pkey.n_len = strlen (pkey_data) + 1;
  178. if (key_decryptsession_pk (cred->adc_fullname.name, &pkey,
  179. sessionkey) < 0)
  180. {
  181. debug ("decryptsessionkey");
  182. return AUTH_BADCRED; /* key not found */
  183. }
  184. }
  185. else
  186. { /* ADN_NICKNAME */
  187. if (cred->adc_nickname >= AUTHDES_CACHESZ)
  188. {
  189. debug ("bad nickname");
  190. return AUTH_BADCRED; /* garbled credential */
  191. }
  192. else
  193. sid = cred->adc_nickname;
  194. /* XXX This could be wrong, but else we have a
  195. security problem */
  196. if (authdes_cache[sid].rname == NULL)
  197. return AUTH_BADCRED;
  198. sessionkey = &authdes_cache[sid].key;
  199. }
  200. /*
  201. * Decrypt the timestamp
  202. */
  203. cryptbuf[0] = verf.adv_xtimestamp;
  204. if (cred->adc_namekind == ADN_FULLNAME)
  205. {
  206. cryptbuf[1].key.high = cred->adc_fullname.window;
  207. cryptbuf[1].key.low = verf.adv_winverf;
  208. ivec.key.high = ivec.key.low = 0;
  209. status = cbc_crypt ((char *) sessionkey, (char *) cryptbuf,
  210. 2 * sizeof (des_block), DES_DECRYPT | DES_HW,
  211. (char *) &ivec);
  212. }
  213. else
  214. status = ecb_crypt ((char *) sessionkey, (char *) cryptbuf,
  215. sizeof (des_block), DES_DECRYPT | DES_HW);
  216. if (DES_FAILED (status))
  217. {
  218. debug ("decryption failure");
  219. return AUTH_FAILED; /* system error */
  220. }
  221. /*
  222. * XDR the decrypted timestamp
  223. */
  224. ixdr = (uint32_t *) cryptbuf;
  225. timestamp.tv_sec = IXDR_GET_INT32 (ixdr);
  226. timestamp.tv_usec = IXDR_GET_INT32 (ixdr);
  227. /*
  228. * Check for valid credentials and verifiers.
  229. * They could be invalid because the key was flushed
  230. * out of the cache, and so a new session should begin.
  231. * Be sure and send AUTH_REJECTED{CRED, VERF} if this is the case.
  232. */
  233. {
  234. struct timeval current;
  235. int nick;
  236. u_int winverf;
  237. if (cred->adc_namekind == ADN_FULLNAME)
  238. {
  239. short tmp_spot;
  240. window = IXDR_GET_U_INT32 (ixdr);
  241. winverf = IXDR_GET_U_INT32 (ixdr);
  242. if (winverf != window - 1)
  243. {
  244. debug ("window verifier mismatch");
  245. return AUTH_BADCRED; /* garbled credential */
  246. }
  247. tmp_spot = cache_spot (sessionkey, cred->adc_fullname.name,
  248. &timestamp);
  249. if (tmp_spot < 0 || tmp_spot > AUTHDES_CACHESZ)
  250. {
  251. debug ("replayed credential");
  252. return AUTH_REJECTEDCRED; /* replay */
  253. }
  254. sid = tmp_spot;
  255. nick = 0;
  256. }
  257. else
  258. { /* ADN_NICKNAME */
  259. window = authdes_cache[sid].window;
  260. nick = 1;
  261. }
  262. if (timestamp.tv_usec >= USEC_PER_SEC)
  263. {
  264. debug ("invalid usecs");
  265. /* cached out (bad key), or garbled verifier */
  266. return nick ? AUTH_REJECTEDVERF : AUTH_BADVERF;
  267. }
  268. if (nick && BEFORE (&timestamp, &authdes_cache[sid].laststamp))
  269. {
  270. debug ("timestamp before last seen");
  271. return AUTH_REJECTEDVERF; /* replay */
  272. }
  273. __gettimeofday (&current, (struct timezone *) NULL);
  274. current.tv_sec -= window; /* allow for expiration */
  275. if (!BEFORE (&current, &timestamp))
  276. {
  277. debug ("timestamp expired");
  278. /* replay, or garbled credential */
  279. return nick ? AUTH_REJECTEDVERF : AUTH_BADCRED;
  280. }
  281. }
  282. /*
  283. * Set up the reply verifier
  284. */
  285. verf.adv_nickname = sid;
  286. /*
  287. * xdr the timestamp before encrypting
  288. */
  289. ixdr = (uint32_t *) cryptbuf;
  290. IXDR_PUT_INT32 (ixdr, timestamp.tv_sec - 1);
  291. IXDR_PUT_INT32 (ixdr, timestamp.tv_usec);
  292. /*
  293. * encrypt the timestamp
  294. */
  295. status = ecb_crypt ((char *) sessionkey, (char *) cryptbuf,
  296. sizeof (des_block), DES_ENCRYPT | DES_HW);
  297. if (DES_FAILED (status))
  298. {
  299. debug ("encryption failure");
  300. return AUTH_FAILED; /* system error */
  301. }
  302. verf.adv_xtimestamp = cryptbuf[0];
  303. /*
  304. * Serialize the reply verifier, and update rqst
  305. */
  306. ixdr = (uint32_t *) msg->rm_call.cb_verf.oa_base;
  307. *ixdr++ = verf.adv_xtimestamp.key.high;
  308. *ixdr++ = verf.adv_xtimestamp.key.low;
  309. *ixdr++ = verf.adv_int_u;
  310. rqst->rq_xprt->xp_verf.oa_flavor = AUTH_DES;
  311. rqst->rq_xprt->xp_verf.oa_base = msg->rm_call.cb_verf.oa_base;
  312. rqst->rq_xprt->xp_verf.oa_length =
  313. (char *) ixdr - msg->rm_call.cb_verf.oa_base;
  314. /*
  315. * We succeeded, commit the data to the cache now and
  316. * finish cooking the credential.
  317. */
  318. entry = &authdes_cache[sid];
  319. entry->laststamp = timestamp;
  320. cache_ref (sid);
  321. if (cred->adc_namekind == ADN_FULLNAME)
  322. {
  323. size_t full_len;
  324. cred->adc_fullname.window = window;
  325. cred->adc_nickname = sid; /* save nickname */
  326. if (entry->rname != NULL)
  327. mem_free (entry->rname, strlen (entry->rname) + 1);
  328. full_len = strlen (cred->adc_fullname.name) + 1;
  329. entry->rname = mem_alloc ((u_int) full_len);
  330. if (entry->rname != NULL)
  331. memcpy (entry->rname, cred->adc_fullname.name, full_len);
  332. else
  333. {
  334. debug ("out of memory");
  335. return AUTH_FAILED; /* out of memory is bad */
  336. }
  337. entry->key = *sessionkey;
  338. entry->window = window;
  339. invalidate (entry->localcred); /* mark any cached cred invalid */
  340. }
  341. else
  342. { /* ADN_NICKNAME */
  343. /*
  344. * nicknames are cooked into fullnames
  345. */
  346. cred->adc_namekind = ADN_FULLNAME;
  347. cred->adc_fullname.name = entry->rname;
  348. cred->adc_fullname.key = entry->key;
  349. cred->adc_fullname.window = entry->window;
  350. }
  351. return AUTH_OK; /* we made it! */
  352. }
  353. /*
  354. * Initialize the cache
  355. */
  356. static void
  357. cache_init (void)
  358. {
  359. register int i;
  360. authdes_cache = (struct cache_entry *)
  361. calloc (sizeof (struct cache_entry) * AUTHDES_CACHESZ, 1);
  362. if (authdes_cache == NULL)
  363. return;
  364. authdes_lru = (int *) mem_alloc (sizeof (int) * AUTHDES_CACHESZ);
  365. /*
  366. * Initialize the lru list
  367. */
  368. for (i = 0; i < AUTHDES_CACHESZ; ++i)
  369. authdes_lru[i] = i;
  370. }
  371. /*
  372. * Find the lru victim
  373. */
  374. static short
  375. cache_victim (void)
  376. {
  377. return authdes_lru[AUTHDES_CACHESZ - 1];
  378. }
  379. /*
  380. * Note that sid was referenced
  381. */
  382. static void
  383. cache_ref (register uint32_t sid)
  384. {
  385. register int i;
  386. register int curr;
  387. register int prev;
  388. prev = authdes_lru[0];
  389. authdes_lru[0] = sid;
  390. for (i = 1; prev != sid; ++i)
  391. {
  392. curr = authdes_lru[i];
  393. authdes_lru[i] = prev;
  394. prev = curr;
  395. }
  396. }
  397. /*
  398. * Find a spot in the cache for a credential containing
  399. * the items given. Return -1 if a replay is detected, otherwise
  400. * return the spot in the cache.
  401. */
  402. static short
  403. cache_spot (register des_block *key, char *name,
  404. struct rpc_timeval *timestamp)
  405. {
  406. register struct cache_entry *cp;
  407. register int i;
  408. register uint32_t hi;
  409. hi = key->key.high;
  410. for (cp = authdes_cache, i = 0; i < AUTHDES_CACHESZ; ++i, ++cp)
  411. {
  412. if (cp->key.key.high == hi &&
  413. cp->key.key.low == key->key.low &&
  414. cp->rname != NULL &&
  415. memcmp (cp->rname, name, strlen (name) + 1) == 0)
  416. {
  417. if (BEFORE (timestamp, &cp->laststamp))
  418. {
  419. ++svcauthdes_stats.ncachereplays;
  420. return -1; /* replay */
  421. }
  422. ++svcauthdes_stats.ncachehits;
  423. return i; /* refresh */
  424. }
  425. }
  426. ++svcauthdes_stats.ncachemisses;
  427. return cache_victim (); /* new credential */
  428. }
  429. /*
  430. * Local credential handling stuff.
  431. * NOTE: bsd unix dependent.
  432. * Other operating systems should put something else here.
  433. */
  434. #define UNKNOWN -2 /* grouplen, if cached cred is unknown user */
  435. #define INVALID -1 /* grouplen, if cache entry is invalid */
  436. struct bsdcred
  437. {
  438. uid_t uid; /* cached uid */
  439. gid_t gid; /* cached gid */
  440. int grouplen; /* length of cached groups */
  441. int grouplen_max; /* length of allocated cached groups */
  442. gid_t groups[0]; /* cached groups */
  443. };
  444. /*
  445. * Map a des credential into a unix cred.
  446. * We cache the credential here so the application does
  447. * not have to make an rpc call every time to interpret
  448. * the credential.
  449. */
  450. int
  451. authdes_getucred (const struct authdes_cred *adc, uid_t * uid, gid_t * gid,
  452. short *grouplen, gid_t * groups)
  453. {
  454. unsigned sid;
  455. register int i;
  456. uid_t i_uid;
  457. gid_t i_gid;
  458. int i_grouplen;
  459. struct bsdcred *cred;
  460. sid = adc->adc_nickname;
  461. if (sid >= AUTHDES_CACHESZ)
  462. {
  463. debug ("invalid nickname");
  464. return 0;
  465. }
  466. cred = (struct bsdcred *) authdes_cache[sid].localcred;
  467. if (cred == NULL || cred->grouplen == INVALID)
  468. {
  469. /*
  470. * not in cache: lookup
  471. */
  472. if (!netname2user (adc->adc_fullname.name, &i_uid, &i_gid,
  473. &i_grouplen, groups))
  474. {
  475. debug ("unknown netname");
  476. if (cred != NULL)
  477. cred->grouplen = UNKNOWN; /* mark as lookup up, but not found */
  478. return 0;
  479. }
  480. if (cred != NULL && cred->grouplen_max < i_grouplen)
  481. {
  482. /* We already have an allocated data structure. But it is
  483. too small. */
  484. free (cred);
  485. authdes_cache[sid].localcred = NULL;
  486. cred = NULL;
  487. }
  488. if (cred == NULL)
  489. {
  490. /* We should allocate room for at least NGROUPS groups. */
  491. int ngroups_max = MAX (i_grouplen, NGROUPS);
  492. cred = (struct bsdcred *) mem_alloc (sizeof (struct bsdcred)
  493. + ngroups_max * sizeof (gid_t));
  494. if (cred == NULL)
  495. return 0;
  496. authdes_cache[sid].localcred = (char *) cred;
  497. cred->grouplen = INVALID;
  498. cred->grouplen_max = ngroups_max;
  499. }
  500. debug ("missed ucred cache");
  501. *uid = cred->uid = i_uid;
  502. *gid = cred->gid = i_gid;
  503. cred->grouplen = i_grouplen;
  504. for (i = i_grouplen - 1; i >= 0; --i)
  505. cred->groups[i] = groups[i];
  506. /* Make sure no too large values are reported. */
  507. *grouplen = MIN (SHRT_MAX, i_grouplen);
  508. return 1;
  509. }
  510. else if (cred->grouplen == UNKNOWN)
  511. {
  512. /*
  513. * Already lookup up, but no match found
  514. */
  515. return 0;
  516. }
  517. /*
  518. * cached credentials
  519. */
  520. *uid = cred->uid;
  521. *gid = cred->gid;
  522. /* Another stupidity in the interface: *grouplen is of type short.
  523. So we might have to cut the information passed up short. */
  524. int grouplen_copy = MIN (SHRT_MAX, cred->grouplen);
  525. *grouplen = grouplen_copy;
  526. for (i = grouplen_copy - 1; i >= 0; --i)
  527. groups[i] = cred->groups[i];
  528. return 1;
  529. }
  530. libc_hidden_nolink_sunrpc (authdes_getucred, GLIBC_2_1)
  531. static void
  532. invalidate (char *cred)
  533. {
  534. if (cred == NULL)
  535. return;
  536. ((struct bsdcred *) cred)->grouplen = INVALID;
  537. }