rpcsec_gss.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2008 Doug Rabson
  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
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. *
  28. * $FreeBSD$
  29. */
  30. #ifndef _RPCSEC_GSS_H
  31. #define _RPCSEC_GSS_H
  32. #include <kgssapi/gssapi.h>
  33. #ifndef MAX_GSS_MECH
  34. #define MAX_GSS_MECH 64
  35. #endif
  36. /*
  37. * Define the types of security service required for rpc_gss_seccreate().
  38. */
  39. typedef enum {
  40. rpc_gss_svc_default = 0,
  41. rpc_gss_svc_none = 1,
  42. rpc_gss_svc_integrity = 2,
  43. rpc_gss_svc_privacy = 3
  44. } rpc_gss_service_t;
  45. /*
  46. * Structure containing options for rpc_gss_seccreate().
  47. */
  48. typedef struct {
  49. int req_flags; /* GSS request bits */
  50. int time_req; /* requested credential lifetime */
  51. gss_cred_id_t my_cred; /* GSS credential */
  52. gss_channel_bindings_t input_channel_bindings;
  53. } rpc_gss_options_req_t;
  54. /*
  55. * Structure containing options returned by rpc_gss_seccreate().
  56. */
  57. typedef struct {
  58. int major_status;
  59. int minor_status;
  60. u_int rpcsec_version;
  61. int ret_flags;
  62. int time_req;
  63. gss_ctx_id_t gss_context;
  64. char actual_mechanism[MAX_GSS_MECH];
  65. } rpc_gss_options_ret_t;
  66. /*
  67. * Client principal type. Used as an argument to
  68. * rpc_gss_get_principal_name(). Also referenced by the
  69. * rpc_gss_rawcred_t structure.
  70. */
  71. typedef struct {
  72. int len;
  73. char name[1];
  74. } *rpc_gss_principal_t;
  75. /*
  76. * Structure for raw credentials used by rpc_gss_getcred() and
  77. * rpc_gss_set_callback().
  78. */
  79. typedef struct {
  80. u_int version; /* RPC version number */
  81. const char *mechanism; /* security mechanism */
  82. const char *qop; /* quality of protection */
  83. rpc_gss_principal_t client_principal; /* client name */
  84. const char *svc_principal; /* server name */
  85. rpc_gss_service_t service; /* service type */
  86. } rpc_gss_rawcred_t;
  87. /*
  88. * Unix credentials derived from raw credentials. Returned by
  89. * rpc_gss_getcred().
  90. */
  91. typedef struct {
  92. uid_t uid; /* user ID */
  93. gid_t gid; /* group ID */
  94. short gidlen;
  95. gid_t *gidlist; /* list of groups */
  96. } rpc_gss_ucred_t;
  97. /*
  98. * Structure used to enforce a particular QOP and service.
  99. */
  100. typedef struct {
  101. bool_t locked;
  102. rpc_gss_rawcred_t *raw_cred;
  103. } rpc_gss_lock_t;
  104. /*
  105. * Callback structure used by rpc_gss_set_callback().
  106. */
  107. typedef struct {
  108. u_int program; /* RPC program number */
  109. u_int version; /* RPC version number */
  110. /* user defined callback */
  111. bool_t (*callback)(struct svc_req *req,
  112. gss_cred_id_t deleg,
  113. gss_ctx_id_t gss_context,
  114. rpc_gss_lock_t *lock,
  115. void **cookie);
  116. } rpc_gss_callback_t;
  117. /*
  118. * Structure used to return error information by rpc_gss_get_error()
  119. */
  120. typedef struct {
  121. int rpc_gss_error;
  122. int system_error; /* same as errno */
  123. } rpc_gss_error_t;
  124. /*
  125. * Values for rpc_gss_error
  126. */
  127. #define RPC_GSS_ER_SUCCESS 0 /* no error */
  128. #define RPC_GSS_ER_SYSTEMERROR 1 /* system error */
  129. __BEGIN_DECLS
  130. #ifdef _KERNEL
  131. /*
  132. * Set up a structure of entry points for the kgssapi module and inline
  133. * functions named rpc_gss_XXX_call() to use them, so that the kgssapi
  134. * module doesn't need to be loaded for the NFS modules to work using
  135. * AUTH_SYS. The kgssapi modules will be loaded by the gssd(8) daemon
  136. * when it is started up and the entry points will then be filled in.
  137. */
  138. typedef AUTH *rpc_gss_secfind_ftype(CLIENT *clnt, struct ucred *cred,
  139. const char *principal, gss_OID mech_oid,
  140. rpc_gss_service_t service);
  141. typedef void rpc_gss_secpurge_ftype(CLIENT *clnt);
  142. typedef AUTH *rpc_gss_seccreate_ftype(CLIENT *clnt, struct ucred *cred,
  143. const char *clnt_principal, const char *principal,
  144. const char *mechanism, rpc_gss_service_t service,
  145. const char *qop, rpc_gss_options_req_t *options_req,
  146. rpc_gss_options_ret_t *options_ret);
  147. typedef bool_t rpc_gss_set_defaults_ftype(AUTH *auth,
  148. rpc_gss_service_t service, const char *qop);
  149. typedef int rpc_gss_max_data_length_ftype(AUTH *handle,
  150. int max_tp_unit_len);
  151. typedef void rpc_gss_get_error_ftype(rpc_gss_error_t *error);
  152. typedef bool_t rpc_gss_mech_to_oid_ftype(const char *mech, gss_OID *oid_ret);
  153. typedef bool_t rpc_gss_oid_to_mech_ftype(gss_OID oid, const char **mech_ret);
  154. typedef bool_t rpc_gss_qop_to_num_ftype(const char *qop, const char *mech,
  155. u_int *num_ret);
  156. typedef const char **rpc_gss_get_mechanisms_ftype(void);
  157. typedef bool_t rpc_gss_get_versions_ftype(u_int *vers_hi, u_int *vers_lo);
  158. typedef bool_t rpc_gss_is_installed_ftype(const char *mech);
  159. typedef bool_t rpc_gss_set_svc_name_ftype(const char *principal,
  160. const char *mechanism, u_int req_time, u_int program,
  161. u_int version);
  162. typedef void rpc_gss_clear_svc_name_ftype(u_int program, u_int version);
  163. typedef bool_t rpc_gss_getcred_ftype(struct svc_req *req,
  164. rpc_gss_rawcred_t **rcred,
  165. rpc_gss_ucred_t **ucred, void **cookie);
  166. typedef bool_t rpc_gss_set_callback_ftype(rpc_gss_callback_t *cb);
  167. typedef void rpc_gss_clear_callback_ftype(rpc_gss_callback_t *cb);
  168. typedef bool_t rpc_gss_get_principal_name_ftype(rpc_gss_principal_t *principal,
  169. const char *mech, const char *name, const char *node,
  170. const char *domain);
  171. typedef int rpc_gss_svc_max_data_length_ftype(struct svc_req *req,
  172. int max_tp_unit_len);
  173. typedef void rpc_gss_refresh_auth_ftype(AUTH *auth);
  174. struct rpc_gss_entries {
  175. rpc_gss_secfind_ftype *rpc_gss_secfind;
  176. rpc_gss_secpurge_ftype *rpc_gss_secpurge;
  177. rpc_gss_seccreate_ftype *rpc_gss_seccreate;
  178. rpc_gss_set_defaults_ftype *rpc_gss_set_defaults;
  179. rpc_gss_max_data_length_ftype *rpc_gss_max_data_length;
  180. rpc_gss_get_error_ftype *rpc_gss_get_error;
  181. rpc_gss_mech_to_oid_ftype *rpc_gss_mech_to_oid;
  182. rpc_gss_oid_to_mech_ftype *rpc_gss_oid_to_mech;
  183. rpc_gss_qop_to_num_ftype *rpc_gss_qop_to_num;
  184. rpc_gss_get_mechanisms_ftype *rpc_gss_get_mechanisms;
  185. rpc_gss_get_versions_ftype *rpc_gss_get_versions;
  186. rpc_gss_is_installed_ftype *rpc_gss_is_installed;
  187. rpc_gss_set_svc_name_ftype *rpc_gss_set_svc_name;
  188. rpc_gss_clear_svc_name_ftype *rpc_gss_clear_svc_name;
  189. rpc_gss_getcred_ftype *rpc_gss_getcred;
  190. rpc_gss_set_callback_ftype *rpc_gss_set_callback;
  191. rpc_gss_clear_callback_ftype *rpc_gss_clear_callback;
  192. rpc_gss_get_principal_name_ftype *rpc_gss_get_principal_name;
  193. rpc_gss_svc_max_data_length_ftype *rpc_gss_svc_max_data_length;
  194. rpc_gss_refresh_auth_ftype *rpc_gss_refresh_auth;
  195. };
  196. extern struct rpc_gss_entries rpc_gss_entries;
  197. /* Functions to access the entry points. */
  198. static __inline AUTH *
  199. rpc_gss_secfind_call(CLIENT *clnt, struct ucred *cred, const char *principal,
  200. gss_OID mech_oid, rpc_gss_service_t service)
  201. {
  202. AUTH *ret = NULL;
  203. if (rpc_gss_entries.rpc_gss_secfind != NULL)
  204. ret = (*rpc_gss_entries.rpc_gss_secfind)(clnt, cred, principal,
  205. mech_oid, service);
  206. return (ret);
  207. }
  208. static __inline void
  209. rpc_gss_secpurge_call(CLIENT *clnt)
  210. {
  211. if (rpc_gss_entries.rpc_gss_secpurge != NULL)
  212. (*rpc_gss_entries.rpc_gss_secpurge)(clnt);
  213. }
  214. static __inline AUTH *
  215. rpc_gss_seccreate_call(CLIENT *clnt, struct ucred *cred,
  216. const char *clnt_principal, const char *principal, const char *mechanism,
  217. rpc_gss_service_t service, const char *qop,
  218. rpc_gss_options_req_t *options_req, rpc_gss_options_ret_t *options_ret)
  219. {
  220. AUTH *ret = NULL;
  221. if (rpc_gss_entries.rpc_gss_seccreate != NULL)
  222. ret = (*rpc_gss_entries.rpc_gss_seccreate)(clnt, cred,
  223. clnt_principal, principal, mechanism, service, qop,
  224. options_req, options_ret);
  225. return (ret);
  226. }
  227. static __inline bool_t
  228. rpc_gss_set_defaults_call(AUTH *auth, rpc_gss_service_t service,
  229. const char *qop)
  230. {
  231. bool_t ret = 1;
  232. if (rpc_gss_entries.rpc_gss_set_defaults != NULL)
  233. ret = (*rpc_gss_entries.rpc_gss_set_defaults)(auth, service,
  234. qop);
  235. return (ret);
  236. }
  237. static __inline int
  238. rpc_gss_max_data_length_call(AUTH *handle, int max_tp_unit_len)
  239. {
  240. int ret = 0;
  241. if (rpc_gss_entries.rpc_gss_max_data_length != NULL)
  242. ret = (*rpc_gss_entries.rpc_gss_max_data_length)(handle,
  243. max_tp_unit_len);
  244. return (ret);
  245. }
  246. static __inline void
  247. rpc_gss_get_error_call(rpc_gss_error_t *error)
  248. {
  249. if (rpc_gss_entries.rpc_gss_get_error != NULL)
  250. (*rpc_gss_entries.rpc_gss_get_error)(error);
  251. }
  252. static __inline bool_t
  253. rpc_gss_mech_to_oid_call(const char *mech, gss_OID *oid_ret)
  254. {
  255. bool_t ret = 1;
  256. if (rpc_gss_entries.rpc_gss_mech_to_oid != NULL)
  257. ret = (*rpc_gss_entries.rpc_gss_mech_to_oid)(mech, oid_ret);
  258. return (ret);
  259. }
  260. static __inline bool_t
  261. rpc_gss_oid_to_mech_call(gss_OID oid, const char **mech_ret)
  262. {
  263. bool_t ret = 1;
  264. if (rpc_gss_entries.rpc_gss_oid_to_mech != NULL)
  265. ret = (*rpc_gss_entries.rpc_gss_oid_to_mech)(oid, mech_ret);
  266. return (ret);
  267. }
  268. static __inline bool_t
  269. rpc_gss_qop_to_num_call(const char *qop, const char *mech, u_int *num_ret)
  270. {
  271. bool_t ret = 1;
  272. if (rpc_gss_entries.rpc_gss_qop_to_num != NULL)
  273. ret = (*rpc_gss_entries.rpc_gss_qop_to_num)(qop, mech, num_ret);
  274. return (ret);
  275. }
  276. static __inline const char **
  277. rpc_gss_get_mechanisms_call(void)
  278. {
  279. const char **ret = NULL;
  280. if (rpc_gss_entries.rpc_gss_get_mechanisms != NULL)
  281. ret = (*rpc_gss_entries.rpc_gss_get_mechanisms)();
  282. return (ret);
  283. }
  284. static __inline bool_t
  285. rpc_gss_get_versions_call(u_int *vers_hi, u_int *vers_lo)
  286. {
  287. bool_t ret = 1;
  288. if (rpc_gss_entries.rpc_gss_get_versions != NULL)
  289. ret = (*rpc_gss_entries.rpc_gss_get_versions)(vers_hi, vers_lo);
  290. return (ret);
  291. }
  292. static __inline bool_t
  293. rpc_gss_is_installed_call(const char *mech)
  294. {
  295. bool_t ret = 1;
  296. if (rpc_gss_entries.rpc_gss_is_installed != NULL)
  297. ret = (*rpc_gss_entries.rpc_gss_is_installed)(mech);
  298. return (ret);
  299. }
  300. static __inline bool_t
  301. rpc_gss_set_svc_name_call(const char *principal, const char *mechanism,
  302. u_int req_time, u_int program, u_int version)
  303. {
  304. bool_t ret = 1;
  305. if (rpc_gss_entries.rpc_gss_set_svc_name != NULL)
  306. ret = (*rpc_gss_entries.rpc_gss_set_svc_name)(principal,
  307. mechanism, req_time, program, version);
  308. return (ret);
  309. }
  310. static __inline void
  311. rpc_gss_clear_svc_name_call(u_int program, u_int version)
  312. {
  313. if (rpc_gss_entries.rpc_gss_clear_svc_name != NULL)
  314. (*rpc_gss_entries.rpc_gss_clear_svc_name)(program, version);
  315. }
  316. static __inline bool_t
  317. rpc_gss_getcred_call(struct svc_req *req, rpc_gss_rawcred_t **rcred,
  318. rpc_gss_ucred_t **ucred, void **cookie)
  319. {
  320. bool_t ret = 1;
  321. if (rpc_gss_entries.rpc_gss_getcred != NULL)
  322. ret = (*rpc_gss_entries.rpc_gss_getcred)(req, rcred, ucred,
  323. cookie);
  324. return (ret);
  325. }
  326. static __inline bool_t
  327. rpc_gss_set_callback_call(rpc_gss_callback_t *cb)
  328. {
  329. bool_t ret = 1;
  330. if (rpc_gss_entries.rpc_gss_set_callback != NULL)
  331. ret = (*rpc_gss_entries.rpc_gss_set_callback)(cb);
  332. return (ret);
  333. }
  334. static __inline void
  335. rpc_gss_clear_callback_call(rpc_gss_callback_t *cb)
  336. {
  337. if (rpc_gss_entries.rpc_gss_clear_callback != NULL)
  338. (*rpc_gss_entries.rpc_gss_clear_callback)(cb);
  339. }
  340. static __inline bool_t
  341. rpc_gss_get_principal_name_call(rpc_gss_principal_t *principal,
  342. const char *mech, const char *name, const char *node, const char *domain)
  343. {
  344. bool_t ret = 1;
  345. if (rpc_gss_entries.rpc_gss_get_principal_name != NULL)
  346. ret = (*rpc_gss_entries.rpc_gss_get_principal_name)(principal,
  347. mech, name, node, domain);
  348. return (ret);
  349. }
  350. static __inline int
  351. rpc_gss_svc_max_data_length_call(struct svc_req *req, int max_tp_unit_len)
  352. {
  353. int ret = 0;
  354. if (rpc_gss_entries.rpc_gss_svc_max_data_length != NULL)
  355. ret = (*rpc_gss_entries.rpc_gss_svc_max_data_length)(req,
  356. max_tp_unit_len);
  357. return (ret);
  358. }
  359. static __inline void
  360. rpc_gss_refresh_auth_call(AUTH *auth)
  361. {
  362. if (rpc_gss_entries.rpc_gss_refresh_auth != NULL)
  363. (*rpc_gss_entries.rpc_gss_refresh_auth)(auth);
  364. }
  365. AUTH *rpc_gss_secfind(CLIENT *clnt, struct ucred *cred,
  366. const char *principal, gss_OID mech_oid, rpc_gss_service_t service);
  367. void rpc_gss_secpurge(CLIENT *clnt);
  368. void rpc_gss_refresh_auth(AUTH *auth);
  369. AUTH *rpc_gss_seccreate(CLIENT *clnt, struct ucred *cred,
  370. const char *clnt_principal, const char *principal,
  371. const char *mechanism, rpc_gss_service_t service,
  372. const char *qop, rpc_gss_options_req_t *options_req,
  373. rpc_gss_options_ret_t *options_ret);
  374. #else /* !_KERNEL */
  375. AUTH *rpc_gss_seccreate(CLIENT *clnt, struct ucred *cred,
  376. const char *principal, const char *mechanism, rpc_gss_service_t service,
  377. const char *qop, rpc_gss_options_req_t *options_req,
  378. rpc_gss_options_ret_t *options_ret);
  379. #endif /* _KERNEL */
  380. bool_t rpc_gss_set_defaults(AUTH *auth, rpc_gss_service_t service,
  381. const char *qop);
  382. int rpc_gss_max_data_length(AUTH *handle, int max_tp_unit_len);
  383. void rpc_gss_get_error(rpc_gss_error_t *error);
  384. bool_t rpc_gss_mech_to_oid(const char *mech, gss_OID *oid_ret);
  385. bool_t rpc_gss_oid_to_mech(gss_OID oid, const char **mech_ret);
  386. bool_t rpc_gss_qop_to_num(const char *qop, const char *mech, u_int *num_ret);
  387. const char **rpc_gss_get_mechanisms(void);
  388. const char **rpc_gss_get_mech_info(const char *mech, rpc_gss_service_t *service);
  389. bool_t rpc_gss_get_versions(u_int *vers_hi, u_int *vers_lo);
  390. bool_t rpc_gss_is_installed(const char *mech);
  391. bool_t rpc_gss_set_svc_name(const char *principal, const char *mechanism,
  392. u_int req_time, u_int program, u_int version);
  393. void rpc_gss_clear_svc_name(u_int program, u_int version);
  394. bool_t rpc_gss_getcred(struct svc_req *req, rpc_gss_rawcred_t **rcred,
  395. rpc_gss_ucred_t **ucred, void **cookie);
  396. bool_t rpc_gss_set_callback(rpc_gss_callback_t *cb);
  397. void rpc_gss_clear_callback(rpc_gss_callback_t *cb);
  398. bool_t rpc_gss_get_principal_name(rpc_gss_principal_t *principal,
  399. const char *mech, const char *name, const char *node, const char *domain);
  400. int rpc_gss_svc_max_data_length(struct svc_req *req, int max_tp_unit_len);
  401. /*
  402. * Internal interface from the RPC implementation.
  403. */
  404. #ifndef _KERNEL
  405. bool_t __rpc_gss_wrap(AUTH *auth, void *header, size_t headerlen,
  406. XDR* xdrs, xdrproc_t xdr_args, void *args_ptr);
  407. bool_t __rpc_gss_unwrap(AUTH *auth, XDR* xdrs, xdrproc_t xdr_args,
  408. void *args_ptr);
  409. #endif
  410. bool_t __rpc_gss_set_error(int rpc_gss_error, int system_error);
  411. __END_DECLS
  412. #endif /* !_RPCSEC_GSS_H */