Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1683383
  • 博文数量: 584
  • 博客积分: 13857
  • 博客等级: 上将
  • 技术积分: 11883
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-16 09:34

分类: WINDOWS

2011-03-28 11:05:53

Creating a New Computer Account

The following code sample demonstrates how to create a new computer account using the function.

The following are considerations for managing computer accounts:

  • The computer account name should be all uppercase for consistency with account management utilities.
  • A computer account name always has a trailing dollar sign ($). Any functions used to manage computer accounts must build the computer name such that the last character of the computer account name is a dollar sign ($). For interdomain trust, the account name is TrustingDomainName$.
  • The maximum computer name length is MAX_COMPUTERNAME_LENGTH (15). This length does not include the trailing dollar sign ($).
  • The password for a new computer account should be the lowercase representation of the computer account name, without the trailing dollar sign ($). For interdomain trust, the password can be an arbitrary value that matches the value specified on the trust side of the relationship.
  • The maximum password length is LM20_PWLEN (14). The password should be truncated to this length if the computer account name exceeds this length.
  • The password provided at computer-account-creation time is valid only until the computer account becomes active on the domain. A new password is established during trust relationship activation.
  1. #include <windows.h>
  2. #include <lm.h>
  3. #pragma comment(lib, "netapi32.lib")

  4. BOOL AddMachineAccount(
  5.     LPWSTR wTargetComputer,
  6.     LPWSTR MachineAccount,
  7.     DWORD AccountType
  8.     )
  9. {
  10.     LPWSTR wAccount;
  11.     LPWSTR wPassword;
  12.     USER_INFO_1 ui;
  13.     DWORD cbAccount;
  14.     DWORD cbLength;
  15.     DWORD dwError;

  16.     //
  17.     // Ensure a valid computer account type was passed.
  18.     //
  19.     if (AccountType != UF_WORKSTATION_TRUST_ACCOUNT &&
  20.         AccountType != UF_SERVER_TRUST_ACCOUNT &&
  21.         AccountType != UF_INTERDOMAIN_TRUST_ACCOUNT
  22.         )
  23.     {
  24.         SetLastError(ERROR_INVALID_PARAMETER);
  25.         return FALSE;
  26.     }

  27.     //
  28.     // Obtain number of chars in computer account name.
  29.     //
  30.     cbLength = cbAccount = lstrlenW(MachineAccount);

  31.     //
  32.     // Ensure computer name doesn't exceed maximum length.
  33.     //
  34.     if(cbLength > MAX_COMPUTERNAME_LENGTH) {
  35.         SetLastError(ERROR_INVALID_ACCOUNT_NAME);
  36.         return FALSE;
  37.     }

  38.     //
  39.     // Allocate storage to contain Unicode representation of
  40.     // computer account name + trailing $ + NULL.
  41.     //
  42.     wAccount=(LPWSTR)HeapAlloc(GetProcessHeap(), 0,
  43.         (cbAccount + 1 + 1) * sizeof(WCHAR) // Account + '$' + NULL
  44.         );

  45.     if(wAccount == NULL) return FALSE;

  46.     //
  47.     // Password is the computer account name converted to lowercase;
  48.     // you will convert the passed MachineAccount in place.
  49.     //
  50.     wPassword = MachineAccount;

  51.     //
  52.     // Copy MachineAccount to the wAccount buffer allocated while
  53.     // converting computer account name to uppercase.
  54.     // Convert password (in place) to lowercase.
  55.     //
  56.     while(cbAccount--) {
  57.         wAccount[cbAccount] = towupper( MachineAccount[cbAccount] );
  58.         wPassword[cbAccount] = towlower( wPassword[cbAccount] );
  59.     }

  60.     //
  61.     // Computer account names have a trailing Unicode '$'.
  62.     //
  63.     wAccount[cbLength] = L'$';
  64.     wAccount[cbLength + 1] = L'\0'; // terminate the string

  65.     //
  66.     // If the password is greater than the max allowed, truncate.
  67.     //
  68.     if(cbLength > LM20_PWLEN) wPassword[LM20_PWLEN] = L'\0';

  69.     //
  70.     // Initialize the USER_INFO_1 structure.
  71.     //
  72.     ZeroMemory(&ui, sizeof(ui));

  73.     ui.usri1_name = wAccount;
  74.     ui.usri1_password = wPassword;

  75.     ui.usri1_flags = AccountType | UF_SCRIPT;
  76.     ui.usri1_priv = USER_PRIV_USER;

  77.     dwError=NetUserAdd(
  78.                 wTargetComputer, // target computer name
  79.                 1, // info level
  80.                 (LPBYTE) &ui, // buffer
  81.                 NULL
  82.                 );

  83.     //
  84.     // Free allocated memory.
  85.     //
  86.     if(wAccount) HeapFree(GetProcessHeap(), 0, wAccount);

  87.     //
  88.     // Indicate whether the function was successful.
  89.     //
  90.     if(dwError == NO_ERROR)
  91.         return TRUE;
  92.     else {
  93.         SetLastError(dwError);
  94.         return FALSE;
  95.     }
  96. }


The user that calls the account management functions must have Administrator privilege on the target computer. In the case of existing computer accounts, the creator of the account can manage the account, regardless of administrative membership. For more information about calling functions that require administrator privileges, see .

The SeMachineAccountPrivilege can be granted on the target computer to give specified users the ability to create computer accounts. This gives non-administrators the ability to create computer accounts. The caller needs to enable this privilege prior to adding the computer account. For more information about account privileges, see and .

 

 

阅读(1257) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~