==============================ModuleN===================================== | | | The program examples' source codes have been arranged in the same | | order that appeared in the Tutorial. This is unedited and unverified | | compilation. Published as is basis for educational, reacretional and | | brain teaser purposes. All trademarks, copyrights and IPs, wherever | | exist, are the sole property of their respective owner and/or | | holder. Any damage or loss by using the materials presented in this | | tutorial is USER responsibility. Part or full distribution, | | reproduction and modification is granted to any body. | | Copyright 2003-2005 © Tenouk, Inc. All rights reserved. | | Distributed through http://www.tenouk.com | | | | | =========================================================================== Compiled using VC++/VC++ .Net.....unmanaged... //******* mychangepass.cpp *********** #define _WIN32_WINNT 0x0501 // Wide character/Unicode based program #ifndef UNICODE #define UNICODE #endif #include #include #include // This program accept 4 arguments, a servername, a username, an old password // and new password. Make sure the old password is entered correctly int wmain(int argc, wchar_t *argv[]) { DWORD dwError = 0; NET_API_STATUS nStatus; // All parameters are required. if(argc != 5) { fwprintf(stderr, L"Usage: %s ServerName UserName OldPassword NewPassword\n", argv[0]); // Just exit, no further processing exit(1); } // Call the NetUserChangePassword function. nStatus = NetUserChangePassword(argv[1], argv[2], argv[3], argv[4]); // If the call succeeds, inform the user. if(nStatus == NERR_Success) fwprintf(stderr, L"Password for %s user has been changed successfully\n", argv[2]); // Otherwise, print the system error. else fprintf(stderr, "A system error has occurred: %d\n", nStatus); return 0; } ----------------------------------------------------------------------------------------------------- //********* mylgroup.cpp ********** // For WinXp #define _WIN32_WINNT 0x0501 // Wide character/Unicode based program #ifndef UNICODE #define UNICODE #endif #include #include #include // This program accept 3 arguments: servername, GroupName and Comment. int wmain(int argc, wchar_t *argv[]) { LOCALGROUP_INFO_1 lgi1; DWORD dwLevel = 1; DWORD dwError = 0; NET_API_STATUS nStatus; if(argc != 4) { fwprintf(stderr, L"Usage: %s ServerName GroupName Comment\n", argv[0]); // Just exit, no further processing exit(1); } // Set up the LOCALGROUP_INFO_1 structure. // Assign the group name and comment lgi1.lgrpi1_name = argv[2]; // Local group name lgi1.lgrpi1_comment = argv[3]; // Comment // Call the NetLocalGroupAdd() function, specifying level 1. nStatus = NetLocalGroupAdd(argv[1], dwLevel, (LPBYTE)&lgi1, &dwError); // If the call succeeds, inform the user. if(nStatus == NERR_Success) fwprintf(stderr, L"%s local group has been created successfully on %s machine.\n", argv[2], argv[1]); // Otherwise, print the system error. else fprintf(stderr, "A system error has occurred: %d\n", nStatus); return 0; } ----------------------------------------------------------------------------------------------------- //********* myaddmember.cpp ********** // For WinXp #define _WIN32_WINNT 0x0501 // Wide character/Unicode based program #ifndef UNICODE #define UNICODE #endif #include #include #include // This program accept 3 arguments: servername, GroupName and // MemberAccountName (DomainName\AccountName). int wmain(int argc, wchar_t *argv[]) { LOCALGROUP_MEMBERS_INFO_3 lgmi3; DWORD dwLevel = 3; DWORD totalEntries = 1; NET_API_STATUS nStatus; if(argc != 4) { fwprintf(stderr, L"Usage: %s ServerName GroupName MemberAccountName-(DomainName\\AccountName)\n", argv[0]); // Just exit, no further processing exit(1); } // Set up the LOCALGROUP_MEMBERS_INFO_3 structure. // Assign the member account name in form of // DomainName\AccountName lgmi3.lgrmi3_domainandname = argv[3]; // Call the NetLocalGroupAddMembers() function, specifying level 3. // Level 0 can use SID nStatus = NetLocalGroupAddMembers(argv[1], argv[2], dwLevel, (LPBYTE)&lgmi3, totalEntries); // If the call succeeds, inform the user. if(nStatus == NERR_Success) fwprintf(stderr, L"%s has been added successfully to %s on %s machine.\n", argv[3], argv[2], argv[1]); // Otherwise, print the system error. else fwprintf(stderr, L"A system error has occurred: %d\n", nStatus); return 0; } ------------------------------------------------------------------------------------------------------------------- //********* mydomusrgrp.cpp ********** // For Win 2000 #define _WIN32_WINNT 0x0500 // Wide character/Unicode based program #ifndef UNICODE #define UNICODE #endif #include #include #include // This program accept 3 arguments: servername, GroupName and // MemberAccountName (DomainName\AccountName). int wmain(int argc, wchar_t *argv[]) { LOCALGROUP_MEMBERS_INFO_3 lgmi3; DWORD dwLevel = 3; DWORD totalEntries = 1; NET_API_STATUS nStatus; if(argc != 4) { fwprintf(stderr, L"Usage: %s ServerName GroupName MemberAccountName-(DomainName\\AccountName)\n", argv[0]); // Just exit, no further processing exit(1); } // Set up the LOCALGROUP_MEMBERS_INFO_3 structure. // Assign the member account name in form of // DomainName\AccountName lgmi3.lgrmi3_domainandname = argv[3]; // Call the NetLocalGroupAddMembers() function, specifying level 3. // Level 0 can use SID nStatus = NetLocalGroupAddMembers(argv[1], argv[2], dwLevel, (LPBYTE)&lgmi3, totalEntries); // If the call succeeds, inform the user. if(nStatus == NERR_Success) fwprintf(stderr, L"%s has been added successfully to %s group on %s machine.\n", argv[3], argv[2], argv[1]); // Otherwise, print the system error. else fwprintf(stderr, L"A system error has occurred: %d\n", nStatus); return 0; } ---------------------------------------------------------------------------------------------------------------------- //********* myuserprog.cpp ********** // For WinXp #define _WIN32_WINNT 0x0501 // Wide character/Unicode based program #ifndef UNICODE #define UNICODE #endif #include #include #include // This program accept 3 arguments: servername, username and password. // It is run on local WinXp machine so the servername is the // local WinXp machine name or you can use NULL for the 1st parameter // of the NetUserAdd() and arguments, should be without the servername. int wmain(int argc, wchar_t *argv[]) { USER_INFO_1 ui; DWORD dwLevel = 1; DWORD dwError = 0; NET_API_STATUS nStatus; if(argc != 4) { fwprintf(stderr, L"Usage: %s ServerName UserName Password.\n", argv[0]); // or use fwprintf(stderr, L"Usage: %s UserName Password.\n", argv[0]); // for local machine and adjust other argc and argv[] array element appropriately. exit(1); } // Set up the USER_INFO_1 structure. // USER_PRIV_USER: name identifies an normal user // UF_SCRIPT: required for LAN Manager 2.0 and Windows NT and later. ui.usri1_name = argv[2]; // Username entered through command line ui.usri1_password = argv[4]; // Password ui.usri1_priv = USER_PRIV_USER; // As a normal/restricted user ui.usri1_home_dir = NULL; // No home directory ui.usri1_comment = L"This is a test normal user account using NetUserAdd"; // Comment ui.usri1_flags = UF_SCRIPT; // Must be UF_SCRIPT ui.usri1_script_path = NULL; // No script path // Call the NetUserAdd() function, specifying level 1. nStatus = NetUserAdd(argv[1], dwLevel, (LPBYTE)&ui, &dwError); // If the call succeeds, inform the user. if(nStatus == NERR_Success) { fwprintf(stderr, L"%s user has been successfully added on %s machine.\n", argv[2], argv[1]); fwprintf(stderr, L"Username: %s password: %s.\n", argv[2], argv[3]); } // Otherwise, print the system error. else fprintf(stderr, "A system error has occurred: %d\n", nStatus); return 0; } ---------------------------------------------------------------------------------------------------------- //********* myuserproglg.cpp ********** // For WinXp #define _WIN32_WINNT 0x0501 // Wide character/Unicode based program #ifndef UNICODE #define UNICODE #endif #include #include #include // This program accept 3 arguments: servername, GroupName and Comment. int wmain(int argc, wchar_t *argv[]) { LOCALGROUP_INFO_1 lgi1; DWORD dwLevel = 1; DWORD dwError = 0; NET_API_STATUS nStatus; if(argc != 4) { fwprintf(stderr, L"Usage: %s ServerName GroupName Comment\n", argv[0]); // Just exit, no further processing exit(1); } // Set up the LOCALGROUP_INFO_1 structure. // Assign the group name and comment lgi1.lgrpi1_name = argv[2]; // Local group name lgi1.lgrpi1_comment = argv[3]; // Comment // Call the NetLocalGroupAdd() function, specifying level 1. nStatus = NetLocalGroupAdd(argv[1], dwLevel, (LPBYTE)&lgi1, &dwError); // If the call succeeds, inform the user. if(nStatus == NERR_Success) fwprintf(stderr, L"%s local group has been created successfully on %s machine.\n", argv[2], argv[1]); // Otherwise, print the system error. else fprintf(stderr, "A system error has occurred: %d\n", nStatus); return 0; } ---------------------------------------------------------------------------------------------------------------- //********* myusrgrp.cpp ************ // Network management functions have their own // error codes... #define WIN32_WINNT 0x0500 #define UNICODE 1 #include #include #include #include #include #include #include NET_API_STATUS MyTestNet(LPWSTR lpszDomain, LPWSTR lpszUser, LPWSTR lpszPassword, LPWSTR lpszLocalGroup ) { USER_INFO_1 user_info; LOCALGROUP_INFO_1 localgroup_info; LOCALGROUP_MEMBERS_INFO_3 localgroup_members; LPWSTR lpszPrimaryDC = L"mawar"; NET_API_STATUS err = 0; DWORD parm_err = 0; // First get the name of the primary domain controller. // Make sure to free the returned buffer. err = NetGetDCName(L"mawar", // Local machine lpszDomain, // Domain name, if NULL use lpszPrimaryDC (LPBYTE *)&lpszPrimaryDC ); // Returned PDC if(err != 0) { printf("Error getting DC name: %d\n", err); return(err); } // Set up the USER_INFO_1 structure. user_info.usri1_name = lpszUser; user_info.usri1_password = lpszPassword; user_info.usri1_priv = USER_PRIV_USER; user_info.usri1_home_dir = TEXT(""); user_info.usri1_comment = TEXT("This is just a sample user lol!"); user_info.usri1_flags = UF_SCRIPT; user_info.usri1_script_path = TEXT(""); err = NetUserAdd(lpszPrimaryDC, // PDC name 1, // Level, use other level for more information (LPBYTE)&user_info, // Input buffer &parm_err); // Parameter in error switch (err) { case 0: printf("%ls user successfully created.\n", user_info.usri1_name); break; case NERR_UserExists: printf("%ls user already exists.\n", user_info.usri1_name); err = 0; break; case ERROR_INVALID_PARAMETER: { printf("Invalid Parameter Error adding user: Parameter Index = %d\n", parm_err); NetApiBufferFree(lpszPrimaryDC); return(err); } default: printf("Error adding %ls user: %d\n", user_info.usri1_name, err); NetApiBufferFree(lpszPrimaryDC); return(err); } // Set up the LOCALGROUP_INFO_1 structure. localgroup_info.lgrpi1_name = lpszLocalGroup; localgroup_info.lgrpi1_comment = TEXT("This is just a sample Local group."); err = NetLocalGroupAdd(lpszPrimaryDC, // PDC name 1, // Level (LPBYTE)&localgroup_info, // Input buffer &parm_err); // Parameter in error switch (err) { case 0: printf("%ls Local Group successfully created.\n", localgroup_info.lgrpi1_name); break; case ERROR_ALIAS_EXISTS: printf("%ls Local Group already exists.\n", localgroup_info.lgrpi1_name); err = 0; break; case ERROR_INVALID_PARAMETER: { printf("Invalid Parameter Error adding Local Group: Parameter Index = %d\n", err, parm_err); NetApiBufferFree(lpszPrimaryDC); return(err); } default: printf("Error adding %ls Local Group: %d\n", localgroup_info.lgrpi1_name, err); NetApiBufferFree(lpszPrimaryDC); return(err); } // Now add the user to the local group. localgroup_members.lgrmi3_domainandname = lpszUser; err = NetLocalGroupAddMembers(lpszPrimaryDC, // PDC name lpszLocalGroup, // Group name 3, // Name (LPBYTE)&localgroup_members, // Buffer 1); // Count switch(err) { case 0: printf("%ls user successfully added to %ls Local Group.\n", user_info.usri1_name, localgroup_info.lgrpi1_name); break; case ERROR_MEMBER_IN_ALIAS: printf("User %ls already in %ls Local Group.\n", user_info.usri1_name, localgroup_info.lgrpi1_name); err = 0; break; default: printf("Error adding %ls user to %ls Local Group: %d\n", user_info.usri1_name, localgroup_info.lgrpi1_name, err); break; } NetApiBufferFree(lpszPrimaryDC); return (err); } // This program run at command prompt, receives 4 arguments: The domain name, // user name (user account), user password and the group name. int wmain(int argc, wchar_t *argv[]) { NET_API_STATUS err = 0; if(argc != 5) { printf("Usage: %ls \n", argv[0]); exit (-1); } printf("Calling MyTestNet(): Create a user and a group then,\n"); printf("add the user to the group.\n"); printf("===================================================.\n"); err = MyTestNet(argv[1], // domain name argv[2], // user account argv[3], // password for the user argv[4]); // group name printf("MyTestNet() returned %d\n", err); return (0); } ----------------------------------------------------------------------------------------------------------------- //********* machineacct.cpp ********* // For Win 2000 #define _WIN32_WINNT 0x0500 // Wide character/Unicode based program #ifndef UNICODE #define UNICODE #endif #include #include #include BOOL AddMachineAccount(LPWSTR wTargetComputer, LPWSTR MachineAccount, DWORD AccountType) { LPWSTR wAccount; LPWSTR wPassword; USER_INFO_1 ui; DWORD cbAccount; DWORD cbLength; DWORD dwError; // Ensure a valid computer account type was passed. if(AccountType != UF_WORKSTATION_TRUST_ACCOUNT && AccountType != UF_SERVER_TRUST_ACCOUNT && AccountType != UF_INTERDOMAIN_TRUST_ACCOUNT) { SetLastError(ERROR_INVALID_PARAMETER); return FALSE; } else printf("Computer account type is valid.\n"); // Obtain the number of chars in computer account name. cbLength = cbAccount = lstrlenW(MachineAccount); // Ensure computer name doesn't exceed maximum length. if(cbLength > MAX_COMPUTERNAME_LENGTH) { SetLastError(ERROR_INVALID_ACCOUNT_NAME); return FALSE; } else printf("Computer name length is valid.\n"); // Allocate storage to contain Unicode representation of // computer account name + trailing $ + NULL. wAccount = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (cbAccount + 1 + 1) * sizeof(WCHAR) // Account + '$' + NULL ); if(wAccount == NULL) return FALSE; else printf("Memory allocation is OK.\n"); // Password is the computer account name converted to lowercase; // you will convert the passed MachineAccount in place. wPassword = MachineAccount; // Copy MachineAccount to the wAccount buffer allocated while // converting computer account name to uppercase. // Convert password (in place) to lowercase. while(cbAccount--) { wAccount[cbAccount] = towupper(MachineAccount[cbAccount]); wPassword[cbAccount] = towlower(wPassword[cbAccount]); } // Computer account names have a trailing Unicode '$'. wAccount[cbLength] = L'$'; wAccount[cbLength + 1] = L'\0'; // terminate the string // If the password is greater than the max allowed, truncate. if(cbLength > LM20_PWLEN) wPassword[LM20_PWLEN] = L'\0'; else printf("No truncation was done to the password, the length is OK, max is 14.\n"); // Initialize the USER_INFO_1 structure. ZeroMemory(&ui, sizeof(ui)); ui.usri1_name = wAccount; ui.usri1_password = wPassword; ui.usri1_flags = AccountType | UF_SCRIPT; ui.usri1_priv = USER_PRIV_USER; ui.usri1_comment = L"A virtual machine created by NetUserAdd()..."; dwError = NetUserAdd( wTargetComputer, // target computer name 1, // info level (LPBYTE) &ui, // buffer NULL ); // Release the allocated memory. if(wAccount) HeapFree(GetProcessHeap(), 0, wAccount); // Indicate whether the function was successful. if(dwError == NO_ERROR) { printf("%ls computer account successfully created on %ls DC.\n", MachineAccount, wTargetComputer); return TRUE; } else { SetLastError(dwError); return FALSE; } } // This program run at command prompt, receives 2 arguments: The target server // and the machine account name. int wmain(int argc, wchar_t *argv[]) { if(argc != 3) { printf("Usage: %s .\n", argv[0]); exit (-1); } DWORD AccountType = UF_WORKSTATION_TRUST_ACCOUNT; BOOL Test = AddMachineAccount(argv[1], argv[2], AccountType); printf("The return value is: %u\n", Test); return 0; } =========================================================================================================