/lwadtool/libadtool/unlock.c

https://github.com/BeyondTrust/pbis-open · C · 208 lines · 124 code · 34 blank · 50 comment · 19 complexity · b20eeb2ba2a6ce9af55e53695719228b MD5 · raw file

  1. /*
  2. * Copyright © BeyondTrust Software 2004 - 2019
  3. * All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * BEYONDTRUST MAKES THIS SOFTWARE AVAILABLE UNDER OTHER LICENSING TERMS AS
  18. * WELL. IF YOU HAVE ENTERED INTO A SEPARATE LICENSE AGREEMENT WITH
  19. * BEYONDTRUST, THEN YOU MAY ELECT TO USE THE SOFTWARE UNDER THE TERMS OF THAT
  20. * SOFTWARE LICENSE AGREEMENT INSTEAD OF THE TERMS OF THE APACHE LICENSE,
  21. * NOTWITHSTANDING THE ABOVE NOTICE. IF YOU HAVE QUESTIONS, OR WISH TO REQUEST
  22. * A COPY OF THE ALTERNATE LICENSING TERMS OFFERED BY BEYONDTRUST, PLEASE CONTACT
  23. * BEYONDTRUST AT beyondtrust.com/contact
  24. */
  25. /*
  26. * Module Name:
  27. *
  28. * unlock.c
  29. *
  30. * Abstract:
  31. * Methods for unlocking user and computer accounts in AD.
  32. *
  33. *
  34. * Authors: Author: CORP\slavam
  35. *
  36. * Created on: Mar 17, 2010
  37. *
  38. */
  39. #include "includes.h"
  40. /**
  41. * Action initialization method.
  42. */
  43. DWORD InitAdtUnlockAccountAction(IN AdtActionTP action)
  44. {
  45. return InitBaseAction(action);
  46. }
  47. /**
  48. * Action validate method.
  49. */
  50. DWORD ValidateAdtUnlockAccountAction(IN AdtActionTP action)
  51. {
  52. DWORD dwError = 0;
  53. if (!action->unlockAccount.user && !action->unlockAccount.computer) {
  54. dwError = ADT_ERR_INVALID_ARG_USER_COMPUTER;
  55. ADT_BAIL_ON_ERROR_NP(dwError);
  56. }
  57. if (action->unlockAccount.user && action->unlockAccount.computer) {
  58. dwError = ADT_ERR_INVALID_ARG_USER_COMPUTER;
  59. ADT_BAIL_ON_ERROR_NP(dwError);
  60. }
  61. if(action->unlockAccount.user) {
  62. dwError = ProcessDash(&(action->unlockAccount.user));
  63. ADT_BAIL_ON_ERROR_NP(dwError);
  64. dwError = OpenADSearchConnectionDomain(action, &(action->unlockAccount.user));
  65. }
  66. else {
  67. dwError = ProcessDash(&(action->unlockAccount.computer));
  68. ADT_BAIL_ON_ERROR_NP(dwError);
  69. dwError = OpenADSearchConnectionDomain(action, &(action->unlockAccount.computer));
  70. }
  71. ADT_BAIL_ON_ERROR_NP(dwError);
  72. SwitchToSearchConnection(action);
  73. cleanup:
  74. return dwError;
  75. error:
  76. goto cleanup;
  77. }
  78. /**
  79. * Action execute method.
  80. */
  81. DWORD ExecuteAdtUnlockAccountAction(IN AdtActionTP action)
  82. {
  83. DWORD dwError = 0;
  84. AppContextTP appContext = (AppContextTP) ((AdtActionBaseTP) action)->opaque;
  85. INT i = 0;
  86. INT j = 0;
  87. AttrValsT *avp = NULL;
  88. AttrValsT *avpMod = NULL;
  89. if(action->unlockAccount.user) {
  90. dwError = LocateADUser(appContext, &(action->unlockAccount.user));
  91. }
  92. else {
  93. dwError = LocateADComputer(appContext, &(action->unlockAccount.computer));
  94. }
  95. ADT_BAIL_ON_ERROR_NP(dwError);
  96. dwError = LwAllocateMemory(2 * sizeof(AttrValsT), OUT_PPVOID(&avp));
  97. ADT_BAIL_ON_ALLOC_FAILURE(!dwError);
  98. avp[0].attr = "samAccountName";
  99. dwError = GetObjectAttrs(appContext,
  100. action->unlockAccount.user ?
  101. action->unlockAccount.user :
  102. action->unlockAccount.computer,
  103. avp);
  104. ADT_BAIL_ON_ERROR_NP(dwError);
  105. if(!avp[0].vals || !avp[0].vals[0]) {
  106. dwError = ADT_ERR_FAILED_AD_GET_ATTR;
  107. ADT_BAIL_ON_ERROR_NP(dwError);
  108. }
  109. PrintStderr(appContext,
  110. LogLevelVerbose,
  111. "%s: Modifying account properties of %s ...\n",
  112. appContext->actionName,
  113. avp[0].vals[0]);
  114. dwError = LwAllocateMemory(2 * sizeof(AttrValsT), OUT_PPVOID(&avpMod));
  115. ADT_BAIL_ON_ALLOC_FAILURE(!dwError);
  116. dwError = LwAllocateMemory(2 * sizeof(PSTR), OUT_PPVOID(&(avpMod[0].vals)));
  117. ADT_BAIL_ON_ALLOC_FAILURE(!dwError);
  118. avpMod[0].attr = "lockoutTime";
  119. avpMod[0].vals[0] = "0";
  120. dwError = ModifyADObject(appContext,
  121. action->unlockAccount.user ?
  122. action->unlockAccount.user :
  123. action->unlockAccount.computer,
  124. avpMod,
  125. 2);
  126. ADT_BAIL_ON_ERROR_NP(dwError);
  127. PrintStderr(appContext,
  128. LogLevelVerbose,
  129. "%s: Done modifying account properties.\n",
  130. appContext->actionName);
  131. if(appContext->gopts.isPrintDN) {
  132. if(!appContext->gopts.isQuiet) {
  133. PrintResult(appContext, LogLevelNone, "%s\n",
  134. action->unlockAccount.user ?
  135. action->unlockAccount.user :
  136. action->unlockAccount.computer
  137. );
  138. }
  139. }
  140. else {
  141. if (!appContext->gopts.isQuiet) {
  142. PrintResult(appContext,
  143. LogLevelNone,
  144. "Account %s has been unlocked\n",
  145. avp[0].vals[0]);
  146. }
  147. }
  148. cleanup:
  149. if (avpMod) {
  150. for (i = 0; avpMod[i].vals; ++i) {
  151. LW_SAFE_FREE_MEMORY(avpMod[i].vals);
  152. }
  153. LW_SAFE_FREE_MEMORY(avpMod);
  154. }
  155. if (avp) {
  156. for (i = 0; avp[i].vals; ++i) {
  157. for (j = 0; avp[i].vals[j]; ++j) {
  158. LW_SAFE_FREE_MEMORY(avp[i].vals[j]);
  159. }
  160. LW_SAFE_FREE_MEMORY(avp[i].vals);
  161. }
  162. LW_SAFE_FREE_MEMORY(avp);
  163. }
  164. return dwError;
  165. error:
  166. goto cleanup;
  167. }
  168. /**
  169. * Action clean up method.
  170. */
  171. DWORD CleanUpAdtUnlockAccountAction(IN AdtActionTP action)
  172. {
  173. return CleanUpBaseAction(action);
  174. }