PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/release/src/router/samba3/source/lib/afs_settoken.c

https://gitlab.com/envieidoc/tomato
C | 239 lines | 160 code | 51 blank | 28 comment | 32 complexity | c83d4cdeb15665782b654beb1ac106f7 MD5 | raw file
  1. /*
  2. * Unix SMB/CIFS implementation.
  3. * Generate AFS tickets
  4. * Copyright (C) Volker Lendecke 2004
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include "includes.h"
  21. #ifdef WITH_FAKE_KASERVER
  22. #define NO_ASN1_TYPEDEFS 1
  23. #include <afs/stds.h>
  24. #include <afs/afs.h>
  25. #include <afs/auth.h>
  26. #include <afs/venus.h>
  27. #include <asm/unistd.h>
  28. #include <openssl/des.h>
  29. #include <sys/syscall.h>
  30. int afs_syscall( int subcall,
  31. char * path,
  32. int cmd,
  33. char * cmarg,
  34. int follow)
  35. {
  36. return( syscall( SYS_afs_syscall, subcall, path, cmd, cmarg, follow));
  37. }
  38. struct ClearToken {
  39. uint32 AuthHandle;
  40. char HandShakeKey[8];
  41. uint32 ViceId;
  42. uint32 BeginTimestamp;
  43. uint32 EndTimestamp;
  44. };
  45. static BOOL afs_decode_token(const char *string, char **cell,
  46. DATA_BLOB *ticket, struct ClearToken *ct)
  47. {
  48. DATA_BLOB blob;
  49. struct ClearToken result_ct;
  50. char *s = SMB_STRDUP(string);
  51. char *t;
  52. if ((t = strtok(s, "\n")) == NULL) {
  53. DEBUG(10, ("strtok failed\n"));
  54. return False;
  55. }
  56. *cell = SMB_STRDUP(t);
  57. if ((t = strtok(NULL, "\n")) == NULL) {
  58. DEBUG(10, ("strtok failed\n"));
  59. return False;
  60. }
  61. if (sscanf(t, "%u", &result_ct.AuthHandle) != 1) {
  62. DEBUG(10, ("sscanf AuthHandle failed\n"));
  63. return False;
  64. }
  65. if ((t = strtok(NULL, "\n")) == NULL) {
  66. DEBUG(10, ("strtok failed\n"));
  67. return False;
  68. }
  69. blob = base64_decode_data_blob(t);
  70. if ( (blob.data == NULL) ||
  71. (blob.length != sizeof(result_ct.HandShakeKey) )) {
  72. DEBUG(10, ("invalid key: %x/%d\n", (uint32)blob.data,
  73. blob.length));
  74. return False;
  75. }
  76. memcpy(result_ct.HandShakeKey, blob.data, blob.length);
  77. data_blob_free(&blob);
  78. if ((t = strtok(NULL, "\n")) == NULL) {
  79. DEBUG(10, ("strtok failed\n"));
  80. return False;
  81. }
  82. if (sscanf(t, "%u", &result_ct.ViceId) != 1) {
  83. DEBUG(10, ("sscanf ViceId failed\n"));
  84. return False;
  85. }
  86. if ((t = strtok(NULL, "\n")) == NULL) {
  87. DEBUG(10, ("strtok failed\n"));
  88. return False;
  89. }
  90. if (sscanf(t, "%u", &result_ct.BeginTimestamp) != 1) {
  91. DEBUG(10, ("sscanf BeginTimestamp failed\n"));
  92. return False;
  93. }
  94. if ((t = strtok(NULL, "\n")) == NULL) {
  95. DEBUG(10, ("strtok failed\n"));
  96. return False;
  97. }
  98. if (sscanf(t, "%u", &result_ct.EndTimestamp) != 1) {
  99. DEBUG(10, ("sscanf EndTimestamp failed\n"));
  100. return False;
  101. }
  102. if ((t = strtok(NULL, "\n")) == NULL) {
  103. DEBUG(10, ("strtok failed\n"));
  104. return False;
  105. }
  106. blob = base64_decode_data_blob(t);
  107. if (blob.data == NULL) {
  108. DEBUG(10, ("Could not get ticket\n"));
  109. return False;
  110. }
  111. *ticket = blob;
  112. *ct = result_ct;
  113. return True;
  114. }
  115. /*
  116. Put an AFS token into the Kernel so that it can authenticate against
  117. the AFS server. This assumes correct local uid settings.
  118. This is currently highly Linux and OpenAFS-specific. The correct API
  119. call for this would be ktc_SetToken. But to do that we would have to
  120. import a REALLY big bunch of libraries which I would currently like
  121. to avoid.
  122. */
  123. static BOOL afs_settoken(const char *cell,
  124. const struct ClearToken *ctok,
  125. DATA_BLOB ticket)
  126. {
  127. int ret;
  128. struct {
  129. char *in, *out;
  130. uint16 in_size, out_size;
  131. } iob;
  132. char buf[1024];
  133. char *p = buf;
  134. int tmp;
  135. memcpy(p, &ticket.length, sizeof(uint32));
  136. p += sizeof(uint32);
  137. memcpy(p, ticket.data, ticket.length);
  138. p += ticket.length;
  139. tmp = sizeof(struct ClearToken);
  140. memcpy(p, &tmp, sizeof(uint32));
  141. p += sizeof(uint32);
  142. memcpy(p, ctok, tmp);
  143. p += tmp;
  144. tmp = 0;
  145. memcpy(p, &tmp, sizeof(uint32));
  146. p += sizeof(uint32);
  147. tmp = strlen(cell);
  148. if (tmp >= MAXKTCREALMLEN) {
  149. DEBUG(1, ("Realm too long\n"));
  150. return False;
  151. }
  152. strncpy(p, cell, tmp);
  153. p += tmp;
  154. *p = 0;
  155. p +=1;
  156. iob.in = buf;
  157. iob.in_size = PTR_DIFF(p,buf);
  158. iob.out = buf;
  159. iob.out_size = sizeof(buf);
  160. #if 0
  161. file_save("/tmp/ioctlbuf", iob.in, iob.in_size);
  162. #endif
  163. ret = afs_syscall(AFSCALL_PIOCTL, 0, VIOCSETTOK, (char *)&iob, 0);
  164. DEBUG(10, ("afs VIOCSETTOK returned %d\n", ret));
  165. return (ret == 0);
  166. }
  167. BOOL afs_settoken_str(const char *token_string)
  168. {
  169. DATA_BLOB ticket;
  170. struct ClearToken ct;
  171. BOOL result;
  172. char *cell;
  173. if (!afs_decode_token(token_string, &cell, &ticket, &ct))
  174. return False;
  175. if (geteuid() != 0)
  176. ct.ViceId = getuid();
  177. result = afs_settoken(cell, &ct, ticket);
  178. SAFE_FREE(cell);
  179. data_blob_free(&ticket);
  180. return result;
  181. }
  182. #else
  183. BOOL afs_settoken_str(const char *token_string)
  184. {
  185. return False;
  186. }
  187. #endif