==============================ModuleM===================================== | | | 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... //********* myadduser.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, user account 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 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; } ---------------------------------------------------------------------------------------------------------------- //********* mydomadduser.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, user account and password. // It is run locally on Win 2000 DC of kpts.org domain. The DC server name is tutorkpts. // May be NULL if it is run on local... int wmain(int argc, wchar_t *argv[]) { USER_INFO_1 ui; DWORD dwLevel = 1; DWORD dwError = 0; NET_API_STATUS nStatus; if(argc != 4) { // For NT40 need to append \\Servername 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 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, through command line ui.usri1_priv = USER_PRIV_USER; // As a normal/restricted user ui.usri1_home_dir = L""; // No home directory, just dummy or point it to NULL 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 = L""; // No script path, just dummy or point it to NULL // 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; } ---------------------------------------------------------------------------------------------------------------- //***** myenumerateuser.cpp ******** // For WinXp #define _WIN32_WINNT 0x0501 // Wide character/Unicode based program #ifndef UNICODE #define UNICODE #endif #include #include #include #include // This program accept 1 argument, a servername // else, local computer is assumed int wmain(int argc, wchar_t *argv[]) { // Use LPUSER_INFO_1 type for more Level 1 detail info. LPUSER_INFO_1 pBuf = NULL; LPUSER_INFO_1 pTmpBuf; DWORD dwLevel = 1; DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH; DWORD dwEntriesRead = 0; DWORD dwTotalEntries = 0; DWORD dwResumeHandle = 0; DWORD i; DWORD dwTotalCount = 0; NET_API_STATUS nStatus; LPTSTR pszServerName = NULL; // Some prompt // If argc == 1, local computer is assumed if(argc > 2) { fwprintf(stderr, L"Usage: %s [ServerName]\n", argv[0]); exit(1); } // The server name is supplied so it is not the default local computer. if(argc == 2) pszServerName = argv[1]; wprintf(L"\nUser accounts, flags and their privileges on %s machine: \n", pszServerName); // Call the NetUserEnum() function, specifying level 0, // enumerate global user account types only. do // begin do { // If pszServerName is NULL, local pc is assumed, here we use servername but it // is just a local WinXP machine. Try other entries also such as password etc. nStatus = NetUserEnum(pszServerName, dwLevel, FILTER_NORMAL_ACCOUNT, // global users, change it appropriately // to dig other info (LPBYTE*)&pBuf, dwPrefMaxLen, &dwEntriesRead, &dwTotalEntries, &dwResumeHandle); // If the call succeeds, if((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA)) { if((pTmpBuf = pBuf) != NULL) { // Loop through the entries. for(i = 0; (i < dwEntriesRead); i++) { // Check buffer assert(pTmpBuf != NULL); if(pTmpBuf == NULL) { fprintf(stderr, "An access violation has occurred\n"); break; } // Print the name of the user account, flag and their privilege. wprintf(L"... %s::%0x::%0x\n", pTmpBuf->usri1_name, pTmpBuf->usri1_flags, pTmpBuf->usri1_priv); pTmpBuf++; dwTotalCount++; } } } // Otherwise, print the system error. else fprintf(stderr, "A system error has occurred: %d\n", nStatus); // Release the allocated buffer. if(pBuf != NULL) { NetApiBufferFree(pBuf); pBuf = NULL; } } // Continue to call NetUserEnum while there are more entries. while (nStatus == ERROR_MORE_DATA); // end do // Check again for allocated memory. if(pBuf != NULL) NetApiBufferFree(pBuf); // Print the final count of users enumerated. fprintf(stderr, "\nTotal of %d entries enumerated\n", dwTotalCount); return 0; } ---------------------------------------------------------------------------------------------------------------- //********** mysetuserinfo.cpp ************ // For WinXp #define _WIN32_WINNT 0x0501 // Wide character/Unicode based program #ifndef UNICODE #define UNICODE #endif #include #include #include // This program accept 2 arguments, a servername and a user account to be disabled int wmain(int argc, wchar_t *argv[]) { // Disable the user account DWORD dwLevel = 1008; USER_INFO_1008 ui; NET_API_STATUS nStatus; // If not enough arguments supplied. if(argc != 3) { fwprintf(stderr, L"Usage: %s ServerName UserName\n", argv[0]); // Just exit, no further processing. exit(1); } // Fill in the USER_INFO_1008 structure member. // UF_SCRIPT: required for LAN Manager 2.0 and Windows NT and later. // UF_ACCOUNTDISABLE: disable the user account. ui.usri1008_flags = (UF_SCRIPT | UF_ACCOUNTDISABLE); // Call the NetUserSetInfo() function // to disable the account, specifying level 1008. nStatus = NetUserSetInfo(argv[1], argv[2], dwLevel, (LPBYTE)&ui, NULL); // Display the result of the call. if(nStatus == NERR_Success) fwprintf(stderr, L"User account %s has been disabled.\n", argv[2]); else fprintf(stderr, "A system error has occurred: %d\n", nStatus); return 0; } ---------------------------------------------------------------------------------------------------------------- //******** mysetuserinfo2.cpp ******* // For WinXp #define _WIN32_WINNT 0x0501 // Wide character/Unicode based program #ifndef UNICODE #define UNICODE #endif #include #include #include // This program accept 2 arguments, a servername and a user account int wmain(int argc, wchar_t *argv[]) { DWORD dwLevel = 1008; USER_INFO_1008 ui; NET_API_STATUS nStatus; // If not enough arguments supplied. if(argc != 3) { fwprintf(stderr, L"Usage: %s ServerName UserName\n", argv[0]); // Just exit, no further processing. exit(1); } // Fill in the USER_INFO_1008 structure member. // UF_SCRIPT: required for LAN Manager 2.0 and Windows NT and later. ui.usri1008_flags = (UF_SCRIPT | UF_PASSWD_CANT_CHANGE | UF_DONT_EXPIRE_PASSWD); // Call the NetUserSetInfo() function // to set Password never expires and User cannot change password. nStatus = NetUserSetInfo(argv[1], argv[2], dwLevel, (LPBYTE)&ui, NULL); // Display the result of the call. if(nStatus == NERR_Success) { fwprintf(stderr, L"%s user account has been set for:\n", argv[2]); fwprintf(stderr, L"-- Password never expires.\n"); fwprintf(stderr, L"-- User cannot change password.\n"); } else fprintf(stderr, "A system error has occurred: %d\n", nStatus); return 0; } ---------------------------------------------------------------------------------------------------------------- //******** mydomsetuserinfo2.cpp ******* // For Win2000 #define _WIN32_WINNT 0x0500 // Wide character/Unicode based program #ifndef UNICODE #define UNICODE #endif #include #include #include // This program accept 2 arguments, a servername and a username int wmain(int argc, wchar_t *argv[]) { DWORD dwLevel = 1008; USER_INFO_1008 ui; NET_API_STATUS nStatus; // If not enough arguments supplied. if(argc != 3) { fwprintf(stderr, L"Usage: %s ServerName UserName\n", argv[0]); // Just exit, no further processing. exit(1); } // Fill in the USER_INFO_1008 structure member. // UF_SCRIPT: required for LAN Manager 2.0 and Windows NT and later. ui.usri1008_flags = (UF_SCRIPT | UF_PASSWD_CANT_CHANGE | UF_DONT_EXPIRE_PASSWD); // Call the NetUserSetInfo() function // to set Password never expires and User cannot change password. nStatus = NetUserSetInfo(argv[1], argv[2], dwLevel, (LPBYTE)&ui, NULL); // Display the result of the call. if(nStatus == NERR_Success) { fwprintf(stderr, L"%s user account has been set for:\n", argv[2]); fwprintf(stderr, L"-- Password never expires.\n"); fwprintf(stderr, L"-- User cannot change password.\n"); } else fprintf(stderr, "A system error has occurred: %d\n", nStatus); return 0; } =======================================================================================================