rpc.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <thread.h>
  4. #include <sunrpc.h>
  5. /*
  6. * RPC protocol constants
  7. */
  8. enum
  9. {
  10. RpcVersion = 2,
  11. /* msg type */
  12. MsgCall = 0,
  13. MsgReply = 1,
  14. /* reply stat */
  15. MsgAccepted = 0,
  16. MsgDenied = 1,
  17. /* accept stat */
  18. MsgSuccess = 0,
  19. MsgProgUnavail = 1,
  20. MsgProgMismatch = 2,
  21. MsgProcUnavail = 3,
  22. MsgGarbageArgs = 4,
  23. MsgSystemErr = 5,
  24. /* reject stat */
  25. MsgRpcMismatch = 0,
  26. MsgAuthError = 1,
  27. /* msg auth xxx */
  28. MsgAuthOk = 0,
  29. MsgAuthBadCred = 1,
  30. MsgAuthRejectedCred = 2,
  31. MsgAuthBadVerf = 3,
  32. MsgAuthRejectedVerf = 4,
  33. MsgAuthTooWeak = 5,
  34. MsgAuthInvalidResp = 6,
  35. MsgAuthFailed = 7,
  36. };
  37. SunStatus
  38. sunRpcPack(uchar *a, uchar *ea, uchar **pa, SunRpc *rpc)
  39. {
  40. u32int x;
  41. if(sunUint32Pack(a, ea, &a, &rpc->xid) < 0)
  42. goto Err;
  43. if(rpc->iscall){
  44. if(sunUint32Pack(a, ea, &a, (x=MsgCall, &x)) < 0
  45. || sunUint32Pack(a, ea, &a, (x=RpcVersion, &x)) < 0
  46. || sunUint32Pack(a, ea, &a, &rpc->prog) < 0
  47. || sunUint32Pack(a, ea, &a, &rpc->vers) < 0
  48. || sunUint32Pack(a, ea, &a, &rpc->proc) < 0
  49. || sunAuthInfoPack(a, ea, &a, &rpc->cred) < 0
  50. || sunAuthInfoPack(a, ea, &a, &rpc->verf) < 0
  51. || sunFixedOpaquePack(a, ea, &a, rpc->data, rpc->ndata) < 0)
  52. goto Err;
  53. }else{
  54. if(sunUint32Pack(a, ea, &a, (x=MsgReply, &x)) < 0)
  55. goto Err;
  56. switch(rpc->status&0xF0000){
  57. case 0:
  58. case SunAcceptError:
  59. if(sunUint32Pack(a, ea, &a, (x=MsgAccepted, &x)) < 0
  60. || sunAuthInfoPack(a, ea, &a, &rpc->verf) < 0)
  61. goto Err;
  62. break;
  63. default:
  64. if(sunUint32Pack(a, ea, &a, (x=MsgDenied, &x)) < 0)
  65. goto Err;
  66. break;
  67. }
  68. switch(rpc->status){
  69. case SunSuccess:
  70. if(sunUint32Pack(a, ea, &a, (x=MsgSuccess, &x)) < 0
  71. || sunFixedOpaquePack(a, ea, &a, rpc->data, rpc->ndata) < 0)
  72. goto Err;
  73. break;
  74. case SunRpcMismatch:
  75. case SunProgMismatch:
  76. if(sunUint32Pack(a, ea, &a, (x=rpc->status&0xFFFF, &x)) < 0
  77. || sunUint32Pack(a, ea, &a, &rpc->low) < 0
  78. || sunUint32Pack(a, ea, &a, &rpc->high) < 0)
  79. goto Err;
  80. break;
  81. default:
  82. if(sunUint32Pack(a, ea, &a, (x=rpc->status&0xFFFF, &x)) < 0)
  83. goto Err;
  84. break;
  85. }
  86. }
  87. *pa = a;
  88. return SunSuccess;
  89. Err:
  90. *pa = ea;
  91. return SunGarbageArgs;
  92. }
  93. uint
  94. sunRpcSize(SunRpc *rpc)
  95. {
  96. uint a;
  97. a = 4;
  98. if(rpc->iscall){
  99. a += 5*4;
  100. a += sunAuthInfoSize(&rpc->cred);
  101. a += sunAuthInfoSize(&rpc->verf);
  102. a += sunFixedOpaqueSize(rpc->ndata);
  103. }else{
  104. a += 4;
  105. switch(rpc->status&0xF0000){
  106. case 0:
  107. case SunAcceptError:
  108. a += 4+sunAuthInfoSize(&rpc->verf);
  109. break;
  110. default:
  111. a += 4;
  112. break;
  113. }
  114. switch(rpc->status){
  115. case SunSuccess:
  116. a += 4+sunFixedOpaqueSize(rpc->ndata);
  117. break;
  118. case SunRpcMismatch:
  119. case SunProgMismatch:
  120. a += 3*4;
  121. default:
  122. a += 4;
  123. }
  124. }
  125. return a;
  126. }
  127. SunStatus
  128. sunRpcUnpack(uchar *a, uchar *ea, uchar **pa, SunRpc *rpc)
  129. {
  130. u32int x;
  131. memset(rpc, 0, sizeof *rpc);
  132. if(sunUint32Unpack(a, ea, &a, &rpc->xid) < 0
  133. || sunUint32Unpack(a, ea, &a, &x) < 0)
  134. goto Err;
  135. switch(x){
  136. default:
  137. goto Err;
  138. case MsgCall:
  139. rpc->iscall = 1;
  140. if(sunUint32Unpack(a, ea, &a, &x) < 0 || x != RpcVersion
  141. || sunUint32Unpack(a, ea, &a, &rpc->prog) < 0
  142. || sunUint32Unpack(a, ea, &a, &rpc->vers) < 0
  143. || sunUint32Unpack(a, ea, &a, &rpc->proc) < 0
  144. || sunAuthInfoUnpack(a, ea, &a, &rpc->cred) < 0
  145. || sunAuthInfoUnpack(a, ea, &a, &rpc->verf) < 0)
  146. goto Err;
  147. rpc->ndata = ea-a;
  148. rpc->data = a;
  149. a = ea;
  150. break;
  151. case MsgReply:
  152. rpc->iscall = 0;
  153. if(sunUint32Unpack(a, ea, &a, &x) < 0)
  154. goto Err;
  155. switch(x){
  156. default:
  157. goto Err;
  158. case MsgAccepted:
  159. if(sunAuthInfoUnpack(a, ea, &a, &rpc->verf) < 0
  160. || sunUint32Unpack(a, ea, &a, &x) < 0)
  161. goto Err;
  162. switch(x){
  163. case MsgSuccess:
  164. rpc->status = SunSuccess;
  165. rpc->ndata = ea-a;
  166. rpc->data = a;
  167. a = ea;
  168. break;
  169. case MsgProgUnavail:
  170. case MsgProcUnavail:
  171. case MsgGarbageArgs:
  172. case MsgSystemErr:
  173. rpc->status = SunAcceptError | x;
  174. break;
  175. case MsgProgMismatch:
  176. rpc->status = SunAcceptError | x;
  177. if(sunUint32Unpack(a, ea, &a, &rpc->low) < 0
  178. || sunUint32Unpack(a, ea, &a, &rpc->high) < 0)
  179. goto Err;
  180. break;
  181. }
  182. break;
  183. case MsgDenied:
  184. if(sunUint32Unpack(a, ea, &a, &x) < 0)
  185. goto Err;
  186. switch(x){
  187. default:
  188. goto Err;
  189. case MsgAuthError:
  190. if(sunUint32Unpack(a, ea, &a, &x) < 0)
  191. goto Err;
  192. rpc->status = SunAuthError | x;
  193. break;
  194. case MsgRpcMismatch:
  195. rpc->status = SunRejectError | x;
  196. if(sunUint32Unpack(a, ea, &a, &rpc->low) < 0
  197. || sunUint32Unpack(a, ea, &a, &rpc->high) < 0)
  198. goto Err;
  199. break;
  200. }
  201. break;
  202. }
  203. }
  204. *pa = a;
  205. return SunSuccess;
  206. Err:
  207. *pa = ea;
  208. return SunGarbageArgs;
  209. }
  210. void
  211. sunRpcPrint(Fmt *fmt, SunRpc *rpc)
  212. {
  213. fmtprint(fmt, "xid=%#ux", rpc->xid);
  214. if(rpc->iscall){
  215. fmtprint(fmt, " prog %#ux vers %#ux proc %#ux [", rpc->prog, rpc->vers, rpc->proc);
  216. sunAuthInfoPrint(fmt, &rpc->cred);
  217. fmtprint(fmt, "] [");
  218. sunAuthInfoPrint(fmt, &rpc->verf);
  219. fmtprint(fmt, "]");
  220. }else{
  221. fmtprint(fmt, " status %#ux [", rpc->status);
  222. sunAuthInfoPrint(fmt, &rpc->verf);
  223. fmtprint(fmt, "] low %#ux high %#ux", rpc->low, rpc->high);
  224. }
  225. }
  226. void
  227. sunAuthInfoPrint(Fmt *fmt, SunAuthInfo *ai)
  228. {
  229. switch(ai->flavor){
  230. case SunAuthNone:
  231. fmtprint(fmt, "none");
  232. break;
  233. case SunAuthShort:
  234. fmtprint(fmt, "short");
  235. break;
  236. case SunAuthSys:
  237. fmtprint(fmt, "sys");
  238. break;
  239. default:
  240. fmtprint(fmt, "%#ux", ai->flavor);
  241. break;
  242. }
  243. // if(ai->ndata)
  244. // fmtprint(fmt, " %.*H", ai->ndata, ai->data);
  245. }
  246. uint
  247. sunAuthInfoSize(SunAuthInfo *ai)
  248. {
  249. return 4 + sunVarOpaqueSize(ai->ndata);
  250. }
  251. int
  252. sunAuthInfoPack(uchar *a, uchar *ea, uchar **pa, SunAuthInfo *ai)
  253. {
  254. if(sunUint32Pack(a, ea, &a, &ai->flavor) < 0
  255. || sunVarOpaquePack(a, ea, &a, &ai->data, &ai->ndata, 400) < 0)
  256. goto Err;
  257. *pa = a;
  258. return 0;
  259. Err:
  260. *pa = ea;
  261. return -1;
  262. }
  263. int
  264. sunAuthInfoUnpack(uchar *a, uchar *ea, uchar **pa, SunAuthInfo *ai)
  265. {
  266. if(sunUint32Unpack(a, ea, &a, &ai->flavor) < 0
  267. || sunVarOpaqueUnpack(a, ea, &a, &ai->data, &ai->ndata, 400) < 0)
  268. goto Err;
  269. *pa = a;
  270. return 0;
  271. Err:
  272. *pa = ea;
  273. return -1;
  274. }
  275. int
  276. sunEnumPack(uchar *a, uchar *ea, uchar **pa, int *e)
  277. {
  278. u32int x;
  279. x = *e;
  280. return sunUint32Pack(a, ea, pa, &x);
  281. }
  282. int
  283. sunUint1Pack(uchar *a, uchar *ea, uchar **pa, u1int *u)
  284. {
  285. u32int x;
  286. x = *u;
  287. return sunUint32Pack(a, ea, pa, &x);
  288. }
  289. int
  290. sunUint32Pack(uchar *a, uchar *ea, uchar **pa, u32int *u)
  291. {
  292. u32int x;
  293. if(ea-a < 4)
  294. goto Err;
  295. x = *u;
  296. *a++ = x>>24;
  297. *a++ = x>>16;
  298. *a++ = x>>8;
  299. *a++ = x;
  300. *pa = a;
  301. return 0;
  302. Err:
  303. *pa = ea;
  304. return -1;
  305. }
  306. int
  307. sunEnumUnpack(uchar *a, uchar *ea, uchar **pa, int *e)
  308. {
  309. u32int x;
  310. if(sunUint32Unpack(a, ea, pa, &x) < 0)
  311. return -1;
  312. *e = x;
  313. return 0;
  314. }
  315. int
  316. sunUint1Unpack(uchar *a, uchar *ea, uchar **pa, u1int *u)
  317. {
  318. u32int x;
  319. if(sunUint32Unpack(a, ea, pa, &x) < 0 || (x!=0 && x!=1)){
  320. *pa = ea;
  321. return -1;
  322. }
  323. *u = x;
  324. return 0;
  325. }
  326. int
  327. sunUint32Unpack(uchar *a, uchar *ea, uchar **pa, u32int *u)
  328. {
  329. u32int x;
  330. if(ea-a < 4)
  331. goto Err;
  332. x = *a++ << 24;
  333. x |= *a++ << 16;
  334. x |= *a++ << 8;
  335. x |= *a++;
  336. *pa = a;
  337. *u = x;
  338. return 0;
  339. Err:
  340. *pa = ea;
  341. return -1;
  342. }
  343. int
  344. sunUint64Unpack(uchar *a, uchar *ea, uchar **pa, u64int *u)
  345. {
  346. u32int x, y;
  347. if(sunUint32Unpack(a, ea, &a, &x) < 0
  348. || sunUint32Unpack(a, ea, &a, &y) < 0)
  349. goto Err;
  350. *u = ((uvlong)x<<32) | y;
  351. *pa = a;
  352. return 0;
  353. Err:
  354. *pa = ea;
  355. return -1;
  356. }
  357. int
  358. sunUint64Pack(uchar *a, uchar *ea, uchar **pa, u64int *u)
  359. {
  360. u32int x, y;
  361. x = *u >> 32;
  362. y = *u;
  363. if(sunUint32Pack(a, ea, &a, &x) < 0
  364. || sunUint32Pack(a, ea, &a, &y) < 0)
  365. goto Err;
  366. *pa = a;
  367. return 0;
  368. Err:
  369. *pa = ea;
  370. return -1;
  371. }
  372. uint
  373. sunStringSize(char *s)
  374. {
  375. return (4+strlen(s)+3) & ~3;
  376. }
  377. int
  378. sunStringUnpack(uchar *a, uchar *ea, uchar **pa, char **s, u32int max)
  379. {
  380. uchar *dat;
  381. u32int n;
  382. if(sunVarOpaqueUnpack(a, ea, pa, &dat, &n, max) < 0)
  383. goto Err;
  384. /* slide string down over length to make room for NUL */
  385. memmove(dat-1, dat, n);
  386. dat[-1+n] = 0;
  387. *s = (char*)(dat-1);
  388. return 0;
  389. Err:
  390. return -1;
  391. }
  392. int
  393. sunStringPack(uchar *a, uchar *ea, uchar **pa, char **s, u32int max)
  394. {
  395. u32int n;
  396. n = strlen(*s);
  397. return sunVarOpaquePack(a, ea, pa, (uchar**)s, &n, max);
  398. }
  399. uint
  400. sunVarOpaqueSize(u32int n)
  401. {
  402. return (4+n+3) & ~3;
  403. }
  404. int
  405. sunVarOpaquePack(uchar *a, uchar *ea, uchar **pa, uchar **dat, u32int *ndat, u32int max)
  406. {
  407. if(*ndat > max || sunUint32Pack(a, ea, &a, ndat) < 0
  408. || sunFixedOpaquePack(a, ea, &a, *dat, *ndat) < 0)
  409. goto Err;
  410. *pa = a;
  411. return 0;
  412. Err:
  413. *pa = ea;
  414. return -1;
  415. }
  416. int
  417. sunVarOpaqueUnpack(uchar *a, uchar *ea, uchar **pa, uchar **dat, u32int *ndat, u32int max)
  418. {
  419. if(sunUint32Unpack(a, ea, &a, ndat) < 0
  420. || *ndat > max)
  421. goto Err;
  422. *dat = a;
  423. a += (*ndat+3)&~3;
  424. if(a > ea)
  425. goto Err;
  426. *pa = a;
  427. return 0;
  428. Err:
  429. *pa = ea;
  430. return -1;
  431. }
  432. uint
  433. sunFixedOpaqueSize(u32int n)
  434. {
  435. return (n+3) & ~3;
  436. }
  437. int
  438. sunFixedOpaquePack(uchar *a, uchar *ea, uchar **pa, uchar *dat, u32int n)
  439. {
  440. uint nn;
  441. nn = (n+3)&~3;
  442. if(a+nn > ea)
  443. goto Err;
  444. memmove(a, dat, n);
  445. if(nn > n)
  446. memset(a+n, 0, nn-n);
  447. a += nn;
  448. *pa = a;
  449. return 0;
  450. Err:
  451. *pa = ea;
  452. return -1;
  453. }
  454. int
  455. sunFixedOpaqueUnpack(uchar *a, uchar *ea, uchar **pa, uchar *dat, u32int n)
  456. {
  457. uint nn;
  458. nn = (n+3)&~3;
  459. if(a+nn > ea)
  460. goto Err;
  461. memmove(dat, a, n);
  462. a += nn;
  463. *pa = a;
  464. return 0;
  465. Err:
  466. *pa = ea;
  467. return -1;
  468. }