auth_gss.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785
  1. /*
  2. * linux/net/sunrpc/auth_gss/auth_gss.c
  3. *
  4. * RPCSEC_GSS client authentication.
  5. *
  6. * Copyright (c) 2000 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Dug Song <dugsong@monkey.org>
  10. * Andy Adamson <andros@umich.edu>
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of the University nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  27. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  32. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/types.h>
  40. #include <linux/slab.h>
  41. #include <linux/sched.h>
  42. #include <linux/pagemap.h>
  43. #include <linux/sunrpc/clnt.h>
  44. #include <linux/sunrpc/auth.h>
  45. #include <linux/sunrpc/auth_gss.h>
  46. #include <linux/sunrpc/svcauth_gss.h>
  47. #include <linux/sunrpc/gss_err.h>
  48. #include <linux/workqueue.h>
  49. #include <linux/sunrpc/rpc_pipe_fs.h>
  50. #include <linux/sunrpc/gss_api.h>
  51. #include <asm/uaccess.h>
  52. #include "../netns.h"
  53. static const struct rpc_authops authgss_ops;
  54. static const struct rpc_credops gss_credops;
  55. static const struct rpc_credops gss_nullops;
  56. #define GSS_RETRY_EXPIRED 5
  57. static unsigned int gss_expired_cred_retry_delay = GSS_RETRY_EXPIRED;
  58. #ifdef RPC_DEBUG
  59. # define RPCDBG_FACILITY RPCDBG_AUTH
  60. #endif
  61. #define GSS_CRED_SLACK (RPC_MAX_AUTH_SIZE * 2)
  62. /* length of a krb5 verifier (48), plus data added before arguments when
  63. * using integrity (two 4-byte integers): */
  64. #define GSS_VERF_SLACK 100
  65. struct gss_auth {
  66. struct kref kref;
  67. struct rpc_auth rpc_auth;
  68. struct gss_api_mech *mech;
  69. enum rpc_gss_svc service;
  70. struct rpc_clnt *client;
  71. struct net *net;
  72. /*
  73. * There are two upcall pipes; dentry[1], named "gssd", is used
  74. * for the new text-based upcall; dentry[0] is named after the
  75. * mechanism (for example, "krb5") and exists for
  76. * backwards-compatibility with older gssd's.
  77. */
  78. struct rpc_pipe *pipe[2];
  79. const char *target_name;
  80. };
  81. /* pipe_version >= 0 if and only if someone has a pipe open. */
  82. static DEFINE_SPINLOCK(pipe_version_lock);
  83. static struct rpc_wait_queue pipe_version_rpc_waitqueue;
  84. static DECLARE_WAIT_QUEUE_HEAD(pipe_version_waitqueue);
  85. static void gss_free_ctx(struct gss_cl_ctx *);
  86. static const struct rpc_pipe_ops gss_upcall_ops_v0;
  87. static const struct rpc_pipe_ops gss_upcall_ops_v1;
  88. static inline struct gss_cl_ctx *
  89. gss_get_ctx(struct gss_cl_ctx *ctx)
  90. {
  91. atomic_inc(&ctx->count);
  92. return ctx;
  93. }
  94. static inline void
  95. gss_put_ctx(struct gss_cl_ctx *ctx)
  96. {
  97. if (atomic_dec_and_test(&ctx->count))
  98. gss_free_ctx(ctx);
  99. }
  100. /* gss_cred_set_ctx:
  101. * called by gss_upcall_callback and gss_create_upcall in order
  102. * to set the gss context. The actual exchange of an old context
  103. * and a new one is protected by the pipe->lock.
  104. */
  105. static void
  106. gss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx)
  107. {
  108. struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
  109. if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
  110. return;
  111. gss_get_ctx(ctx);
  112. rcu_assign_pointer(gss_cred->gc_ctx, ctx);
  113. set_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  114. smp_mb__before_clear_bit();
  115. clear_bit(RPCAUTH_CRED_NEW, &cred->cr_flags);
  116. }
  117. static const void *
  118. simple_get_bytes(const void *p, const void *end, void *res, size_t len)
  119. {
  120. const void *q = (const void *)((const char *)p + len);
  121. if (unlikely(q > end || q < p))
  122. return ERR_PTR(-EFAULT);
  123. memcpy(res, p, len);
  124. return q;
  125. }
  126. static inline const void *
  127. simple_get_netobj(const void *p, const void *end, struct xdr_netobj *dest)
  128. {
  129. const void *q;
  130. unsigned int len;
  131. p = simple_get_bytes(p, end, &len, sizeof(len));
  132. if (IS_ERR(p))
  133. return p;
  134. q = (const void *)((const char *)p + len);
  135. if (unlikely(q > end || q < p))
  136. return ERR_PTR(-EFAULT);
  137. dest->data = kmemdup(p, len, GFP_NOFS);
  138. if (unlikely(dest->data == NULL))
  139. return ERR_PTR(-ENOMEM);
  140. dest->len = len;
  141. return q;
  142. }
  143. static struct gss_cl_ctx *
  144. gss_cred_get_ctx(struct rpc_cred *cred)
  145. {
  146. struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
  147. struct gss_cl_ctx *ctx = NULL;
  148. rcu_read_lock();
  149. if (gss_cred->gc_ctx)
  150. ctx = gss_get_ctx(gss_cred->gc_ctx);
  151. rcu_read_unlock();
  152. return ctx;
  153. }
  154. static struct gss_cl_ctx *
  155. gss_alloc_context(void)
  156. {
  157. struct gss_cl_ctx *ctx;
  158. ctx = kzalloc(sizeof(*ctx), GFP_NOFS);
  159. if (ctx != NULL) {
  160. ctx->gc_proc = RPC_GSS_PROC_DATA;
  161. ctx->gc_seq = 1; /* NetApp 6.4R1 doesn't accept seq. no. 0 */
  162. spin_lock_init(&ctx->gc_seq_lock);
  163. atomic_set(&ctx->count,1);
  164. }
  165. return ctx;
  166. }
  167. #define GSSD_MIN_TIMEOUT (60 * 60)
  168. static const void *
  169. gss_fill_context(const void *p, const void *end, struct gss_cl_ctx *ctx, struct gss_api_mech *gm)
  170. {
  171. const void *q;
  172. unsigned int seclen;
  173. unsigned int timeout;
  174. unsigned long now = jiffies;
  175. u32 window_size;
  176. int ret;
  177. /* First unsigned int gives the remaining lifetime in seconds of the
  178. * credential - e.g. the remaining TGT lifetime for Kerberos or
  179. * the -t value passed to GSSD.
  180. */
  181. p = simple_get_bytes(p, end, &timeout, sizeof(timeout));
  182. if (IS_ERR(p))
  183. goto err;
  184. if (timeout == 0)
  185. timeout = GSSD_MIN_TIMEOUT;
  186. ctx->gc_expiry = now + ((unsigned long)timeout * HZ);
  187. /* Sequence number window. Determines the maximum number of
  188. * simultaneous requests
  189. */
  190. p = simple_get_bytes(p, end, &window_size, sizeof(window_size));
  191. if (IS_ERR(p))
  192. goto err;
  193. ctx->gc_win = window_size;
  194. /* gssd signals an error by passing ctx->gc_win = 0: */
  195. if (ctx->gc_win == 0) {
  196. /*
  197. * in which case, p points to an error code. Anything other
  198. * than -EKEYEXPIRED gets converted to -EACCES.
  199. */
  200. p = simple_get_bytes(p, end, &ret, sizeof(ret));
  201. if (!IS_ERR(p))
  202. p = (ret == -EKEYEXPIRED) ? ERR_PTR(-EKEYEXPIRED) :
  203. ERR_PTR(-EACCES);
  204. goto err;
  205. }
  206. /* copy the opaque wire context */
  207. p = simple_get_netobj(p, end, &ctx->gc_wire_ctx);
  208. if (IS_ERR(p))
  209. goto err;
  210. /* import the opaque security context */
  211. p = simple_get_bytes(p, end, &seclen, sizeof(seclen));
  212. if (IS_ERR(p))
  213. goto err;
  214. q = (const void *)((const char *)p + seclen);
  215. if (unlikely(q > end || q < p)) {
  216. p = ERR_PTR(-EFAULT);
  217. goto err;
  218. }
  219. ret = gss_import_sec_context(p, seclen, gm, &ctx->gc_gss_ctx, NULL, GFP_NOFS);
  220. if (ret < 0) {
  221. p = ERR_PTR(ret);
  222. goto err;
  223. }
  224. dprintk("RPC: %s Success. gc_expiry %lu now %lu timeout %u\n",
  225. __func__, ctx->gc_expiry, now, timeout);
  226. return q;
  227. err:
  228. dprintk("RPC: %s returns error %ld\n", __func__, -PTR_ERR(p));
  229. return p;
  230. }
  231. #define UPCALL_BUF_LEN 128
  232. struct gss_upcall_msg {
  233. atomic_t count;
  234. kuid_t uid;
  235. struct rpc_pipe_msg msg;
  236. struct list_head list;
  237. struct gss_auth *auth;
  238. struct rpc_pipe *pipe;
  239. struct rpc_wait_queue rpc_waitqueue;
  240. wait_queue_head_t waitqueue;
  241. struct gss_cl_ctx *ctx;
  242. char databuf[UPCALL_BUF_LEN];
  243. };
  244. static int get_pipe_version(struct net *net)
  245. {
  246. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  247. int ret;
  248. spin_lock(&pipe_version_lock);
  249. if (sn->pipe_version >= 0) {
  250. atomic_inc(&sn->pipe_users);
  251. ret = sn->pipe_version;
  252. } else
  253. ret = -EAGAIN;
  254. spin_unlock(&pipe_version_lock);
  255. return ret;
  256. }
  257. static void put_pipe_version(struct net *net)
  258. {
  259. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  260. if (atomic_dec_and_lock(&sn->pipe_users, &pipe_version_lock)) {
  261. sn->pipe_version = -1;
  262. spin_unlock(&pipe_version_lock);
  263. }
  264. }
  265. static void
  266. gss_release_msg(struct gss_upcall_msg *gss_msg)
  267. {
  268. struct net *net = gss_msg->auth->net;
  269. if (!atomic_dec_and_test(&gss_msg->count))
  270. return;
  271. put_pipe_version(net);
  272. BUG_ON(!list_empty(&gss_msg->list));
  273. if (gss_msg->ctx != NULL)
  274. gss_put_ctx(gss_msg->ctx);
  275. rpc_destroy_wait_queue(&gss_msg->rpc_waitqueue);
  276. kfree(gss_msg);
  277. }
  278. static struct gss_upcall_msg *
  279. __gss_find_upcall(struct rpc_pipe *pipe, kuid_t uid)
  280. {
  281. struct gss_upcall_msg *pos;
  282. list_for_each_entry(pos, &pipe->in_downcall, list) {
  283. if (!uid_eq(pos->uid, uid))
  284. continue;
  285. atomic_inc(&pos->count);
  286. dprintk("RPC: %s found msg %p\n", __func__, pos);
  287. return pos;
  288. }
  289. dprintk("RPC: %s found nothing\n", __func__);
  290. return NULL;
  291. }
  292. /* Try to add an upcall to the pipefs queue.
  293. * If an upcall owned by our uid already exists, then we return a reference
  294. * to that upcall instead of adding the new upcall.
  295. */
  296. static inline struct gss_upcall_msg *
  297. gss_add_msg(struct gss_upcall_msg *gss_msg)
  298. {
  299. struct rpc_pipe *pipe = gss_msg->pipe;
  300. struct gss_upcall_msg *old;
  301. spin_lock(&pipe->lock);
  302. old = __gss_find_upcall(pipe, gss_msg->uid);
  303. if (old == NULL) {
  304. atomic_inc(&gss_msg->count);
  305. list_add(&gss_msg->list, &pipe->in_downcall);
  306. } else
  307. gss_msg = old;
  308. spin_unlock(&pipe->lock);
  309. return gss_msg;
  310. }
  311. static void
  312. __gss_unhash_msg(struct gss_upcall_msg *gss_msg)
  313. {
  314. list_del_init(&gss_msg->list);
  315. rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
  316. wake_up_all(&gss_msg->waitqueue);
  317. atomic_dec(&gss_msg->count);
  318. }
  319. static void
  320. gss_unhash_msg(struct gss_upcall_msg *gss_msg)
  321. {
  322. struct rpc_pipe *pipe = gss_msg->pipe;
  323. if (list_empty(&gss_msg->list))
  324. return;
  325. spin_lock(&pipe->lock);
  326. if (!list_empty(&gss_msg->list))
  327. __gss_unhash_msg(gss_msg);
  328. spin_unlock(&pipe->lock);
  329. }
  330. static void
  331. gss_handle_downcall_result(struct gss_cred *gss_cred, struct gss_upcall_msg *gss_msg)
  332. {
  333. switch (gss_msg->msg.errno) {
  334. case 0:
  335. if (gss_msg->ctx == NULL)
  336. break;
  337. clear_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
  338. gss_cred_set_ctx(&gss_cred->gc_base, gss_msg->ctx);
  339. break;
  340. case -EKEYEXPIRED:
  341. set_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
  342. }
  343. gss_cred->gc_upcall_timestamp = jiffies;
  344. gss_cred->gc_upcall = NULL;
  345. rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
  346. }
  347. static void
  348. gss_upcall_callback(struct rpc_task *task)
  349. {
  350. struct gss_cred *gss_cred = container_of(task->tk_rqstp->rq_cred,
  351. struct gss_cred, gc_base);
  352. struct gss_upcall_msg *gss_msg = gss_cred->gc_upcall;
  353. struct rpc_pipe *pipe = gss_msg->pipe;
  354. spin_lock(&pipe->lock);
  355. gss_handle_downcall_result(gss_cred, gss_msg);
  356. spin_unlock(&pipe->lock);
  357. task->tk_status = gss_msg->msg.errno;
  358. gss_release_msg(gss_msg);
  359. }
  360. static void gss_encode_v0_msg(struct gss_upcall_msg *gss_msg)
  361. {
  362. uid_t uid = from_kuid(&init_user_ns, gss_msg->uid);
  363. memcpy(gss_msg->databuf, &uid, sizeof(uid));
  364. gss_msg->msg.data = gss_msg->databuf;
  365. gss_msg->msg.len = sizeof(uid);
  366. BUG_ON(sizeof(uid) > UPCALL_BUF_LEN);
  367. }
  368. static void gss_encode_v1_msg(struct gss_upcall_msg *gss_msg,
  369. const char *service_name,
  370. const char *target_name)
  371. {
  372. struct gss_api_mech *mech = gss_msg->auth->mech;
  373. char *p = gss_msg->databuf;
  374. int len = 0;
  375. gss_msg->msg.len = sprintf(gss_msg->databuf, "mech=%s uid=%d ",
  376. mech->gm_name,
  377. from_kuid(&init_user_ns, gss_msg->uid));
  378. p += gss_msg->msg.len;
  379. if (target_name) {
  380. len = sprintf(p, "target=%s ", target_name);
  381. p += len;
  382. gss_msg->msg.len += len;
  383. }
  384. if (service_name != NULL) {
  385. len = sprintf(p, "service=%s ", service_name);
  386. p += len;
  387. gss_msg->msg.len += len;
  388. }
  389. if (mech->gm_upcall_enctypes) {
  390. len = sprintf(p, "enctypes=%s ", mech->gm_upcall_enctypes);
  391. p += len;
  392. gss_msg->msg.len += len;
  393. }
  394. len = sprintf(p, "\n");
  395. gss_msg->msg.len += len;
  396. gss_msg->msg.data = gss_msg->databuf;
  397. BUG_ON(gss_msg->msg.len > UPCALL_BUF_LEN);
  398. }
  399. static struct gss_upcall_msg *
  400. gss_alloc_msg(struct gss_auth *gss_auth,
  401. kuid_t uid, const char *service_name)
  402. {
  403. struct gss_upcall_msg *gss_msg;
  404. int vers;
  405. gss_msg = kzalloc(sizeof(*gss_msg), GFP_NOFS);
  406. if (gss_msg == NULL)
  407. return ERR_PTR(-ENOMEM);
  408. vers = get_pipe_version(gss_auth->net);
  409. if (vers < 0) {
  410. kfree(gss_msg);
  411. return ERR_PTR(vers);
  412. }
  413. gss_msg->pipe = gss_auth->pipe[vers];
  414. INIT_LIST_HEAD(&gss_msg->list);
  415. rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
  416. init_waitqueue_head(&gss_msg->waitqueue);
  417. atomic_set(&gss_msg->count, 1);
  418. gss_msg->uid = uid;
  419. gss_msg->auth = gss_auth;
  420. switch (vers) {
  421. case 0:
  422. gss_encode_v0_msg(gss_msg);
  423. default:
  424. gss_encode_v1_msg(gss_msg, service_name, gss_auth->target_name);
  425. };
  426. return gss_msg;
  427. }
  428. static struct gss_upcall_msg *
  429. gss_setup_upcall(struct gss_auth *gss_auth, struct rpc_cred *cred)
  430. {
  431. struct gss_cred *gss_cred = container_of(cred,
  432. struct gss_cred, gc_base);
  433. struct gss_upcall_msg *gss_new, *gss_msg;
  434. kuid_t uid = cred->cr_uid;
  435. gss_new = gss_alloc_msg(gss_auth, uid, gss_cred->gc_principal);
  436. if (IS_ERR(gss_new))
  437. return gss_new;
  438. gss_msg = gss_add_msg(gss_new);
  439. if (gss_msg == gss_new) {
  440. int res = rpc_queue_upcall(gss_new->pipe, &gss_new->msg);
  441. if (res) {
  442. gss_unhash_msg(gss_new);
  443. gss_msg = ERR_PTR(res);
  444. }
  445. } else
  446. gss_release_msg(gss_new);
  447. return gss_msg;
  448. }
  449. static void warn_gssd(void)
  450. {
  451. static unsigned long ratelimit;
  452. unsigned long now = jiffies;
  453. if (time_after(now, ratelimit)) {
  454. printk(KERN_WARNING "RPC: AUTH_GSS upcall timed out.\n"
  455. "Please check user daemon is running.\n");
  456. ratelimit = now + 15*HZ;
  457. }
  458. }
  459. static inline int
  460. gss_refresh_upcall(struct rpc_task *task)
  461. {
  462. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  463. struct gss_auth *gss_auth = container_of(cred->cr_auth,
  464. struct gss_auth, rpc_auth);
  465. struct gss_cred *gss_cred = container_of(cred,
  466. struct gss_cred, gc_base);
  467. struct gss_upcall_msg *gss_msg;
  468. struct rpc_pipe *pipe;
  469. int err = 0;
  470. dprintk("RPC: %5u %s for uid %u\n",
  471. task->tk_pid, __func__, from_kuid(&init_user_ns, cred->cr_uid));
  472. gss_msg = gss_setup_upcall(gss_auth, cred);
  473. if (PTR_ERR(gss_msg) == -EAGAIN) {
  474. /* XXX: warning on the first, under the assumption we
  475. * shouldn't normally hit this case on a refresh. */
  476. warn_gssd();
  477. task->tk_timeout = 15*HZ;
  478. rpc_sleep_on(&pipe_version_rpc_waitqueue, task, NULL);
  479. return -EAGAIN;
  480. }
  481. if (IS_ERR(gss_msg)) {
  482. err = PTR_ERR(gss_msg);
  483. goto out;
  484. }
  485. pipe = gss_msg->pipe;
  486. spin_lock(&pipe->lock);
  487. if (gss_cred->gc_upcall != NULL)
  488. rpc_sleep_on(&gss_cred->gc_upcall->rpc_waitqueue, task, NULL);
  489. else if (gss_msg->ctx == NULL && gss_msg->msg.errno >= 0) {
  490. task->tk_timeout = 0;
  491. gss_cred->gc_upcall = gss_msg;
  492. /* gss_upcall_callback will release the reference to gss_upcall_msg */
  493. atomic_inc(&gss_msg->count);
  494. rpc_sleep_on(&gss_msg->rpc_waitqueue, task, gss_upcall_callback);
  495. } else {
  496. gss_handle_downcall_result(gss_cred, gss_msg);
  497. err = gss_msg->msg.errno;
  498. }
  499. spin_unlock(&pipe->lock);
  500. gss_release_msg(gss_msg);
  501. out:
  502. dprintk("RPC: %5u %s for uid %u result %d\n",
  503. task->tk_pid, __func__,
  504. from_kuid(&init_user_ns, cred->cr_uid), err);
  505. return err;
  506. }
  507. static inline int
  508. gss_create_upcall(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
  509. {
  510. struct net *net = gss_auth->net;
  511. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  512. struct rpc_pipe *pipe;
  513. struct rpc_cred *cred = &gss_cred->gc_base;
  514. struct gss_upcall_msg *gss_msg;
  515. unsigned long timeout;
  516. DEFINE_WAIT(wait);
  517. int err;
  518. dprintk("RPC: %s for uid %u\n",
  519. __func__, from_kuid(&init_user_ns, cred->cr_uid));
  520. retry:
  521. err = 0;
  522. /* Default timeout is 15s unless we know that gssd is not running */
  523. timeout = 15 * HZ;
  524. if (!sn->gssd_running)
  525. timeout = HZ >> 2;
  526. gss_msg = gss_setup_upcall(gss_auth, cred);
  527. if (PTR_ERR(gss_msg) == -EAGAIN) {
  528. err = wait_event_interruptible_timeout(pipe_version_waitqueue,
  529. sn->pipe_version >= 0, timeout);
  530. if (sn->pipe_version < 0) {
  531. if (err == 0)
  532. sn->gssd_running = 0;
  533. warn_gssd();
  534. err = -EACCES;
  535. }
  536. if (err < 0)
  537. goto out;
  538. goto retry;
  539. }
  540. if (IS_ERR(gss_msg)) {
  541. err = PTR_ERR(gss_msg);
  542. goto out;
  543. }
  544. pipe = gss_msg->pipe;
  545. for (;;) {
  546. prepare_to_wait(&gss_msg->waitqueue, &wait, TASK_KILLABLE);
  547. spin_lock(&pipe->lock);
  548. if (gss_msg->ctx != NULL || gss_msg->msg.errno < 0) {
  549. break;
  550. }
  551. spin_unlock(&pipe->lock);
  552. if (fatal_signal_pending(current)) {
  553. err = -ERESTARTSYS;
  554. goto out_intr;
  555. }
  556. schedule();
  557. }
  558. if (gss_msg->ctx)
  559. gss_cred_set_ctx(cred, gss_msg->ctx);
  560. else
  561. err = gss_msg->msg.errno;
  562. spin_unlock(&pipe->lock);
  563. out_intr:
  564. finish_wait(&gss_msg->waitqueue, &wait);
  565. gss_release_msg(gss_msg);
  566. out:
  567. dprintk("RPC: %s for uid %u result %d\n",
  568. __func__, from_kuid(&init_user_ns, cred->cr_uid), err);
  569. return err;
  570. }
  571. #define MSG_BUF_MAXSIZE 1024
  572. static ssize_t
  573. gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  574. {
  575. const void *p, *end;
  576. void *buf;
  577. struct gss_upcall_msg *gss_msg;
  578. struct rpc_pipe *pipe = RPC_I(file_inode(filp))->pipe;
  579. struct gss_cl_ctx *ctx;
  580. uid_t id;
  581. kuid_t uid;
  582. ssize_t err = -EFBIG;
  583. if (mlen > MSG_BUF_MAXSIZE)
  584. goto out;
  585. err = -ENOMEM;
  586. buf = kmalloc(mlen, GFP_NOFS);
  587. if (!buf)
  588. goto out;
  589. err = -EFAULT;
  590. if (copy_from_user(buf, src, mlen))
  591. goto err;
  592. end = (const void *)((char *)buf + mlen);
  593. p = simple_get_bytes(buf, end, &id, sizeof(id));
  594. if (IS_ERR(p)) {
  595. err = PTR_ERR(p);
  596. goto err;
  597. }
  598. uid = make_kuid(&init_user_ns, id);
  599. if (!uid_valid(uid)) {
  600. err = -EINVAL;
  601. goto err;
  602. }
  603. err = -ENOMEM;
  604. ctx = gss_alloc_context();
  605. if (ctx == NULL)
  606. goto err;
  607. err = -ENOENT;
  608. /* Find a matching upcall */
  609. spin_lock(&pipe->lock);
  610. gss_msg = __gss_find_upcall(pipe, uid);
  611. if (gss_msg == NULL) {
  612. spin_unlock(&pipe->lock);
  613. goto err_put_ctx;
  614. }
  615. list_del_init(&gss_msg->list);
  616. spin_unlock(&pipe->lock);
  617. p = gss_fill_context(p, end, ctx, gss_msg->auth->mech);
  618. if (IS_ERR(p)) {
  619. err = PTR_ERR(p);
  620. switch (err) {
  621. case -EACCES:
  622. case -EKEYEXPIRED:
  623. gss_msg->msg.errno = err;
  624. err = mlen;
  625. break;
  626. case -EFAULT:
  627. case -ENOMEM:
  628. case -EINVAL:
  629. case -ENOSYS:
  630. gss_msg->msg.errno = -EAGAIN;
  631. break;
  632. default:
  633. printk(KERN_CRIT "%s: bad return from "
  634. "gss_fill_context: %zd\n", __func__, err);
  635. BUG();
  636. }
  637. goto err_release_msg;
  638. }
  639. gss_msg->ctx = gss_get_ctx(ctx);
  640. err = mlen;
  641. err_release_msg:
  642. spin_lock(&pipe->lock);
  643. __gss_unhash_msg(gss_msg);
  644. spin_unlock(&pipe->lock);
  645. gss_release_msg(gss_msg);
  646. err_put_ctx:
  647. gss_put_ctx(ctx);
  648. err:
  649. kfree(buf);
  650. out:
  651. dprintk("RPC: %s returning %Zd\n", __func__, err);
  652. return err;
  653. }
  654. static int gss_pipe_open(struct inode *inode, int new_version)
  655. {
  656. struct net *net = inode->i_sb->s_fs_info;
  657. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  658. int ret = 0;
  659. spin_lock(&pipe_version_lock);
  660. if (sn->pipe_version < 0) {
  661. /* First open of any gss pipe determines the version: */
  662. sn->pipe_version = new_version;
  663. rpc_wake_up(&pipe_version_rpc_waitqueue);
  664. wake_up(&pipe_version_waitqueue);
  665. } else if (sn->pipe_version != new_version) {
  666. /* Trying to open a pipe of a different version */
  667. ret = -EBUSY;
  668. goto out;
  669. }
  670. atomic_inc(&sn->pipe_users);
  671. out:
  672. spin_unlock(&pipe_version_lock);
  673. return ret;
  674. }
  675. static int gss_pipe_open_v0(struct inode *inode)
  676. {
  677. return gss_pipe_open(inode, 0);
  678. }
  679. static int gss_pipe_open_v1(struct inode *inode)
  680. {
  681. return gss_pipe_open(inode, 1);
  682. }
  683. static void
  684. gss_pipe_release(struct inode *inode)
  685. {
  686. struct net *net = inode->i_sb->s_fs_info;
  687. struct rpc_pipe *pipe = RPC_I(inode)->pipe;
  688. struct gss_upcall_msg *gss_msg;
  689. restart:
  690. spin_lock(&pipe->lock);
  691. list_for_each_entry(gss_msg, &pipe->in_downcall, list) {
  692. if (!list_empty(&gss_msg->msg.list))
  693. continue;
  694. gss_msg->msg.errno = -EPIPE;
  695. atomic_inc(&gss_msg->count);
  696. __gss_unhash_msg(gss_msg);
  697. spin_unlock(&pipe->lock);
  698. gss_release_msg(gss_msg);
  699. goto restart;
  700. }
  701. spin_unlock(&pipe->lock);
  702. put_pipe_version(net);
  703. }
  704. static void
  705. gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  706. {
  707. struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
  708. if (msg->errno < 0) {
  709. dprintk("RPC: %s releasing msg %p\n",
  710. __func__, gss_msg);
  711. atomic_inc(&gss_msg->count);
  712. gss_unhash_msg(gss_msg);
  713. if (msg->errno == -ETIMEDOUT)
  714. warn_gssd();
  715. gss_release_msg(gss_msg);
  716. }
  717. }
  718. static void gss_pipes_dentries_destroy(struct rpc_auth *auth)
  719. {
  720. struct gss_auth *gss_auth;
  721. gss_auth = container_of(auth, struct gss_auth, rpc_auth);
  722. if (gss_auth->pipe[0]->dentry) {
  723. rpc_unlink(gss_auth->pipe[0]->dentry);
  724. gss_auth->pipe[0]->dentry = NULL;
  725. }
  726. if (gss_auth->pipe[1]->dentry) {
  727. rpc_unlink(gss_auth->pipe[1]->dentry);
  728. gss_auth->pipe[1]->dentry = NULL;
  729. }
  730. }
  731. static int gss_pipes_dentries_create(struct rpc_auth *auth)
  732. {
  733. int err;
  734. struct gss_auth *gss_auth;
  735. struct rpc_clnt *clnt;
  736. struct dentry *dentry;
  737. gss_auth = container_of(auth, struct gss_auth, rpc_auth);
  738. clnt = gss_auth->client;
  739. dentry = rpc_mkpipe_dentry(clnt->cl_dentry, "gssd",
  740. clnt, gss_auth->pipe[1]);
  741. if (IS_ERR(dentry)) {
  742. err = PTR_ERR(dentry);
  743. goto err;
  744. }
  745. gss_auth->pipe[1]->dentry = dentry;
  746. dentry = rpc_mkpipe_dentry(clnt->cl_dentry, gss_auth->mech->gm_name,
  747. clnt, gss_auth->pipe[0]);
  748. if (IS_ERR(dentry)) {
  749. err = PTR_ERR(dentry);
  750. goto err_unlink_pipe_1;
  751. }
  752. return 0;
  753. err_unlink_pipe_1:
  754. rpc_unlink(gss_auth->pipe[1]->dentry);
  755. gss_auth->pipe[1]->dentry = NULL;
  756. err:
  757. return err;
  758. }
  759. static void gss_pipes_dentries_destroy_net(struct rpc_clnt *clnt,
  760. struct rpc_auth *auth)
  761. {
  762. struct gss_auth *gss_auth = container_of(auth, struct gss_auth,
  763. rpc_auth);
  764. struct net *net = gss_auth->net;
  765. struct super_block *sb;
  766. sb = rpc_get_sb_net(net);
  767. if (sb) {
  768. if (clnt->cl_dentry)
  769. gss_pipes_dentries_destroy(auth);
  770. rpc_put_sb_net(net);
  771. }
  772. }
  773. static int gss_pipes_dentries_create_net(struct rpc_clnt *clnt,
  774. struct rpc_auth *auth)
  775. {
  776. struct gss_auth *gss_auth = container_of(auth, struct gss_auth,
  777. rpc_auth);
  778. struct net *net = gss_auth->net;
  779. struct super_block *sb;
  780. int err = 0;
  781. sb = rpc_get_sb_net(net);
  782. if (sb) {
  783. if (clnt->cl_dentry)
  784. err = gss_pipes_dentries_create(auth);
  785. rpc_put_sb_net(net);
  786. }
  787. return err;
  788. }
  789. /*
  790. * NOTE: we have the opportunity to use different
  791. * parameters based on the input flavor (which must be a pseudoflavor)
  792. */
  793. static struct rpc_auth *
  794. gss_create(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
  795. {
  796. rpc_authflavor_t flavor = args->pseudoflavor;
  797. struct gss_auth *gss_auth;
  798. struct rpc_auth * auth;
  799. int err = -ENOMEM; /* XXX? */
  800. dprintk("RPC: creating GSS authenticator for client %p\n", clnt);
  801. if (!try_module_get(THIS_MODULE))
  802. return ERR_PTR(err);
  803. if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
  804. goto out_dec;
  805. gss_auth->target_name = NULL;
  806. if (args->target_name) {
  807. gss_auth->target_name = kstrdup(args->target_name, GFP_KERNEL);
  808. if (gss_auth->target_name == NULL)
  809. goto err_free;
  810. }
  811. gss_auth->client = clnt;
  812. gss_auth->net = get_net(rpc_net_ns(clnt));
  813. err = -EINVAL;
  814. gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
  815. if (!gss_auth->mech) {
  816. dprintk("RPC: Pseudoflavor %d not found!\n", flavor);
  817. goto err_put_net;
  818. }
  819. gss_auth->service = gss_pseudoflavor_to_service(gss_auth->mech, flavor);
  820. if (gss_auth->service == 0)
  821. goto err_put_mech;
  822. auth = &gss_auth->rpc_auth;
  823. auth->au_cslack = GSS_CRED_SLACK >> 2;
  824. auth->au_rslack = GSS_VERF_SLACK >> 2;
  825. auth->au_ops = &authgss_ops;
  826. auth->au_flavor = flavor;
  827. atomic_set(&auth->au_count, 1);
  828. kref_init(&gss_auth->kref);
  829. /*
  830. * Note: if we created the old pipe first, then someone who
  831. * examined the directory at the right moment might conclude
  832. * that we supported only the old pipe. So we instead create
  833. * the new pipe first.
  834. */
  835. gss_auth->pipe[1] = rpc_mkpipe_data(&gss_upcall_ops_v1,
  836. RPC_PIPE_WAIT_FOR_OPEN);
  837. if (IS_ERR(gss_auth->pipe[1])) {
  838. err = PTR_ERR(gss_auth->pipe[1]);
  839. goto err_put_mech;
  840. }
  841. gss_auth->pipe[0] = rpc_mkpipe_data(&gss_upcall_ops_v0,
  842. RPC_PIPE_WAIT_FOR_OPEN);
  843. if (IS_ERR(gss_auth->pipe[0])) {
  844. err = PTR_ERR(gss_auth->pipe[0]);
  845. goto err_destroy_pipe_1;
  846. }
  847. err = gss_pipes_dentries_create_net(clnt, auth);
  848. if (err)
  849. goto err_destroy_pipe_0;
  850. err = rpcauth_init_credcache(auth);
  851. if (err)
  852. goto err_unlink_pipes;
  853. return auth;
  854. err_unlink_pipes:
  855. gss_pipes_dentries_destroy_net(clnt, auth);
  856. err_destroy_pipe_0:
  857. rpc_destroy_pipe_data(gss_auth->pipe[0]);
  858. err_destroy_pipe_1:
  859. rpc_destroy_pipe_data(gss_auth->pipe[1]);
  860. err_put_mech:
  861. gss_mech_put(gss_auth->mech);
  862. err_put_net:
  863. put_net(gss_auth->net);
  864. err_free:
  865. kfree(gss_auth->target_name);
  866. kfree(gss_auth);
  867. out_dec:
  868. module_put(THIS_MODULE);
  869. return ERR_PTR(err);
  870. }
  871. static void
  872. gss_free(struct gss_auth *gss_auth)
  873. {
  874. gss_pipes_dentries_destroy_net(gss_auth->client, &gss_auth->rpc_auth);
  875. rpc_destroy_pipe_data(gss_auth->pipe[0]);
  876. rpc_destroy_pipe_data(gss_auth->pipe[1]);
  877. gss_mech_put(gss_auth->mech);
  878. put_net(gss_auth->net);
  879. kfree(gss_auth->target_name);
  880. kfree(gss_auth);
  881. module_put(THIS_MODULE);
  882. }
  883. static void
  884. gss_free_callback(struct kref *kref)
  885. {
  886. struct gss_auth *gss_auth = container_of(kref, struct gss_auth, kref);
  887. gss_free(gss_auth);
  888. }
  889. static void
  890. gss_destroy(struct rpc_auth *auth)
  891. {
  892. struct gss_auth *gss_auth;
  893. dprintk("RPC: destroying GSS authenticator %p flavor %d\n",
  894. auth, auth->au_flavor);
  895. rpcauth_destroy_credcache(auth);
  896. gss_auth = container_of(auth, struct gss_auth, rpc_auth);
  897. kref_put(&gss_auth->kref, gss_free_callback);
  898. }
  899. /*
  900. * gss_destroying_context will cause the RPCSEC_GSS to send a NULL RPC call
  901. * to the server with the GSS control procedure field set to
  902. * RPC_GSS_PROC_DESTROY. This should normally cause the server to release
  903. * all RPCSEC_GSS state associated with that context.
  904. */
  905. static int
  906. gss_destroying_context(struct rpc_cred *cred)
  907. {
  908. struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
  909. struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
  910. struct rpc_task *task;
  911. if (gss_cred->gc_ctx == NULL ||
  912. test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0)
  913. return 0;
  914. gss_cred->gc_ctx->gc_proc = RPC_GSS_PROC_DESTROY;
  915. cred->cr_ops = &gss_nullops;
  916. /* Take a reference to ensure the cred will be destroyed either
  917. * by the RPC call or by the put_rpccred() below */
  918. get_rpccred(cred);
  919. task = rpc_call_null(gss_auth->client, cred, RPC_TASK_ASYNC|RPC_TASK_SOFT);
  920. if (!IS_ERR(task))
  921. rpc_put_task(task);
  922. put_rpccred(cred);
  923. return 1;
  924. }
  925. /* gss_destroy_cred (and gss_free_ctx) are used to clean up after failure
  926. * to create a new cred or context, so they check that things have been
  927. * allocated before freeing them. */
  928. static void
  929. gss_do_free_ctx(struct gss_cl_ctx *ctx)
  930. {
  931. dprintk("RPC: %s\n", __func__);
  932. gss_delete_sec_context(&ctx->gc_gss_ctx);
  933. kfree(ctx->gc_wire_ctx.data);
  934. kfree(ctx);
  935. }
  936. static void
  937. gss_free_ctx_callback(struct rcu_head *head)
  938. {
  939. struct gss_cl_ctx *ctx = container_of(head, struct gss_cl_ctx, gc_rcu);
  940. gss_do_free_ctx(ctx);
  941. }
  942. static void
  943. gss_free_ctx(struct gss_cl_ctx *ctx)
  944. {
  945. call_rcu(&ctx->gc_rcu, gss_free_ctx_callback);
  946. }
  947. static void
  948. gss_free_cred(struct gss_cred *gss_cred)
  949. {
  950. dprintk("RPC: %s cred=%p\n", __func__, gss_cred);
  951. kfree(gss_cred);
  952. }
  953. static void
  954. gss_free_cred_callback(struct rcu_head *head)
  955. {
  956. struct gss_cred *gss_cred = container_of(head, struct gss_cred, gc_base.cr_rcu);
  957. gss_free_cred(gss_cred);
  958. }
  959. static void
  960. gss_destroy_nullcred(struct rpc_cred *cred)
  961. {
  962. struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
  963. struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
  964. struct gss_cl_ctx *ctx = gss_cred->gc_ctx;
  965. RCU_INIT_POINTER(gss_cred->gc_ctx, NULL);
  966. call_rcu(&cred->cr_rcu, gss_free_cred_callback);
  967. if (ctx)
  968. gss_put_ctx(ctx);
  969. kref_put(&gss_auth->kref, gss_free_callback);
  970. }
  971. static void
  972. gss_destroy_cred(struct rpc_cred *cred)
  973. {
  974. if (gss_destroying_context(cred))
  975. return;
  976. gss_destroy_nullcred(cred);
  977. }
  978. /*
  979. * Lookup RPCSEC_GSS cred for the current process
  980. */
  981. static struct rpc_cred *
  982. gss_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  983. {
  984. return rpcauth_lookup_credcache(auth, acred, flags);
  985. }
  986. static struct rpc_cred *
  987. gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  988. {
  989. struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
  990. struct gss_cred *cred = NULL;
  991. int err = -ENOMEM;
  992. dprintk("RPC: %s for uid %d, flavor %d\n",
  993. __func__, from_kuid(&init_user_ns, acred->uid),
  994. auth->au_flavor);
  995. if (!(cred = kzalloc(sizeof(*cred), GFP_NOFS)))
  996. goto out_err;
  997. rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops);
  998. /*
  999. * Note: in order to force a call to call_refresh(), we deliberately
  1000. * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
  1001. */
  1002. cred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_NEW;
  1003. cred->gc_service = gss_auth->service;
  1004. cred->gc_principal = NULL;
  1005. if (acred->machine_cred)
  1006. cred->gc_principal = acred->principal;
  1007. kref_get(&gss_auth->kref);
  1008. return &cred->gc_base;
  1009. out_err:
  1010. dprintk("RPC: %s failed with error %d\n", __func__, err);
  1011. return ERR_PTR(err);
  1012. }
  1013. static int
  1014. gss_cred_init(struct rpc_auth *auth, struct rpc_cred *cred)
  1015. {
  1016. struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
  1017. struct gss_cred *gss_cred = container_of(cred,struct gss_cred, gc_base);
  1018. int err;
  1019. do {
  1020. err = gss_create_upcall(gss_auth, gss_cred);
  1021. } while (err == -EAGAIN);
  1022. return err;
  1023. }
  1024. static int
  1025. gss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags)
  1026. {
  1027. struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
  1028. if (test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags))
  1029. goto out;
  1030. /* Don't match with creds that have expired. */
  1031. if (time_after(jiffies, gss_cred->gc_ctx->gc_expiry))
  1032. return 0;
  1033. if (!test_bit(RPCAUTH_CRED_UPTODATE, &rc->cr_flags))
  1034. return 0;
  1035. out:
  1036. if (acred->principal != NULL) {
  1037. if (gss_cred->gc_principal == NULL)
  1038. return 0;
  1039. return strcmp(acred->principal, gss_cred->gc_principal) == 0;
  1040. }
  1041. if (gss_cred->gc_principal != NULL)
  1042. return 0;
  1043. return uid_eq(rc->cr_uid, acred->uid);
  1044. }
  1045. /*
  1046. * Marshal credentials.
  1047. * Maybe we should keep a cached credential for performance reasons.
  1048. */
  1049. static __be32 *
  1050. gss_marshal(struct rpc_task *task, __be32 *p)
  1051. {
  1052. struct rpc_rqst *req = task->tk_rqstp;
  1053. struct rpc_cred *cred = req->rq_cred;
  1054. struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
  1055. gc_base);
  1056. struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
  1057. __be32 *cred_len;
  1058. u32 maj_stat = 0;
  1059. struct xdr_netobj mic;
  1060. struct kvec iov;
  1061. struct xdr_buf verf_buf;
  1062. dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
  1063. *p++ = htonl(RPC_AUTH_GSS);
  1064. cred_len = p++;
  1065. spin_lock(&ctx->gc_seq_lock);
  1066. req->rq_seqno = ctx->gc_seq++;
  1067. spin_unlock(&ctx->gc_seq_lock);
  1068. *p++ = htonl((u32) RPC_GSS_VERSION);
  1069. *p++ = htonl((u32) ctx->gc_proc);
  1070. *p++ = htonl((u32) req->rq_seqno);
  1071. *p++ = htonl((u32) gss_cred->gc_service);
  1072. p = xdr_encode_netobj(p, &ctx->gc_wire_ctx);
  1073. *cred_len = htonl((p - (cred_len + 1)) << 2);
  1074. /* We compute the checksum for the verifier over the xdr-encoded bytes
  1075. * starting with the xid and ending at the end of the credential: */
  1076. iov.iov_base = xprt_skip_transport_header(req->rq_xprt,
  1077. req->rq_snd_buf.head[0].iov_base);
  1078. iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
  1079. xdr_buf_from_iov(&iov, &verf_buf);
  1080. /* set verifier flavor*/
  1081. *p++ = htonl(RPC_AUTH_GSS);
  1082. mic.data = (u8 *)(p + 1);
  1083. maj_stat = gss_get_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
  1084. if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
  1085. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1086. } else if (maj_stat != 0) {
  1087. printk("gss_marshal: gss_get_mic FAILED (%d)\n", maj_stat);
  1088. goto out_put_ctx;
  1089. }
  1090. p = xdr_encode_opaque(p, NULL, mic.len);
  1091. gss_put_ctx(ctx);
  1092. return p;
  1093. out_put_ctx:
  1094. gss_put_ctx(ctx);
  1095. return NULL;
  1096. }
  1097. static int gss_renew_cred(struct rpc_task *task)
  1098. {
  1099. struct rpc_cred *oldcred = task->tk_rqstp->rq_cred;
  1100. struct gss_cred *gss_cred = container_of(oldcred,
  1101. struct gss_cred,
  1102. gc_base);
  1103. struct rpc_auth *auth = oldcred->cr_auth;
  1104. struct auth_cred acred = {
  1105. .uid = oldcred->cr_uid,
  1106. .principal = gss_cred->gc_principal,
  1107. .machine_cred = (gss_cred->gc_principal != NULL ? 1 : 0),
  1108. };
  1109. struct rpc_cred *new;
  1110. new = gss_lookup_cred(auth, &acred, RPCAUTH_LOOKUP_NEW);
  1111. if (IS_ERR(new))
  1112. return PTR_ERR(new);
  1113. task->tk_rqstp->rq_cred = new;
  1114. put_rpccred(oldcred);
  1115. return 0;
  1116. }
  1117. static int gss_cred_is_negative_entry(struct rpc_cred *cred)
  1118. {
  1119. if (test_bit(RPCAUTH_CRED_NEGATIVE, &cred->cr_flags)) {
  1120. unsigned long now = jiffies;
  1121. unsigned long begin, expire;
  1122. struct gss_cred *gss_cred;
  1123. gss_cred = container_of(cred, struct gss_cred, gc_base);
  1124. begin = gss_cred->gc_upcall_timestamp;
  1125. expire = begin + gss_expired_cred_retry_delay * HZ;
  1126. if (time_in_range_open(now, begin, expire))
  1127. return 1;
  1128. }
  1129. return 0;
  1130. }
  1131. /*
  1132. * Refresh credentials. XXX - finish
  1133. */
  1134. static int
  1135. gss_refresh(struct rpc_task *task)
  1136. {
  1137. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  1138. int ret = 0;
  1139. if (gss_cred_is_negative_entry(cred))
  1140. return -EKEYEXPIRED;
  1141. if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
  1142. !test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags)) {
  1143. ret = gss_renew_cred(task);
  1144. if (ret < 0)
  1145. goto out;
  1146. cred = task->tk_rqstp->rq_cred;
  1147. }
  1148. if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
  1149. ret = gss_refresh_upcall(task);
  1150. out:
  1151. return ret;
  1152. }
  1153. /* Dummy refresh routine: used only when destroying the context */
  1154. static int
  1155. gss_refresh_null(struct rpc_task *task)
  1156. {
  1157. return -EACCES;
  1158. }
  1159. static __be32 *
  1160. gss_validate(struct rpc_task *task, __be32 *p)
  1161. {
  1162. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  1163. struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
  1164. __be32 seq;
  1165. struct kvec iov;
  1166. struct xdr_buf verf_buf;
  1167. struct xdr_netobj mic;
  1168. u32 flav,len;
  1169. u32 maj_stat;
  1170. dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
  1171. flav = ntohl(*p++);
  1172. if ((len = ntohl(*p++)) > RPC_MAX_AUTH_SIZE)
  1173. goto out_bad;
  1174. if (flav != RPC_AUTH_GSS)
  1175. goto out_bad;
  1176. seq = htonl(task->tk_rqstp->rq_seqno);
  1177. iov.iov_base = &seq;
  1178. iov.iov_len = sizeof(seq);
  1179. xdr_buf_from_iov(&iov, &verf_buf);
  1180. mic.data = (u8 *)p;
  1181. mic.len = len;
  1182. maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
  1183. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  1184. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1185. if (maj_stat) {
  1186. dprintk("RPC: %5u %s: gss_verify_mic returned error 0x%08x\n",
  1187. task->tk_pid, __func__, maj_stat);
  1188. goto out_bad;
  1189. }
  1190. /* We leave it to unwrap to calculate au_rslack. For now we just
  1191. * calculate the length of the verifier: */
  1192. cred->cr_auth->au_verfsize = XDR_QUADLEN(len) + 2;
  1193. gss_put_ctx(ctx);
  1194. dprintk("RPC: %5u %s: gss_verify_mic succeeded.\n",
  1195. task->tk_pid, __func__);
  1196. return p + XDR_QUADLEN(len);
  1197. out_bad:
  1198. gss_put_ctx(ctx);
  1199. dprintk("RPC: %5u %s failed.\n", task->tk_pid, __func__);
  1200. return NULL;
  1201. }
  1202. static void gss_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
  1203. __be32 *p, void *obj)
  1204. {
  1205. struct xdr_stream xdr;
  1206. xdr_init_encode(&xdr, &rqstp->rq_snd_buf, p);
  1207. encode(rqstp, &xdr, obj);
  1208. }
  1209. static inline int
  1210. gss_wrap_req_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
  1211. kxdreproc_t encode, struct rpc_rqst *rqstp,
  1212. __be32 *p, void *obj)
  1213. {
  1214. struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
  1215. struct xdr_buf integ_buf;
  1216. __be32 *integ_len = NULL;
  1217. struct xdr_netobj mic;
  1218. u32 offset;
  1219. __be32 *q;
  1220. struct kvec *iov;
  1221. u32 maj_stat = 0;
  1222. int status = -EIO;
  1223. integ_len = p++;
  1224. offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
  1225. *p++ = htonl(rqstp->rq_seqno);
  1226. gss_wrap_req_encode(encode, rqstp, p, obj);
  1227. if (xdr_buf_subsegment(snd_buf, &integ_buf,
  1228. offset, snd_buf->len - offset))
  1229. return status;
  1230. *integ_len = htonl(integ_buf.len);
  1231. /* guess whether we're in the head or the tail: */
  1232. if (snd_buf->page_len || snd_buf->tail[0].iov_len)
  1233. iov = snd_buf->tail;
  1234. else
  1235. iov = snd_buf->head;
  1236. p = iov->iov_base + iov->iov_len;
  1237. mic.data = (u8 *)(p + 1);
  1238. maj_stat = gss_get_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
  1239. status = -EIO; /* XXX? */
  1240. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  1241. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1242. else if (maj_stat)
  1243. return status;
  1244. q = xdr_encode_opaque(p, NULL, mic.len);
  1245. offset = (u8 *)q - (u8 *)p;
  1246. iov->iov_len += offset;
  1247. snd_buf->len += offset;
  1248. return 0;
  1249. }
  1250. static void
  1251. priv_release_snd_buf(struct rpc_rqst *rqstp)
  1252. {
  1253. int i;
  1254. for (i=0; i < rqstp->rq_enc_pages_num; i++)
  1255. __free_page(rqstp->rq_enc_pages[i]);
  1256. kfree(rqstp->rq_enc_pages);
  1257. }
  1258. static int
  1259. alloc_enc_pages(struct rpc_rqst *rqstp)
  1260. {
  1261. struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
  1262. int first, last, i;
  1263. if (snd_buf->page_len == 0) {
  1264. rqstp->rq_enc_pages_num = 0;
  1265. return 0;
  1266. }
  1267. first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
  1268. last = (snd_buf->page_base + snd_buf->page_len - 1) >> PAGE_CACHE_SHIFT;
  1269. rqstp->rq_enc_pages_num = last - first + 1 + 1;
  1270. rqstp->rq_enc_pages
  1271. = kmalloc(rqstp->rq_enc_pages_num * sizeof(struct page *),
  1272. GFP_NOFS);
  1273. if (!rqstp->rq_enc_pages)
  1274. goto out;
  1275. for (i=0; i < rqstp->rq_enc_pages_num; i++) {
  1276. rqstp->rq_enc_pages[i] = alloc_page(GFP_NOFS);
  1277. if (rqstp->rq_enc_pages[i] == NULL)
  1278. goto out_free;
  1279. }
  1280. rqstp->rq_release_snd_buf = priv_release_snd_buf;
  1281. return 0;
  1282. out_free:
  1283. rqstp->rq_enc_pages_num = i;
  1284. priv_release_snd_buf(rqstp);
  1285. out:
  1286. return -EAGAIN;
  1287. }
  1288. static inline int
  1289. gss_wrap_req_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
  1290. kxdreproc_t encode, struct rpc_rqst *rqstp,
  1291. __be32 *p, void *obj)
  1292. {
  1293. struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
  1294. u32 offset;
  1295. u32 maj_stat;
  1296. int status;
  1297. __be32 *opaque_len;
  1298. struct page **inpages;
  1299. int first;
  1300. int pad;
  1301. struct kvec *iov;
  1302. char *tmp;
  1303. opaque_len = p++;
  1304. offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
  1305. *p++ = htonl(rqstp->rq_seqno);
  1306. gss_wrap_req_encode(encode, rqstp, p, obj);
  1307. status = alloc_enc_pages(rqstp);
  1308. if (status)
  1309. return status;
  1310. first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
  1311. inpages = snd_buf->pages + first;
  1312. snd_buf->pages = rqstp->rq_enc_pages;
  1313. snd_buf->page_base -= first << PAGE_CACHE_SHIFT;
  1314. /*
  1315. * Give the tail its own page, in case we need extra space in the
  1316. * head when wrapping:
  1317. *
  1318. * call_allocate() allocates twice the slack space required
  1319. * by the authentication flavor to rq_callsize.
  1320. * For GSS, slack is GSS_CRED_SLACK.
  1321. */
  1322. if (snd_buf->page_len || snd_buf->tail[0].iov_len) {
  1323. tmp = page_address(rqstp->rq_enc_pages[rqstp->rq_enc_pages_num - 1]);
  1324. memcpy(tmp, snd_buf->tail[0].iov_base, snd_buf->tail[0].iov_len);
  1325. snd_buf->tail[0].iov_base = tmp;
  1326. }
  1327. maj_stat = gss_wrap(ctx->gc_gss_ctx, offset, snd_buf, inpages);
  1328. /* slack space should prevent this ever happening: */
  1329. BUG_ON(snd_buf->len > snd_buf->buflen);
  1330. status = -EIO;
  1331. /* We're assuming that when GSS_S_CONTEXT_EXPIRED, the encryption was
  1332. * done anyway, so it's safe to put the request on the wire: */
  1333. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  1334. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1335. else if (maj_stat)
  1336. return status;
  1337. *opaque_len = htonl(snd_buf->len - offset);
  1338. /* guess whether we're in the head or the tail: */
  1339. if (snd_buf->page_len || snd_buf->tail[0].iov_len)
  1340. iov = snd_buf->tail;
  1341. else
  1342. iov = snd_buf->head;
  1343. p = iov->iov_base + iov->iov_len;
  1344. pad = 3 - ((snd_buf->len - offset - 1) & 3);
  1345. memset(p, 0, pad);
  1346. iov->iov_len += pad;
  1347. snd_buf->len += pad;
  1348. return 0;
  1349. }
  1350. static int
  1351. gss_wrap_req(struct rpc_task *task,
  1352. kxdreproc_t encode, void *rqstp, __be32 *p, void *obj)
  1353. {
  1354. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  1355. struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
  1356. gc_base);
  1357. struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
  1358. int status = -EIO;
  1359. dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
  1360. if (ctx->gc_proc != RPC_GSS_PROC_DATA) {
  1361. /* The spec seems a little ambiguous here, but I think that not
  1362. * wrapping context destruction requests makes the most sense.
  1363. */
  1364. gss_wrap_req_encode(encode, rqstp, p, obj);
  1365. status = 0;
  1366. goto out;
  1367. }
  1368. switch (gss_cred->gc_service) {
  1369. case RPC_GSS_SVC_NONE:
  1370. gss_wrap_req_encode(encode, rqstp, p, obj);
  1371. status = 0;
  1372. break;
  1373. case RPC_GSS_SVC_INTEGRITY:
  1374. status = gss_wrap_req_integ(cred, ctx, encode, rqstp, p, obj);
  1375. break;
  1376. case RPC_GSS_SVC_PRIVACY:
  1377. status = gss_wrap_req_priv(cred, ctx, encode, rqstp, p, obj);
  1378. break;
  1379. }
  1380. out:
  1381. gss_put_ctx(ctx);
  1382. dprintk("RPC: %5u %s returning %d\n", task->tk_pid, __func__, status);
  1383. return status;
  1384. }
  1385. static inline int
  1386. gss_unwrap_resp_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
  1387. struct rpc_rqst *rqstp, __be32 **p)
  1388. {
  1389. struct xdr_buf *rcv_buf = &rqstp->rq_rcv_buf;
  1390. struct xdr_buf integ_buf;
  1391. struct xdr_netobj mic;
  1392. u32 data_offset, mic_offset;
  1393. u32 integ_len;
  1394. u32 maj_stat;
  1395. int status = -EIO;
  1396. integ_len = ntohl(*(*p)++);
  1397. if (integ_len & 3)
  1398. return status;
  1399. data_offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
  1400. mic_offset = integ_len + data_offset;
  1401. if (mic_offset > rcv_buf->len)
  1402. return status;
  1403. if (ntohl(*(*p)++) != rqstp->rq_seqno)
  1404. return status;
  1405. if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset,
  1406. mic_offset - data_offset))
  1407. return status;
  1408. if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
  1409. return status;
  1410. maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
  1411. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  1412. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1413. if (maj_stat != GSS_S_COMPLETE)
  1414. return status;
  1415. return 0;
  1416. }
  1417. static inline int
  1418. gss_unwrap_resp_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
  1419. struct rpc_rqst *rqstp, __be32 **p)
  1420. {
  1421. struct xdr_buf *rcv_buf = &rqstp->rq_rcv_buf;
  1422. u32 offset;
  1423. u32 opaque_len;
  1424. u32 maj_stat;
  1425. int status = -EIO;
  1426. opaque_len = ntohl(*(*p)++);
  1427. offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
  1428. if (offset + opaque_len > rcv_buf->len)
  1429. return status;
  1430. /* remove padding: */
  1431. rcv_buf->len = offset + opaque_len;
  1432. maj_stat = gss_unwrap(ctx->gc_gss_ctx, offset, rcv_buf);
  1433. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  1434. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1435. if (maj_stat != GSS_S_COMPLETE)
  1436. return status;
  1437. if (ntohl(*(*p)++) != rqstp->rq_seqno)
  1438. return status;
  1439. return 0;
  1440. }
  1441. static int
  1442. gss_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
  1443. __be32 *p, void *obj)
  1444. {
  1445. struct xdr_stream xdr;
  1446. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  1447. return decode(rqstp, &xdr, obj);
  1448. }
  1449. static int
  1450. gss_unwrap_resp(struct rpc_task *task,
  1451. kxdrdproc_t decode, void *rqstp, __be32 *p, void *obj)
  1452. {
  1453. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  1454. struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
  1455. gc_base);
  1456. struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
  1457. __be32 *savedp = p;
  1458. struct kvec *head = ((struct rpc_rqst *)rqstp)->rq_rcv_buf.head;
  1459. int savedlen = head->iov_len;
  1460. int status = -EIO;
  1461. if (ctx->gc_proc != RPC_GSS_PROC_DATA)
  1462. goto out_decode;
  1463. switch (gss_cred->gc_service) {
  1464. case RPC_GSS_SVC_NONE:
  1465. break;
  1466. case RPC_GSS_SVC_INTEGRITY:
  1467. status = gss_unwrap_resp_integ(cred, ctx, rqstp, &p);
  1468. if (status)
  1469. goto out;
  1470. break;
  1471. case RPC_GSS_SVC_PRIVACY:
  1472. status = gss_unwrap_resp_priv(cred, ctx, rqstp, &p);
  1473. if (status)
  1474. goto out;
  1475. break;
  1476. }
  1477. /* take into account extra slack for integrity and privacy cases: */
  1478. cred->cr_auth->au_rslack = cred->cr_auth->au_verfsize + (p - savedp)
  1479. + (savedlen - head->iov_len);
  1480. out_decode:
  1481. status = gss_unwrap_req_decode(decode, rqstp, p, obj);
  1482. out:
  1483. gss_put_ctx(ctx);
  1484. dprintk("RPC: %5u %s returning %d\n",
  1485. task->tk_pid, __func__, status);
  1486. return status;
  1487. }
  1488. static const struct rpc_authops authgss_ops = {
  1489. .owner = THIS_MODULE,
  1490. .au_flavor = RPC_AUTH_GSS,
  1491. .au_name = "RPCSEC_GSS",
  1492. .create = gss_create,
  1493. .destroy = gss_destroy,
  1494. .lookup_cred = gss_lookup_cred,
  1495. .crcreate = gss_create_cred,
  1496. .pipes_create = gss_pipes_dentries_create,
  1497. .pipes_destroy = gss_pipes_dentries_destroy,
  1498. .list_pseudoflavors = gss_mech_list_pseudoflavors,
  1499. .info2flavor = gss_mech_info2flavor,
  1500. .flavor2info = gss_mech_flavor2info,
  1501. };
  1502. static const struct rpc_credops gss_credops = {
  1503. .cr_name = "AUTH_GSS",
  1504. .crdestroy = gss_destroy_cred,
  1505. .cr_init = gss_cred_init,
  1506. .crbind = rpcauth_generic_bind_cred,
  1507. .crmatch = gss_match,
  1508. .crmarshal = gss_marshal,
  1509. .crrefresh = gss_refresh,
  1510. .crvalidate = gss_validate,
  1511. .crwrap_req = gss_wrap_req,
  1512. .crunwrap_resp = gss_unwrap_resp,
  1513. };
  1514. static const struct rpc_credops gss_nullops = {
  1515. .cr_name = "AUTH_GSS",
  1516. .crdestroy = gss_destroy_nullcred,
  1517. .crbind = rpcauth_generic_bind_cred,
  1518. .crmatch = gss_match,
  1519. .crmarshal = gss_marshal,
  1520. .crrefresh = gss_refresh_null,
  1521. .crvalidate = gss_validate,
  1522. .crwrap_req = gss_wrap_req,
  1523. .crunwrap_resp = gss_unwrap_resp,
  1524. };
  1525. static const struct rpc_pipe_ops gss_upcall_ops_v0 = {
  1526. .upcall = rpc_pipe_generic_upcall,
  1527. .downcall = gss_pipe_downcall,
  1528. .destroy_msg = gss_pipe_destroy_msg,
  1529. .open_pipe = gss_pipe_open_v0,
  1530. .release_pipe = gss_pipe_release,
  1531. };
  1532. static const struct rpc_pipe_ops gss_upcall_ops_v1 = {
  1533. .upcall = rpc_pipe_generic_upcall,
  1534. .downcall = gss_pipe_downcall,
  1535. .destroy_msg = gss_pipe_destroy_msg,
  1536. .open_pipe = gss_pipe_open_v1,
  1537. .release_pipe = gss_pipe_release,
  1538. };
  1539. static __net_init int rpcsec_gss_init_net(struct net *net)
  1540. {
  1541. return gss_svc_init_net(net);
  1542. }
  1543. static __net_exit void rpcsec_gss_exit_net(struct net *net)
  1544. {
  1545. gss_svc_shutdown_net(net);
  1546. }
  1547. static struct pernet_operations rpcsec_gss_net_ops = {
  1548. .init = rpcsec_gss_init_net,
  1549. .exit = rpcsec_gss_exit_net,
  1550. };
  1551. /*
  1552. * Initialize RPCSEC_GSS module
  1553. */
  1554. static int __init init_rpcsec_gss(void)
  1555. {
  1556. int err = 0;
  1557. err = rpcauth_register(&authgss_ops);
  1558. if (err)
  1559. goto out;
  1560. err = gss_svc_init();
  1561. if (err)
  1562. goto out_unregister;
  1563. err = register_pernet_subsys(&rpcsec_gss_net_ops);
  1564. if (err)
  1565. goto out_svc_exit;
  1566. rpc_init_wait_queue(&pipe_version_rpc_waitqueue, "gss pipe version");
  1567. return 0;
  1568. out_svc_exit:
  1569. gss_svc_shutdown();
  1570. out_unregister:
  1571. rpcauth_unregister(&authgss_ops);
  1572. out:
  1573. return err;
  1574. }
  1575. static void __exit exit_rpcsec_gss(void)
  1576. {
  1577. unregister_pernet_subsys(&rpcsec_gss_net_ops);
  1578. gss_svc_shutdown();
  1579. rpcauth_unregister(&authgss_ops);
  1580. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1581. }
  1582. MODULE_ALIAS("rpc-auth-6");
  1583. MODULE_LICENSE("GPL");
  1584. module_param_named(expired_cred_retry_delay,
  1585. gss_expired_cred_retry_delay,
  1586. uint, 0644);
  1587. MODULE_PARM_DESC(expired_cred_retry_delay, "Timeout (in seconds) until "
  1588. "the RPC engine retries an expired credential");
  1589. module_init(init_rpcsec_gss)
  1590. module_exit(exit_rpcsec_gss)