2 * Copyright (c) 2010, Oracle America, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
31 * svcauth_des.c, server-side des authentication
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.
39 * It is up to the server to determine if the window size is
47 #include <sys/param.h>
48 #include <netinet/in.h>
52 #include <rpc/auth_des.h>
53 #include <rpc/svc_auth.h>
55 #include <rpc/des_crypt.h>
56 #include <shlib-compat.h>
58 #define debug(msg) /*printf("svcauth_des: %s\n", msg) */
60 #define USEC_PER_SEC ((uint32_t) 1000000L)
61 #define BEFORE(t1, t2) timercmp(t1, t2, <)
64 * LRU cache of conversation keys and some other useful items.
66 #define AUTHDES_CACHESZ 64
69 des_block key
; /* conversation key */
70 char *rname
; /* client's name */
71 u_int window
; /* credential lifetime window */
72 struct rpc_timeval laststamp
; /* detect replays of creds */
73 char *localcred
; /* generic local credential */
75 #ifdef _RPC_THREAD_SAFE_
76 #define authdes_cache RPC_THREAD_VARIABLE(authdes_cache_s)
77 #define authdes_lru RPC_THREAD_VARIABLE(authdes_lru_s)
79 static struct cache_entry
*authdes_cache
;
80 static int *authdes_lru
;
83 static void cache_init (void); /* initialize the cache */
84 static short cache_spot (des_block
*, char *, struct rpc_timeval
*);
85 /* find an entry in the cache */
86 static void cache_ref (uint32_t sid
); /* note that sid was ref'd */
88 static void invalidate (char *cred
); /* invalidate entry in cache */
95 u_long ncachehits
; /* times cache hit, and is not replay */
96 u_long ncachereplays
; /* times cache hit, and is replay */
97 u_long ncachemisses
; /* times cache missed */
102 * Service side authenticator for AUTH_DES
105 _svcauth_des (register struct svc_req
*rqst
, register struct rpc_msg
*msg
)
107 register uint32_t *ixdr
;
108 des_block cryptbuf
[2];
109 register struct authdes_cred
*cred
;
110 struct authdes_verf verf
;
112 register struct cache_entry
*entry
;
114 des_block
*sessionkey
;
117 struct rpc_timeval timestamp
;
121 struct authdes_cred area_cred
;
122 char area_netname
[MAXNETNAMELEN
+ 1];
126 if (authdes_cache
== NULL
)
128 if (authdes_cache
== NULL
) /* No free memory */
131 area
= (struct area
*) rqst
->rq_clntcred
;
132 cred
= (struct authdes_cred
*) &area
->area_cred
;
137 if (msg
->rm_call
.cb_cred
.oa_length
<= 0 ||
138 msg
->rm_call
.cb_cred
.oa_length
> MAX_AUTH_BYTES
)
141 ixdr
= (uint32_t *) msg
->rm_call
.cb_cred
.oa_base
;
142 cred
->adc_namekind
= IXDR_GET_ENUM (ixdr
, enum authdes_namekind
);
143 switch (cred
->adc_namekind
)
146 namelen
= IXDR_GET_U_INT32 (ixdr
);
147 if (namelen
> MAXNETNAMELEN
)
151 cred
->adc_fullname
.name
= area
->area_netname
;
152 memcpy (cred
->adc_fullname
.name
, (char *) ixdr
, namelen
);
153 cred
->adc_fullname
.name
[namelen
] = 0;
154 ixdr
+= (RNDUP (namelen
) / BYTES_PER_XDR_UNIT
);
155 cred
->adc_fullname
.key
.key
.high
= *ixdr
++;
156 cred
->adc_fullname
.key
.key
.low
= *ixdr
++;
157 cred
->adc_fullname
.window
= *ixdr
++;
160 cred
->adc_nickname
= *ixdr
++;
169 if (msg
->rm_call
.cb_verf
.oa_length
<= 0 ||
170 msg
->rm_call
.cb_verf
.oa_length
> MAX_AUTH_BYTES
)
173 ixdr
= (uint32_t *) msg
->rm_call
.cb_verf
.oa_base
;
174 verf
.adv_xtimestamp
.key
.high
= *ixdr
++;
175 verf
.adv_xtimestamp
.key
.low
= *ixdr
++;
176 verf
.adv_int_u
= *ixdr
++;
179 * Get the conversation key
181 if (cred
->adc_namekind
== ADN_FULLNAME
)
184 char pkey_data
[1024];
186 sessionkey
= &cred
->adc_fullname
.key
;
187 if (!getpublickey (cred
->adc_fullname
.name
, pkey_data
))
189 debug("getpublickey");
192 pkey
.n_bytes
= pkey_data
;
193 pkey
.n_len
= strlen (pkey_data
) + 1;
194 if (key_decryptsession_pk (cred
->adc_fullname
.name
, &pkey
,
197 debug ("decryptsessionkey");
198 return AUTH_BADCRED
; /* key not found */
203 if (cred
->adc_nickname
>= AUTHDES_CACHESZ
)
205 debug ("bad nickname");
206 return AUTH_BADCRED
; /* garbled credential */
209 sid
= cred
->adc_nickname
;
211 /* XXX This could be wrong, but else we have a
213 if (authdes_cache
[sid
].rname
== NULL
)
215 sessionkey
= &authdes_cache
[sid
].key
;
220 * Decrypt the timestamp
222 cryptbuf
[0] = verf
.adv_xtimestamp
;
223 if (cred
->adc_namekind
== ADN_FULLNAME
)
225 cryptbuf
[1].key
.high
= cred
->adc_fullname
.window
;
226 cryptbuf
[1].key
.low
= verf
.adv_winverf
;
227 ivec
.key
.high
= ivec
.key
.low
= 0;
228 status
= cbc_crypt ((char *) sessionkey
, (char *) cryptbuf
,
229 2 * sizeof (des_block
), DES_DECRYPT
| DES_HW
,
233 status
= ecb_crypt ((char *) sessionkey
, (char *) cryptbuf
,
234 sizeof (des_block
), DES_DECRYPT
| DES_HW
);
236 if (DES_FAILED (status
))
238 debug ("decryption failure");
239 return AUTH_FAILED
; /* system error */
243 * XDR the decrypted timestamp
245 ixdr
= (uint32_t *) cryptbuf
;
246 timestamp
.tv_sec
= IXDR_GET_INT32 (ixdr
);
247 timestamp
.tv_usec
= IXDR_GET_INT32 (ixdr
);
250 * Check for valid credentials and verifiers.
251 * They could be invalid because the key was flushed
252 * out of the cache, and so a new session should begin.
253 * Be sure and send AUTH_REJECTED{CRED, VERF} if this is the case.
256 struct timeval current
;
260 if (cred
->adc_namekind
== ADN_FULLNAME
)
264 window
= IXDR_GET_U_INT32 (ixdr
);
265 winverf
= IXDR_GET_U_INT32 (ixdr
);
266 if (winverf
!= window
- 1)
268 debug ("window verifier mismatch");
269 return AUTH_BADCRED
; /* garbled credential */
271 tmp_spot
= cache_spot (sessionkey
, cred
->adc_fullname
.name
,
273 if (tmp_spot
< 0 || tmp_spot
> AUTHDES_CACHESZ
)
275 debug ("replayed credential");
276 return AUTH_REJECTEDCRED
; /* replay */
283 window
= authdes_cache
[sid
].window
;
287 if (timestamp
.tv_usec
>= USEC_PER_SEC
)
289 debug ("invalid usecs");
290 /* cached out (bad key), or garbled verifier */
291 return nick
? AUTH_REJECTEDVERF
: AUTH_BADVERF
;
293 if (nick
&& BEFORE (×tamp
, &authdes_cache
[sid
].laststamp
))
295 debug ("timestamp before last seen");
296 return AUTH_REJECTEDVERF
; /* replay */
298 __gettimeofday (¤t
, (struct timezone
*) NULL
);
299 current
.tv_sec
-= window
; /* allow for expiration */
300 if (!BEFORE (¤t
, ×tamp
))
302 debug ("timestamp expired");
303 /* replay, or garbled credential */
304 return nick
? AUTH_REJECTEDVERF
: AUTH_BADCRED
;
309 * Set up the reply verifier
311 verf
.adv_nickname
= sid
;
314 * xdr the timestamp before encrypting
316 ixdr
= (uint32_t *) cryptbuf
;
317 IXDR_PUT_INT32 (ixdr
, timestamp
.tv_sec
- 1);
318 IXDR_PUT_INT32 (ixdr
, timestamp
.tv_usec
);
321 * encrypt the timestamp
323 status
= ecb_crypt ((char *) sessionkey
, (char *) cryptbuf
,
324 sizeof (des_block
), DES_ENCRYPT
| DES_HW
);
325 if (DES_FAILED (status
))
327 debug ("encryption failure");
328 return AUTH_FAILED
; /* system error */
330 verf
.adv_xtimestamp
= cryptbuf
[0];
333 * Serialize the reply verifier, and update rqst
335 ixdr
= (uint32_t *) msg
->rm_call
.cb_verf
.oa_base
;
336 *ixdr
++ = verf
.adv_xtimestamp
.key
.high
;
337 *ixdr
++ = verf
.adv_xtimestamp
.key
.low
;
338 *ixdr
++ = verf
.adv_int_u
;
340 rqst
->rq_xprt
->xp_verf
.oa_flavor
= AUTH_DES
;
341 rqst
->rq_xprt
->xp_verf
.oa_base
= msg
->rm_call
.cb_verf
.oa_base
;
342 rqst
->rq_xprt
->xp_verf
.oa_length
=
343 (char *) ixdr
- msg
->rm_call
.cb_verf
.oa_base
;
346 * We succeeded, commit the data to the cache now and
347 * finish cooking the credential.
349 entry
= &authdes_cache
[sid
];
350 entry
->laststamp
= timestamp
;
352 if (cred
->adc_namekind
== ADN_FULLNAME
)
356 cred
->adc_fullname
.window
= window
;
357 cred
->adc_nickname
= sid
; /* save nickname */
358 if (entry
->rname
!= NULL
)
359 mem_free (entry
->rname
, strlen (entry
->rname
) + 1);
360 full_len
= strlen (cred
->adc_fullname
.name
) + 1;
361 entry
->rname
= mem_alloc ((u_int
) full_len
);
362 if (entry
->rname
!= NULL
)
363 memcpy (entry
->rname
, cred
->adc_fullname
.name
, full_len
);
366 debug ("out of memory");
367 return AUTH_FAILED
; /* out of memory is bad */
369 entry
->key
= *sessionkey
;
370 entry
->window
= window
;
371 invalidate (entry
->localcred
); /* mark any cached cred invalid */
376 * nicknames are cooked into fullnames
378 cred
->adc_namekind
= ADN_FULLNAME
;
379 cred
->adc_fullname
.name
= entry
->rname
;
380 cred
->adc_fullname
.key
= entry
->key
;
381 cred
->adc_fullname
.window
= entry
->window
;
383 return AUTH_OK
; /* we made it! */
388 * Initialize the cache
395 authdes_cache
= (struct cache_entry
*)
396 calloc (sizeof (struct cache_entry
) * AUTHDES_CACHESZ
, 1);
397 if (authdes_cache
== NULL
)
400 authdes_lru
= (int *) mem_alloc (sizeof (int) * AUTHDES_CACHESZ
);
402 * Initialize the lru list
404 for (i
= 0; i
< AUTHDES_CACHESZ
; ++i
)
410 * Find the lru victim
415 return authdes_lru
[AUTHDES_CACHESZ
- 1];
419 * Note that sid was referenced
422 cache_ref (register uint32_t sid
)
428 prev
= authdes_lru
[0];
429 authdes_lru
[0] = sid
;
430 for (i
= 1; prev
!= sid
; ++i
)
432 curr
= authdes_lru
[i
];
433 authdes_lru
[i
] = prev
;
439 * Find a spot in the cache for a credential containing
440 * the items given. Return -1 if a replay is detected, otherwise
441 * return the spot in the cache.
444 cache_spot (register des_block
*key
, char *name
,
445 struct rpc_timeval
*timestamp
)
447 register struct cache_entry
*cp
;
449 register uint32_t hi
;
452 for (cp
= authdes_cache
, i
= 0; i
< AUTHDES_CACHESZ
; ++i
, ++cp
)
454 if (cp
->key
.key
.high
== hi
&&
455 cp
->key
.key
.low
== key
->key
.low
&&
457 memcmp (cp
->rname
, name
, strlen (name
) + 1) == 0)
459 if (BEFORE (timestamp
, &cp
->laststamp
))
461 ++svcauthdes_stats
.ncachereplays
;
462 return -1; /* replay */
464 ++svcauthdes_stats
.ncachehits
;
465 return i
; /* refresh */
468 ++svcauthdes_stats
.ncachemisses
;
469 return cache_victim (); /* new credential */
473 * Local credential handling stuff.
474 * NOTE: bsd unix dependent.
475 * Other operating systems should put something else here.
477 #define UNKNOWN -2 /* grouplen, if cached cred is unknown user */
478 #define INVALID -1 /* grouplen, if cache entry is invalid */
482 uid_t uid
; /* cached uid */
483 gid_t gid
; /* cached gid */
484 int grouplen
; /* length of cached groups */
485 int grouplen_max
; /* length of allocated cached groups */
486 gid_t groups
[0]; /* cached groups */
490 * Map a des credential into a unix cred.
491 * We cache the credential here so the application does
492 * not have to make an rpc call every time to interpret
496 authdes_getucred (const struct authdes_cred
*adc
, uid_t
* uid
, gid_t
* gid
,
497 short *grouplen
, gid_t
* groups
)
504 struct bsdcred
*cred
;
506 sid
= adc
->adc_nickname
;
507 if (sid
>= AUTHDES_CACHESZ
)
509 debug ("invalid nickname");
512 cred
= (struct bsdcred
*) authdes_cache
[sid
].localcred
;
513 if (cred
== NULL
|| cred
->grouplen
== INVALID
)
516 * not in cache: lookup
518 if (!netname2user (adc
->adc_fullname
.name
, &i_uid
, &i_gid
,
519 &i_grouplen
, groups
))
521 debug ("unknown netname");
523 cred
->grouplen
= UNKNOWN
; /* mark as lookup up, but not found */
527 if (cred
!= NULL
&& cred
->grouplen_max
< i_grouplen
)
529 /* We already have an allocated data structure. But it is
532 authdes_cache
[sid
].localcred
= NULL
;
538 /* We should allocate room for at least NGROUPS groups. */
539 int ngroups_max
= MAX (i_grouplen
, NGROUPS
);
541 cred
= (struct bsdcred
*) mem_alloc (sizeof (struct bsdcred
)
542 + ngroups_max
* sizeof (gid_t
));
546 authdes_cache
[sid
].localcred
= (char *) cred
;
547 cred
->grouplen
= INVALID
;
548 cred
->grouplen_max
= ngroups_max
;
551 debug ("missed ucred cache");
552 *uid
= cred
->uid
= i_uid
;
553 *gid
= cred
->gid
= i_gid
;
554 cred
->grouplen
= i_grouplen
;
555 for (i
= i_grouplen
- 1; i
>= 0; --i
)
556 cred
->groups
[i
] = groups
[i
];
557 /* Make sure no too large values are reported. */
558 *grouplen
= MIN (SHRT_MAX
, i_grouplen
);
561 else if (cred
->grouplen
== UNKNOWN
)
564 * Already lookup up, but no match found
575 /* Another stupidity in the interface: *grouplen is of type short.
576 So we might have to cut the information passed up short. */
577 int grouplen_copy
= MIN (SHRT_MAX
, cred
->grouplen
);
578 *grouplen
= grouplen_copy
;
579 for (i
= grouplen_copy
- 1; i
>= 0; --i
)
580 groups
[i
] = cred
->groups
[i
];
583 libc_hidden_nolink_sunrpc (authdes_getucred
, GLIBC_2_1
)
586 invalidate (char *cred
)
590 ((struct bsdcred
*) cred
)->grouplen
= INVALID
;