PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src/router/samba3/source/auth/auth_domain.c

https://gitlab.com/envieidoc/advancedtomato2
C | 474 lines | 280 code | 79 blank | 115 comment | 39 complexity | c74694427ec7763b2b172d8f85859867 MD5 | raw file
  1. /*
  2. Unix SMB/CIFS implementation.
  3. Authenticate against a remote domain
  4. Copyright (C) Andrew Tridgell 1992-1998
  5. Copyright (C) Andrew Bartlett 2001
  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. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include "includes.h"
  19. #undef DBGC_CLASS
  20. #define DBGC_CLASS DBGC_AUTH
  21. extern BOOL global_machine_password_needs_changing;
  22. /**
  23. * Connect to a remote server for (inter)domain security authenticaion.
  24. *
  25. * @param cli the cli to return containing the active connection
  26. * @param server either a machine name or text IP address to
  27. * connect to.
  28. * @param setup_creds_as domain account to setup credentials as
  29. * @param sec_chan a switch value to distinguish between domain
  30. * member and interdomain authentication
  31. * @param trust_passwd the trust password to establish the
  32. * credentials with.
  33. *
  34. **/
  35. static NTSTATUS connect_to_domain_password_server(struct cli_state **cli,
  36. const char *domain,
  37. const char *dc_name,
  38. struct in_addr dc_ip,
  39. struct rpc_pipe_client **pipe_ret,
  40. BOOL *retry)
  41. {
  42. NTSTATUS result;
  43. struct rpc_pipe_client *netlogon_pipe = NULL;
  44. *cli = NULL;
  45. *pipe_ret = NULL;
  46. /* TODO: Send a SAMLOGON request to determine whether this is a valid
  47. logonserver. We can avoid a 30-second timeout if the DC is down
  48. if the SAMLOGON request fails as it is only over UDP. */
  49. /* we use a mutex to prevent two connections at once - when a
  50. Win2k PDC get two connections where one hasn't completed a
  51. session setup yet it will send a TCP reset to the first
  52. connection (tridge) */
  53. /*
  54. * With NT4.x DC's *all* authentication must be serialized to avoid
  55. * ACCESS_DENIED errors if 2 auths are done from the same machine. JRA.
  56. */
  57. if (!grab_server_mutex(dc_name)) {
  58. return NT_STATUS_NO_LOGON_SERVERS;
  59. }
  60. /* Attempt connection */
  61. *retry = True;
  62. result = cli_full_connection(cli, global_myname(), dc_name, &dc_ip, 0,
  63. "IPC$", "IPC", "", "", "", 0, Undefined, retry);
  64. if (!NT_STATUS_IS_OK(result)) {
  65. /* map to something more useful */
  66. if (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)) {
  67. result = NT_STATUS_NO_LOGON_SERVERS;
  68. }
  69. if (*cli) {
  70. cli_shutdown(*cli);
  71. *cli = NULL;
  72. }
  73. release_server_mutex();
  74. return result;
  75. }
  76. /*
  77. * We now have an anonymous connection to IPC$ on the domain password server.
  78. */
  79. /*
  80. * Even if the connect succeeds we need to setup the netlogon
  81. * pipe here. We do this as we may just have changed the domain
  82. * account password on the PDC and yet we may be talking to
  83. * a BDC that doesn't have this replicated yet. In this case
  84. * a successful connect to a DC needs to take the netlogon connect
  85. * into account also. This patch from "Bjart Kvarme" <bjart.kvarme@usit.uio.no>.
  86. */
  87. /* open the netlogon pipe. */
  88. if (lp_client_schannel()) {
  89. /* We also setup the creds chain in the open_schannel call. */
  90. netlogon_pipe = cli_rpc_pipe_open_schannel(*cli, PI_NETLOGON,
  91. PIPE_AUTH_LEVEL_PRIVACY, domain, &result);
  92. } else {
  93. netlogon_pipe = cli_rpc_pipe_open_noauth(*cli, PI_NETLOGON, &result);
  94. }
  95. if(!netlogon_pipe) {
  96. DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
  97. machine %s. Error was : %s.\n", dc_name, nt_errstr(result)));
  98. cli_shutdown(*cli);
  99. *cli = NULL;
  100. release_server_mutex();
  101. return result;
  102. }
  103. if (!lp_client_schannel()) {
  104. /* We need to set up a creds chain on an unauthenticated netlogon pipe. */
  105. uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
  106. uint32 sec_chan_type = 0;
  107. unsigned char machine_pwd[16];
  108. const char *account_name;
  109. if (!get_trust_pw_hash(domain, machine_pwd, &account_name,
  110. &sec_chan_type))
  111. {
  112. DEBUG(0, ("connect_to_domain_password_server: could not fetch "
  113. "trust account password for domain '%s'\n",
  114. domain));
  115. cli_shutdown(*cli);
  116. *cli = NULL;
  117. release_server_mutex();
  118. return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
  119. }
  120. result = rpccli_netlogon_setup_creds(netlogon_pipe,
  121. dc_name, /* server name */
  122. domain, /* domain */
  123. global_myname(), /* client name */
  124. account_name, /* machine account name */
  125. machine_pwd,
  126. sec_chan_type,
  127. &neg_flags);
  128. if (!NT_STATUS_IS_OK(result)) {
  129. cli_shutdown(*cli);
  130. *cli = NULL;
  131. release_server_mutex();
  132. return result;
  133. }
  134. }
  135. if(!netlogon_pipe) {
  136. DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
  137. machine %s. Error was : %s.\n", dc_name, cli_errstr(*cli)));
  138. cli_shutdown(*cli);
  139. *cli = NULL;
  140. release_server_mutex();
  141. return NT_STATUS_NO_LOGON_SERVERS;
  142. }
  143. /* We exit here with the mutex *locked*. JRA */
  144. *pipe_ret = netlogon_pipe;
  145. return NT_STATUS_OK;
  146. }
  147. /***********************************************************************
  148. Do the same as security=server, but using NT Domain calls and a session
  149. key from the machine password. If the server parameter is specified
  150. use it, otherwise figure out a server from the 'password server' param.
  151. ************************************************************************/
  152. static NTSTATUS domain_client_validate(TALLOC_CTX *mem_ctx,
  153. const auth_usersupplied_info *user_info,
  154. const char *domain,
  155. uchar chal[8],
  156. auth_serversupplied_info **server_info,
  157. const char *dc_name,
  158. struct in_addr dc_ip)
  159. {
  160. NET_USER_INFO_3 info3;
  161. struct cli_state *cli = NULL;
  162. struct rpc_pipe_client *netlogon_pipe = NULL;
  163. NTSTATUS nt_status = NT_STATUS_NO_LOGON_SERVERS;
  164. int i;
  165. BOOL retry = True;
  166. /*
  167. * At this point, smb_apasswd points to the lanman response to
  168. * the challenge in local_challenge, and smb_ntpasswd points to
  169. * the NT response to the challenge in local_challenge. Ship
  170. * these over the secure channel to a domain controller and
  171. * see if they were valid.
  172. */
  173. /* rety loop for robustness */
  174. for (i = 0; !NT_STATUS_IS_OK(nt_status) && retry && (i < 3); i++) {
  175. nt_status = connect_to_domain_password_server(&cli,
  176. domain,
  177. dc_name,
  178. dc_ip,
  179. &netlogon_pipe,
  180. &retry);
  181. }
  182. if ( !NT_STATUS_IS_OK(nt_status) ) {
  183. DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
  184. if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
  185. return NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE;
  186. }
  187. return nt_status;
  188. }
  189. /* store a successful connection */
  190. saf_store( domain, cli->desthost );
  191. ZERO_STRUCT(info3);
  192. /*
  193. * If this call succeeds, we now have lots of info about the user
  194. * in the info3 structure.
  195. */
  196. nt_status = rpccli_netlogon_sam_network_logon(netlogon_pipe,
  197. mem_ctx,
  198. user_info->logon_parameters,/* flags such as 'allow workstation logon' */
  199. dc_name, /* server name */
  200. user_info->smb_name, /* user name logging on. */
  201. user_info->client_domain, /* domain name */
  202. user_info->wksta_name, /* workstation name */
  203. chal, /* 8 byte challenge. */
  204. user_info->lm_resp, /* lanman 24 byte response */
  205. user_info->nt_resp, /* nt 24 byte response */
  206. &info3); /* info3 out */
  207. /* Let go as soon as possible so we avoid any potential deadlocks
  208. with winbind lookup up users or groups. */
  209. release_server_mutex();
  210. if (!NT_STATUS_IS_OK(nt_status)) {
  211. DEBUG(0,("domain_client_validate: unable to validate password "
  212. "for user %s in domain %s to Domain controller %s. "
  213. "Error was %s.\n", user_info->smb_name,
  214. user_info->client_domain, dc_name,
  215. nt_errstr(nt_status)));
  216. /* map to something more useful */
  217. if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
  218. nt_status = NT_STATUS_NO_LOGON_SERVERS;
  219. }
  220. } else {
  221. nt_status = make_server_info_info3(mem_ctx,
  222. user_info->smb_name,
  223. domain,
  224. server_info,
  225. &info3);
  226. if (NT_STATUS_IS_OK(nt_status)) {
  227. (*server_info)->was_mapped |= user_info->was_mapped;
  228. if ( ! (*server_info)->guest) {
  229. /* if a real user check pam account restrictions */
  230. /* only really perfomed if "obey pam restriction" is true */
  231. nt_status = smb_pam_accountcheck((*server_info)->unix_name);
  232. if ( !NT_STATUS_IS_OK(nt_status)) {
  233. DEBUG(1, ("PAM account restriction prevents user login\n"));
  234. cli_shutdown(cli);
  235. return nt_status;
  236. }
  237. }
  238. }
  239. netsamlogon_cache_store( user_info->smb_name, &info3 );
  240. }
  241. /* Note - once the cli stream is shutdown the mem_ctx used
  242. to allocate the other_sids and gids structures has been deleted - so
  243. these pointers are no longer valid..... */
  244. cli_shutdown(cli);
  245. return nt_status;
  246. }
  247. /****************************************************************************
  248. Check for a valid username and password in security=domain mode.
  249. ****************************************************************************/
  250. static NTSTATUS check_ntdomain_security(const struct auth_context *auth_context,
  251. void *my_private_data,
  252. TALLOC_CTX *mem_ctx,
  253. const auth_usersupplied_info *user_info,
  254. auth_serversupplied_info **server_info)
  255. {
  256. NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
  257. const char *domain = lp_workgroup();
  258. fstring dc_name;
  259. struct in_addr dc_ip;
  260. if ( lp_server_role() != ROLE_DOMAIN_MEMBER ) {
  261. DEBUG(0,("check_ntdomain_security: Configuration error! Cannot use "
  262. "ntdomain auth method when not a member of a domain.\n"));
  263. return NT_STATUS_NOT_IMPLEMENTED;
  264. }
  265. if (!user_info || !server_info || !auth_context) {
  266. DEBUG(1,("check_ntdomain_security: Critical variables not present. Failing.\n"));
  267. return NT_STATUS_INVALID_PARAMETER;
  268. }
  269. /*
  270. * Check that the requested domain is not our own machine name.
  271. * If it is, we should never check the PDC here, we use our own local
  272. * password file.
  273. */
  274. if(strequal(get_global_sam_name(), user_info->domain)) {
  275. DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
  276. return NT_STATUS_NOT_IMPLEMENTED;
  277. }
  278. /* we need our DC to send the net_sam_logon() request to */
  279. if ( !get_dc_name(domain, NULL, dc_name, &dc_ip) ) {
  280. DEBUG(5,("check_ntdomain_security: unable to locate a DC for domain %s\n",
  281. user_info->domain));
  282. return NT_STATUS_NO_LOGON_SERVERS;
  283. }
  284. nt_status = domain_client_validate(mem_ctx,
  285. user_info,
  286. domain,
  287. (uchar *)auth_context->challenge.data,
  288. server_info,
  289. dc_name,
  290. dc_ip);
  291. return nt_status;
  292. }
  293. /* module initialisation */
  294. static NTSTATUS auth_init_ntdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
  295. {
  296. if (!make_auth_methods(auth_context, auth_method)) {
  297. return NT_STATUS_NO_MEMORY;
  298. }
  299. (*auth_method)->name = "ntdomain";
  300. (*auth_method)->auth = check_ntdomain_security;
  301. return NT_STATUS_OK;
  302. }
  303. /****************************************************************************
  304. Check for a valid username and password in a trusted domain
  305. ****************************************************************************/
  306. static NTSTATUS check_trustdomain_security(const struct auth_context *auth_context,
  307. void *my_private_data,
  308. TALLOC_CTX *mem_ctx,
  309. const auth_usersupplied_info *user_info,
  310. auth_serversupplied_info **server_info)
  311. {
  312. NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
  313. unsigned char trust_md4_password[16];
  314. char *trust_password;
  315. time_t last_change_time;
  316. DOM_SID sid;
  317. fstring dc_name;
  318. struct in_addr dc_ip;
  319. if (!user_info || !server_info || !auth_context) {
  320. DEBUG(1,("check_trustdomain_security: Critical variables not present. Failing.\n"));
  321. return NT_STATUS_INVALID_PARAMETER;
  322. }
  323. /*
  324. * Check that the requested domain is not our own machine name or domain name.
  325. */
  326. if( strequal(get_global_sam_name(), user_info->domain)) {
  327. DEBUG(3,("check_trustdomain_security: Requested domain [%s] was for this machine.\n",
  328. user_info->domain));
  329. return NT_STATUS_NOT_IMPLEMENTED;
  330. }
  331. /* No point is bothering if this is not a trusted domain.
  332. This return makes "map to guest = bad user" work again.
  333. The logic is that if we know nothing about the domain, that
  334. user is not known to us and does not exist */
  335. if ( !is_trusted_domain( user_info->domain ) )
  336. return NT_STATUS_NOT_IMPLEMENTED;
  337. /*
  338. * Get the trusted account password for the trusted domain
  339. * No need to become_root() as secrets_init() is done at startup.
  340. */
  341. if (!secrets_fetch_trusted_domain_password(user_info->domain, &trust_password,
  342. &sid, &last_change_time)) {
  343. DEBUG(0, ("check_trustdomain_security: could not fetch trust "
  344. "account password for domain %s\n",
  345. user_info->domain));
  346. return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
  347. }
  348. #ifdef DEBUG_PASSWORD
  349. DEBUG(100, ("Trust password for domain %s is %s\n", user_info->domain,
  350. trust_password));
  351. #endif
  352. E_md4hash(trust_password, trust_md4_password);
  353. SAFE_FREE(trust_password);
  354. #if 0
  355. /* Test if machine password is expired and need to be changed */
  356. if (time(NULL) > last_change_time + (time_t)lp_machine_password_timeout())
  357. {
  358. global_machine_password_needs_changing = True;
  359. }
  360. #endif
  361. /* use get_dc_name() for consistency even through we know that it will be
  362. a netbios name */
  363. if ( !get_dc_name(user_info->domain, NULL, dc_name, &dc_ip) ) {
  364. DEBUG(5,("check_trustdomain_security: unable to locate a DC for domain %s\n",
  365. user_info->domain));
  366. return NT_STATUS_NO_LOGON_SERVERS;
  367. }
  368. nt_status = domain_client_validate(mem_ctx,
  369. user_info,
  370. user_info->domain,
  371. (uchar *)auth_context->challenge.data,
  372. server_info,
  373. dc_name,
  374. dc_ip);
  375. return nt_status;
  376. }
  377. /* module initialisation */
  378. static NTSTATUS auth_init_trustdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
  379. {
  380. if (!make_auth_methods(auth_context, auth_method)) {
  381. return NT_STATUS_NO_MEMORY;
  382. }
  383. (*auth_method)->name = "trustdomain";
  384. (*auth_method)->auth = check_trustdomain_security;
  385. return NT_STATUS_OK;
  386. }
  387. NTSTATUS auth_domain_init(void)
  388. {
  389. smb_register_auth(AUTH_INTERFACE_VERSION, "trustdomain", auth_init_trustdomain);
  390. smb_register_auth(AUTH_INTERFACE_VERSION, "ntdomain", auth_init_ntdomain);
  391. return NT_STATUS_OK;
  392. }