auth_gss.c 46 KB

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