auth_gss.c 45 KB

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