Imported Upstream version 0.2.5
[platform/upstream/libtirpc.git] / tirpc / rpc / auth.h
1 /*      $NetBSD: auth.h,v 1.15 2000/06/02 22:57:55 fvdl Exp $   */
2
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  *      from: @(#)auth.h 1.17 88/02/08 SMI
31  *      from: @(#)auth.h        2.3 88/08/07 4.0 RPCSRC
32  *      from: @(#)auth.h        1.43    98/02/02 SMI
33  * $FreeBSD: src/include/rpc/auth.h,v 1.20 2003/01/01 18:48:42 schweikh Exp $
34  */
35
36 /*
37  * auth.h, Authentication interface.
38  *
39  * Copyright (C) 1984, Sun Microsystems, Inc.
40  *
41  * The data structures are completely opaque to the client.  The client
42  * is required to pass an AUTH * to routines that create rpc
43  * "sessions".
44  */
45
46 #ifndef _TIRPC_AUTH_H
47 #define  _TIRPC_AUTH_H
48
49 #include <rpc/xdr.h>
50 #include <rpc/clnt_stat.h>
51 #include <sys/cdefs.h>
52 #include <sys/socket.h>
53 #include <sys/types.h>
54
55
56 #define MAX_AUTH_BYTES  400
57 #define MAXNETNAMELEN   255     /* maximum length of network user's name */
58
59 /*
60  *  Client side authentication/security data
61  */
62
63 typedef struct sec_data {
64         u_int   secmod;         /* security mode number e.g. in nfssec.conf */
65         u_int   rpcflavor;      /* rpc flavors:AUTH_UNIX,AUTH_DES,RPCSEC_GSS */
66         int     flags;          /* AUTH_F_xxx flags */
67         caddr_t data;           /* opaque data per flavor */
68 } sec_data_t;
69
70 #ifdef _SYSCALL32_IMPL
71 struct sec_data32 {
72         uint32_t secmod;        /* security mode number e.g. in nfssec.conf */
73         uint32_t rpcflavor;     /* rpc flavors:AUTH_UNIX,AUTH_DES,RPCSEC_GSS */
74         int32_t flags;          /* AUTH_F_xxx flags */
75         caddr32_t data;         /* opaque data per flavor */
76 };
77 #endif /* _SYSCALL32_IMPL */
78
79 /*
80  * AUTH_DES flavor specific data from sec_data opaque data field.
81  * AUTH_KERB has the same structure.
82  */
83 typedef struct des_clnt_data {
84         struct netbuf   syncaddr;       /* time sync addr */
85         struct knetconfig *knconf;      /* knetconfig info that associated */
86                                         /* with the syncaddr. */
87         char            *netname;       /* server's netname */
88         int             netnamelen;     /* server's netname len */
89 } dh_k4_clntdata_t;
90
91 #ifdef _SYSCALL32_IMPL
92 struct des_clnt_data32 {
93         struct netbuf32 syncaddr;       /* time sync addr */
94         caddr32_t knconf;               /* knetconfig info that associated */
95                                         /* with the syncaddr. */
96         caddr32_t netname;              /* server's netname */
97         int32_t netnamelen;             /* server's netname len */
98 };
99 #endif /* _SYSCALL32_IMPL */
100
101 #ifdef KERBEROS
102 /*
103  * flavor specific data to hold the data for AUTH_DES/AUTH_KERB(v4)
104  * in sec_data->data opaque field.
105  */
106 typedef struct krb4_svc_data {
107         int             window;         /* window option value */
108 } krb4_svcdata_t;
109  
110 typedef struct krb4_svc_data    des_svcdata_t;
111 #endif /* KERBEROS */
112
113 /*
114  * authentication/security specific flags
115  */
116 #define AUTH_F_RPCTIMESYNC      0x001   /* use RPC to do time sync */
117 #define AUTH_F_TRYNONE          0x002   /* allow fall back to AUTH_NONE */
118
119
120 /*
121  * Status returned from authentication check
122  */
123 enum auth_stat {
124         AUTH_OK=0,
125         /*
126          * failed at  remote end
127          */
128         AUTH_BADCRED=1,                 /* bogus credentials (seal broken) */
129         AUTH_REJECTEDCRED=2,            /* client should begin new session */
130         AUTH_BADVERF=3,                 /* bogus verifier (seal broken) */
131         AUTH_REJECTEDVERF=4,            /* verifier expired or was replayed */
132         AUTH_TOOWEAK=5,                 /* rejected due to security reasons */
133         /*
134          * failed locally
135         */
136         AUTH_INVALIDRESP=6,             /* bogus response verifier */
137         AUTH_FAILED=7,                  /* some unknown reason */
138 #ifdef KERBEROS
139         /*
140          * kerberos errors
141          */
142         AUTH_KERB_GENERIC = 8,          /* kerberos generic error */
143         AUTH_TIMEEXPIRE = 9,            /* time of credential expired */
144         AUTH_TKT_FILE = 10,             /* something wrong with ticket file */
145         AUTH_DECODE = 11,               /* can't decode authenticator */
146         AUTH_NET_ADDR = 12,             /* wrong net address in ticket */
147 #endif /* KERBEROS */
148
149         /*
150          * RPCSEC_GSS errors
151          */
152         RPCSEC_GSS_CREDPROBLEM = 13,
153         RPCSEC_GSS_CTXPROBLEM = 14
154
155 };
156
157 typedef u_int32_t u_int32;      /* 32-bit unsigned integers */
158
159 union des_block {
160         struct {
161                 u_int32_t high;
162                 u_int32_t low;
163         } key;
164         char c[8];
165 };
166 typedef union des_block des_block;
167 __BEGIN_DECLS
168 extern bool_t xdr_des_block(XDR *, des_block *);
169 __END_DECLS
170
171 /*
172  * Authentication info.  Opaque to client.
173  */
174 struct opaque_auth {
175         enum_t  oa_flavor;              /* flavor of auth */
176         caddr_t oa_base;                /* address of more auth stuff */
177         u_int   oa_length;              /* not to exceed MAX_AUTH_BYTES */
178 };
179
180
181 /*
182  * Auth handle, interface to client side authenticators.
183  */
184 typedef struct __auth {
185         struct  opaque_auth     ah_cred;
186         struct  opaque_auth     ah_verf;
187         union   des_block       ah_key;
188         struct auth_ops {
189                 void    (*ah_nextverf) (struct __auth *);
190                 /* nextverf & serialize */
191                 int     (*ah_marshal) (struct __auth *, XDR *);
192                 /* validate verifier */
193                 int     (*ah_validate) (struct __auth *,
194                             struct opaque_auth *);
195                 /* refresh credentials */
196                 int     (*ah_refresh) (struct __auth *, void *);
197                 /* destroy this structure */
198                 void    (*ah_destroy) (struct __auth *);
199                 /* encode data for wire */
200                 int     (*ah_wrap) (struct __auth *, XDR *, xdrproc_t, caddr_t);
201                 /* decode data for wire */
202                 int     (*ah_unwrap) (struct __auth *, XDR *, xdrproc_t, caddr_t);
203
204         } *ah_ops;
205         void *ah_private;
206         int ah_refcnt;
207 } AUTH;
208
209 static __inline int
210 auth_get(AUTH *auth)
211 {
212         return __sync_add_and_fetch(&auth->ah_refcnt, 1);
213 }
214
215 static __inline int
216 auth_put(AUTH *auth)
217 {
218         return __sync_sub_and_fetch(&auth->ah_refcnt, 1);
219 }
220
221
222
223 /*
224  * Authentication ops.
225  * The ops and the auth handle provide the interface to the authenticators.
226  *
227  * AUTH *auth;
228  * XDR  *xdrs;
229  * struct opaque_auth verf;
230  */
231 #define AUTH_NEXTVERF(auth)             \
232                 ((*((auth)->ah_ops->ah_nextverf))(auth))
233 #define auth_nextverf(auth)             \
234                 ((*((auth)->ah_ops->ah_nextverf))(auth))
235
236 #define AUTH_MARSHALL(auth, xdrs)       \
237                 ((*((auth)->ah_ops->ah_marshal))(auth, xdrs))
238 #define auth_marshall(auth, xdrs)       \
239                 ((*((auth)->ah_ops->ah_marshal))(auth, xdrs))
240
241 #define AUTH_VALIDATE(auth, verfp)      \
242                 ((*((auth)->ah_ops->ah_validate))((auth), verfp))
243 #define auth_validate(auth, verfp)      \
244                 ((*((auth)->ah_ops->ah_validate))((auth), verfp))
245
246 #define AUTH_REFRESH(auth, msg)         \
247                 ((*((auth)->ah_ops->ah_refresh))(auth, msg))
248 #define auth_refresh(auth, msg)         \
249                 ((*((auth)->ah_ops->ah_refresh))(auth, msg))
250
251 #define AUTH_DESTROY(auth)                                              \
252                 do {                                                    \
253                         int refs;                                       \
254                         if ((refs = auth_put((auth))) == 0)             \
255                                 ((*((auth)->ah_ops->ah_destroy))(auth));\
256                 } while (0)
257
258 #define auth_destroy(auth)                                              \
259                 do {                                                    \
260                         int refs;                                       \
261                         if ((refs = auth_put((auth))) == 0)             \
262                                 ((*((auth)->ah_ops->ah_destroy))(auth));\
263                 } while (0)
264
265 #define AUTH_WRAP(auth, xdrs, xfunc, xwhere)            \
266                 ((*((auth)->ah_ops->ah_wrap))(auth, xdrs, \
267                 xfunc, xwhere))
268 #define auth_wrap(auth, xdrs, xfunc, xwhere)            \
269                 ((*((auth)->ah_ops->ah_wrap))(auth, xdrs, \
270                 xfunc, xwhere))
271
272 #define AUTH_UNWRAP(auth, xdrs, xfunc, xwhere)          \
273                 ((*((auth)->ah_ops->ah_unwrap))(auth, xdrs, \
274                 xfunc, xwhere))
275 #define auth_unwrap(auth, xdrs, xfunc, xwhere)          \
276                 ((*((auth)->ah_ops->ah_unwrap))(auth, xdrs, \
277                 xfunc, xwhere))
278
279
280 __BEGIN_DECLS
281 extern struct opaque_auth _null_auth;
282 __END_DECLS
283
284 /*
285  * Any style authentication.  These routines can be used by any
286  * authentication style that does not use the wrap/unwrap functions.
287  */
288 int authany_wrap(void), authany_unwrap(void);
289
290 /*
291  * These are the various implementations of client side authenticators.
292  */
293
294 /*
295  * System style authentication
296  * AUTH *authunix_create(machname, uid, gid, len, aup_gids)
297  *      char *machname;
298  *      int uid;
299  *      int gid;
300  *      int len;
301  *      int *aup_gids;
302  */
303 __BEGIN_DECLS
304 extern AUTH *authunix_create(char *, uid_t, uid_t, int, uid_t *);
305 extern AUTH *authunix_create_default(void);     /* takes no parameters */
306 extern AUTH *authnone_create(void);             /* takes no parameters */
307 __END_DECLS
308 /*
309  * DES style authentication
310  * AUTH *authsecdes_create(servername, window, timehost, ckey)
311  *      char *servername;               - network name of server
312  *      u_int window;                   - time to live
313  *      const char *timehost;                   - optional hostname to sync with
314  *      des_block *ckey;                - optional conversation key to use
315  */
316 __BEGIN_DECLS
317 extern AUTH *authdes_create (char *, u_int, struct sockaddr *, des_block *);
318 extern AUTH *authdes_seccreate (const char *, const u_int, const  char *,
319     const  des_block *);
320 __END_DECLS
321
322 __BEGIN_DECLS
323 extern bool_t xdr_opaque_auth           (XDR *, struct opaque_auth *);
324 __END_DECLS
325
326 #define authsys_create(c,i1,i2,i3,ip) authunix_create((c),(i1),(i2),(i3),(ip))
327 #define authsys_create_default() authunix_create_default()
328
329 /*
330  * Netname manipulation routines.
331  */
332 __BEGIN_DECLS
333 extern int getnetname(char *);
334 extern int host2netname(char *, const char *, const char *);
335 extern int user2netname(char *, const uid_t, const char *);
336 extern int netname2user(char *, uid_t *, gid_t *, int *, gid_t *);
337 extern int netname2host(char *, char *, const int);
338 extern void passwd2des ( char *, char * );
339 __END_DECLS
340
341 /*
342  *
343  * These routines interface to the keyserv daemon
344  *
345  */
346 __BEGIN_DECLS
347 extern int key_decryptsession(const char *, des_block *);
348 extern int key_encryptsession(const char *, des_block *);
349 extern int key_gendes(des_block *);
350 extern int key_setsecret(const char *);
351 extern int key_secretkey_is_set(void);
352 __END_DECLS
353
354 /*
355  * Publickey routines.
356  */
357 __BEGIN_DECLS
358 extern int getpublickey (const char *, char *);
359 extern int getpublicandprivatekey (char *, char *);
360 extern int getsecretkey (char *, char *, char *);
361 __END_DECLS
362
363 #ifdef KERBEROS
364 /*
365  * Kerberos style authentication
366  * AUTH *authkerb_seccreate(service, srv_inst, realm, window, timehost, status)
367  *      const char *service;                    - service name
368  *      const char *srv_inst;                   - server instance
369  *      const char *realm;                      - server realm
370  *      const u_int window;                     - time to live
371  *      const char *timehost;                   - optional hostname to sync with
372  *      int *status;                            - kerberos status returned
373  */
374 __BEGIN_DECLS
375 extern AUTH     *authkerb_seccreate(const char *, const char *, const  char *,
376                     const u_int, const char *, int *);
377 __END_DECLS
378
379 /*
380  * Map a kerberos credential into a unix cred.
381  *
382  *      authkerb_getucred(rqst, uid, gid, grouplen, groups)
383  *      const struct svc_req *rqst;             - request pointer
384  *      uid_t *uid;
385  *      gid_t *gid;
386  *      short *grouplen;
387  *      int *groups;
388  *
389  */
390 __BEGIN_DECLS
391 extern int      authkerb_getucred(/* struct svc_req *, uid_t *, gid_t *,
392                     short *, int * */);
393 __END_DECLS
394 #endif /* KERBEROS */
395
396 __BEGIN_DECLS
397 struct svc_req;
398 struct rpc_msg;
399 enum auth_stat _svcauth_none (struct svc_req *, struct rpc_msg *);
400 enum auth_stat _svcauth_short (struct svc_req *, struct rpc_msg *);
401 enum auth_stat _svcauth_unix (struct svc_req *, struct rpc_msg *);
402 enum auth_stat _svcauth_gss (struct svc_req *, struct rpc_msg *, bool_t *);
403 __END_DECLS
404
405 #define AUTH_NONE       0               /* no authentication */
406 #define AUTH_NULL       0               /* backward compatibility */
407 #define AUTH_SYS        1               /* unix style (uid, gids) */
408 #define AUTH_UNIX       AUTH_SYS
409 #define AUTH_SHORT      2               /* short hand unix style */
410 #define AUTH_DH         3               /* for Diffie-Hellman mechanism */
411 #define AUTH_DES        AUTH_DH         /* for backward compatibility */
412 #define AUTH_KERB       4               /* kerberos style */
413 #define RPCSEC_GSS      6               /* RPCSEC_GSS */
414
415 #endif /* !_TIRPC_AUTH_H */