PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/release/src/router/samba3/source/client/cifs.upcall.c

https://gitlab.com/envieidoc/advancedtomato2
C | 392 lines | 284 code | 38 blank | 70 comment | 75 complexity | 98163e7646e45062fb8124b53a4b3b17 MD5 | raw file
  1. /*
  2. * CIFS user-space helper.
  3. * Copyright (C) Igor Mammedov (niallain@gmail.com) 2007
  4. *
  5. * Used by /sbin/request-key for handling
  6. * cifs upcall for kerberos authorization of access to share and
  7. * cifs upcall for DFS srver name resolving (IPv4/IPv6 aware).
  8. * You should have keyutils installed and add something like the
  9. * following lines to /etc/request-key.conf file:
  10. create cifs.spnego * * /usr/local/sbin/cifs.upcall %k
  11. create dns_resolver * * /usr/local/sbin/cifs.upcall %k
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include "includes.h"
  25. #include <keyutils.h>
  26. #include "cifs_spnego.h"
  27. const char *CIFSSPNEGO_VERSION = "1.2";
  28. static const char *prog = "cifs.upcall";
  29. typedef enum _secType {
  30. NONE = 0,
  31. KRB5,
  32. MS_KRB5
  33. } secType_t;
  34. const DATA_BLOB data_blob_null = { NULL, 0, NULL };
  35. /*
  36. * Prepares AP-REQ data for mechToken and gets session key
  37. * Uses credentials from cache. It will not ask for password
  38. * you should receive credentials for yuor name manually using
  39. * kinit or whatever you wish.
  40. *
  41. * in:
  42. * oid - string with OID/ Could be OID_KERBEROS5
  43. * or OID_KERBEROS5_OLD
  44. * principal - Service name.
  45. * Could be "cifs/FQDN" for KRB5 OID
  46. * or for MS_KRB5 OID style server principal
  47. * like "pdc$@YOUR.REALM.NAME"
  48. *
  49. * out:
  50. * secblob - pointer for spnego wrapped AP-REQ data to be stored
  51. * sess_key- pointer for SessionKey data to be stored
  52. *
  53. * ret: 0 - success, others - failure
  54. */
  55. static int
  56. handle_krb5_mech(const char *oid, const char *principal,
  57. DATA_BLOB * secblob, DATA_BLOB * sess_key)
  58. {
  59. int retval;
  60. DATA_BLOB tkt, tkt_wrapped;
  61. /* get a kerberos ticket for the service and extract the session key */
  62. retval = cli_krb5_get_ticket(principal, 0,
  63. &tkt, sess_key, 0, NULL, NULL);
  64. if (retval)
  65. return retval;
  66. /* wrap that up in a nice GSS-API wrapping */
  67. tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
  68. /* and wrap that in a shiny SPNEGO wrapper */
  69. *secblob = gen_negTokenInit(oid, tkt_wrapped);
  70. data_blob_free(&tkt_wrapped);
  71. data_blob_free(&tkt);
  72. return retval;
  73. }
  74. #define DKD_HAVE_HOSTNAME 1
  75. #define DKD_HAVE_VERSION 2
  76. #define DKD_HAVE_SEC 4
  77. #define DKD_HAVE_IPV4 8
  78. #define DKD_HAVE_IPV6 16
  79. #define DKD_HAVE_UID 32
  80. #define DKD_MUSTHAVE_SET (DKD_HAVE_HOSTNAME|DKD_HAVE_VERSION|DKD_HAVE_SEC)
  81. static int
  82. decode_key_description(const char *desc, int *ver, secType_t * sec,
  83. char **hostname, uid_t * uid)
  84. {
  85. int retval = 0;
  86. char *pos;
  87. const char *tkn = desc;
  88. do {
  89. pos = index(tkn, ';');
  90. if (strncmp(tkn, "host=", 5) == 0) {
  91. int len;
  92. if (pos == NULL) {
  93. len = strlen(tkn);
  94. } else {
  95. len = pos - tkn;
  96. }
  97. len -= 4;
  98. SAFE_FREE(*hostname);
  99. *hostname = SMB_XMALLOC_ARRAY(char, len);
  100. strlcpy(*hostname, tkn + 5, len);
  101. retval |= DKD_HAVE_HOSTNAME;
  102. } else if (strncmp(tkn, "ipv4=", 5) == 0) {
  103. /* BB: do we need it if we have hostname already? */
  104. } else if (strncmp(tkn, "ipv6=", 5) == 0) {
  105. /* BB: do we need it if we have hostname already? */
  106. } else if (strncmp(tkn, "sec=", 4) == 0) {
  107. if (strncmp(tkn + 4, "krb5", 4) == 0) {
  108. retval |= DKD_HAVE_SEC;
  109. *sec = KRB5;
  110. } else if (strncmp(tkn + 4, "mskrb5", 6) == 0) {
  111. retval |= DKD_HAVE_SEC;
  112. *sec = MS_KRB5;
  113. }
  114. } else if (strncmp(tkn, "uid=", 4) == 0) {
  115. errno = 0;
  116. *uid = strtol(tkn + 4, NULL, 16);
  117. if (errno != 0) {
  118. syslog(LOG_WARNING, "Invalid uid format: %s",
  119. strerror(errno));
  120. return 1;
  121. } else {
  122. retval |= DKD_HAVE_UID;
  123. }
  124. } else if (strncmp(tkn, "ver=", 4) == 0) { /* if version */
  125. errno = 0;
  126. *ver = strtol(tkn + 4, NULL, 16);
  127. if (errno != 0) {
  128. syslog(LOG_WARNING,
  129. "Invalid version format: %s",
  130. strerror(errno));
  131. return 1;
  132. } else {
  133. retval |= DKD_HAVE_VERSION;
  134. }
  135. }
  136. if (pos == NULL)
  137. break;
  138. tkn = pos + 1;
  139. } while (tkn);
  140. return retval;
  141. }
  142. static int
  143. cifs_resolver(const key_serial_t key, const char *key_descr)
  144. {
  145. int c;
  146. struct addrinfo *addr;
  147. char ip[INET6_ADDRSTRLEN];
  148. void *p;
  149. const char *keyend = key_descr;
  150. /* skip next 4 ';' delimiters to get to description */
  151. for (c = 1; c <= 4; c++) {
  152. keyend = index(keyend+1, ';');
  153. if (!keyend) {
  154. syslog(LOG_WARNING, "invalid key description: %s",
  155. key_descr);
  156. return 1;
  157. }
  158. }
  159. keyend++;
  160. /* resolve name to ip */
  161. c = getaddrinfo(keyend, NULL, NULL, &addr);
  162. if (c) {
  163. syslog(LOG_WARNING, "unable to resolve hostname: %s [%s]",
  164. keyend, gai_strerror(c));
  165. return 1;
  166. }
  167. /* conver ip to string form */
  168. if (addr->ai_family == AF_INET) {
  169. p = &(((struct sockaddr_in *)addr->ai_addr)->sin_addr);
  170. } else {
  171. p = &(((struct sockaddr_in6 *)addr->ai_addr)->sin6_addr);
  172. }
  173. if (!inet_ntop(addr->ai_family, p, ip, sizeof(ip))) {
  174. syslog(LOG_WARNING, "%s: inet_ntop: %s",
  175. __FUNCTION__, strerror(errno));
  176. freeaddrinfo(addr);
  177. return 1;
  178. }
  179. /* setup key */
  180. c = keyctl_instantiate(key, ip, strlen(ip)+1, 0);
  181. if (c == -1) {
  182. syslog(LOG_WARNING, "%s: keyctl_instantiate: %s",
  183. __FUNCTION__, strerror(errno));
  184. freeaddrinfo(addr);
  185. return 1;
  186. }
  187. freeaddrinfo(addr);
  188. return 0;
  189. }
  190. static void
  191. usage(void)
  192. {
  193. syslog(LOG_WARNING, "Usage: %s [-c] [-v] key_serial", prog);
  194. fprintf(stderr, "Usage: %s [-c] [-v] key_serial\n", prog);
  195. }
  196. int main(const int argc, char *const argv[])
  197. {
  198. struct cifs_spnego_msg *keydata = NULL;
  199. DATA_BLOB secblob = data_blob_null;
  200. DATA_BLOB sess_key = data_blob_null;
  201. secType_t sectype = NONE;
  202. key_serial_t key = 0;
  203. size_t datalen;
  204. long rc = 1;
  205. uid_t uid = 0;
  206. int kernel_upcall_version = 0;
  207. int c, use_cifs_service_prefix = 0;
  208. char *buf, *hostname = NULL;
  209. const char *oid;
  210. openlog(prog, 0, LOG_DAEMON);
  211. while ((c = getopt(argc, argv, "cv")) != -1) {
  212. switch (c) {
  213. case 'c':{
  214. use_cifs_service_prefix = 1;
  215. break;
  216. }
  217. case 'v':{
  218. printf("version: %s\n", CIFSSPNEGO_VERSION);
  219. goto out;
  220. }
  221. default:{
  222. syslog(LOG_WARNING, "unknown option: %c", c);
  223. goto out;
  224. }
  225. }
  226. }
  227. /* is there a key? */
  228. if (argc <= optind) {
  229. usage();
  230. goto out;
  231. }
  232. /* get key and keyring values */
  233. errno = 0;
  234. key = strtol(argv[optind], NULL, 10);
  235. if (errno != 0) {
  236. key = 0;
  237. syslog(LOG_WARNING, "Invalid key format: %s", strerror(errno));
  238. goto out;
  239. }
  240. rc = keyctl_describe_alloc(key, &buf);
  241. if (rc == -1) {
  242. syslog(LOG_WARNING, "keyctl_describe_alloc failed: %s",
  243. strerror(errno));
  244. rc = 1;
  245. goto out;
  246. }
  247. if ((strncmp(buf, "cifs.resolver", sizeof("cifs.resolver")-1) == 0) ||
  248. (strncmp(buf, "dns_resolver", sizeof("dns_resolver")-1) == 0)) {
  249. rc = cifs_resolver(key, buf);
  250. goto out;
  251. }
  252. rc = decode_key_description(buf, &kernel_upcall_version, &sectype,
  253. &hostname, &uid);
  254. if ((rc & DKD_MUSTHAVE_SET) != DKD_MUSTHAVE_SET) {
  255. syslog(LOG_WARNING,
  256. "unable to get from description necessary params");
  257. rc = 1;
  258. SAFE_FREE(buf);
  259. goto out;
  260. }
  261. SAFE_FREE(buf);
  262. if (kernel_upcall_version > CIFS_SPNEGO_UPCALL_VERSION) {
  263. syslog(LOG_WARNING,
  264. "incompatible kernel upcall version: 0x%x",
  265. kernel_upcall_version);
  266. rc = 1;
  267. goto out;
  268. }
  269. if (rc & DKD_HAVE_UID) {
  270. rc = setuid(uid);
  271. if (rc == -1) {
  272. syslog(LOG_WARNING, "setuid: %s", strerror(errno));
  273. goto out;
  274. }
  275. }
  276. /* BB: someday upcall SPNEGO blob could be checked here to decide
  277. * what mech to use */
  278. // do mech specific authorization
  279. switch (sectype) {
  280. case MS_KRB5:
  281. case KRB5:{
  282. char *princ;
  283. size_t len;
  284. /* for "cifs/" service name + terminating 0 */
  285. len = strlen(hostname) + 5 + 1;
  286. princ = SMB_XMALLOC_ARRAY(char, len);
  287. if (!princ) {
  288. rc = 1;
  289. break;
  290. }
  291. if (use_cifs_service_prefix) {
  292. strlcpy(princ, "cifs/", len);
  293. } else {
  294. strlcpy(princ, "host/", len);
  295. }
  296. strlcpy(princ + 5, hostname, len - 5);
  297. if (sectype == MS_KRB5)
  298. oid = OID_KERBEROS5_OLD;
  299. else
  300. oid = OID_KERBEROS5;
  301. rc = handle_krb5_mech(oid, princ, &secblob, &sess_key);
  302. SAFE_FREE(princ);
  303. break;
  304. }
  305. default:{
  306. syslog(LOG_WARNING, "sectype: %d is not implemented",
  307. sectype);
  308. rc = 1;
  309. break;
  310. }
  311. }
  312. if (rc) {
  313. goto out;
  314. }
  315. /* pack SecurityBLob and SessionKey into downcall packet */
  316. datalen =
  317. sizeof(struct cifs_spnego_msg) + secblob.length + sess_key.length;
  318. keydata = (struct cifs_spnego_msg*)SMB_XMALLOC_ARRAY(char, datalen);
  319. if (!keydata) {
  320. rc = 1;
  321. goto out;
  322. }
  323. keydata->version = kernel_upcall_version;
  324. keydata->flags = 0;
  325. keydata->sesskey_len = sess_key.length;
  326. keydata->secblob_len = secblob.length;
  327. memcpy(&(keydata->data), sess_key.data, sess_key.length);
  328. memcpy(&(keydata->data) + keydata->sesskey_len,
  329. secblob.data, secblob.length);
  330. /* setup key */
  331. rc = keyctl_instantiate(key, keydata, datalen, 0);
  332. if (rc == -1) {
  333. syslog(LOG_WARNING, "keyctl_instantiate: %s", strerror(errno));
  334. goto out;
  335. }
  336. /* BB: maybe we need use timeout for key: for example no more then
  337. * ticket lifietime? */
  338. /* keyctl_set_timeout( key, 60); */
  339. out:
  340. /*
  341. * on error, negatively instantiate the key ourselves so that we can
  342. * make sure the kernel doesn't hang it off of a searchable keyring
  343. * and interfere with the next attempt to instantiate the key.
  344. */
  345. if (rc != 0 && key == 0)
  346. keyctl_negate(key, 1, KEY_REQKEY_DEFL_DEFAULT);
  347. data_blob_free(&secblob);
  348. data_blob_free(&sess_key);
  349. SAFE_FREE(hostname);
  350. SAFE_FREE(keydata);
  351. return rc;
  352. }