1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS net command
4  * FILE:            base/applications/network/net/cmdComputer.c
5  * PROGRAMMERS:     Eric Kohl <eric.kohl@reactos.org>
6  */
7 
8 #include "net.h"
9 
10 INT
11 cmdComputer(
12     INT argc,
13     WCHAR **argv)
14 {
15     WCHAR ComputerAccountName[MAX_PATH + 2];
16     WCHAR ComputerPassword[LM20_PWLEN + 1];
17     USER_INFO_1 UserInfo;
18     INT i, result = 0;
19     BOOL bAdd = FALSE;
20     BOOL bDelete = FALSE;
21     PWSTR pComputerName = NULL;
22     NET_API_STATUS Status = NERR_Success;
23 /*
24     OSVERSIONINFOEX VersionInfo;
25 
26     VersionInfo.dwOSVersionInfoSize = sizeof(VersionInfo);
27     if (!GetVersionEx((LPOSVERSIONINFO)&VersionInfo))
28     {
29         PrintErrorMessage(GetLastError());
30         return 1;
31     }
32 
33     if (VersionInfo.wProductType != VER_NT_DOMAIN_CONTROLLER)
34     {
35         PrintErrorMessage(3515);
36         return 1;
37     }
38 */
39 
40     i = 2;
41     if (argc > 2 && argv[i][0] == L'\\' && argv[i][1] == L'\\')
42     {
43         pComputerName = argv[i];
44         i++;
45     }
46 
47     for (; i < argc; i++)
48     {
49         if (_wcsicmp(argv[i], L"help") == 0)
50         {
51             /* Print short syntax help */
52             ConResPuts(StdOut, IDS_GENERIC_SYNTAX);
53             PrintNetMessage(MSG_COMPUTER_SYNTAX);
54             return 0;
55         }
56 
57         if (_wcsicmp(argv[i], L"/help") == 0)
58         {
59             /* Print full help text*/
60             ConResPuts(StdOut, IDS_GENERIC_SYNTAX);
61             PrintNetMessage(MSG_COMPUTER_SYNTAX);
62             PrintNetMessage(MSG_COMPUTER_HELP);
63             return 0;
64         }
65 
66         if (_wcsicmp(argv[i], L"/add") == 0)
67         {
68             bAdd = TRUE;
69             continue;
70         }
71         else if (_wcsicmp(argv[i], L"/del") == 0)
72         {
73             bDelete = TRUE;
74             continue;
75         }
76         else
77         {
78             PrintErrorMessage(3506/*, argv[i]*/);
79             return 1;
80         }
81     }
82 
83     if (pComputerName == NULL ||
84         (bAdd == FALSE && bDelete == FALSE) ||
85         (bAdd == TRUE && bDelete == TRUE))
86     {
87         ConResPuts(StdOut, IDS_GENERIC_SYNTAX);
88         PrintNetMessage(MSG_COMPUTER_SYNTAX);
89         return 1;
90     }
91 
92     /*
93      * Create the computer account name:
94      *  Skip the leading '\\' and appand a '$'.
95      */
96     wcscpy(ComputerAccountName, &pComputerName[2]);
97     wcscat(ComputerAccountName, L"$");
98 
99     if (bAdd)
100     {
101         /*
102          * Create the computer password:
103          *   Skip the leading '\\', shorten to a maximum of 14 characters
104          *   and convert to lower case
105          */
106         wcsncpy(ComputerPassword, &pComputerName[2], LM20_PWLEN);
107         ComputerPassword[LM20_PWLEN] = UNICODE_NULL;
108         _wcslwr(ComputerPassword);
109 
110         /* Set the account data */
111         UserInfo.usri1_name = ComputerAccountName;
112         UserInfo.usri1_password = ComputerPassword;
113         UserInfo.usri1_password_age = 0;
114         UserInfo.usri1_priv = USER_PRIV_USER;
115         UserInfo.usri1_home_dir = NULL;
116         UserInfo.usri1_comment = NULL;
117         UserInfo.usri1_flags = UF_SCRIPT | UF_WORKSTATION_TRUST_ACCOUNT;
118         UserInfo.usri1_script_path = NULL;
119 
120         /* Add the computer account */
121         Status = NetUserAdd(NULL,
122                             1,
123                             (LPBYTE)&UserInfo,
124                             NULL);
125     }
126     else if (bDelete)
127     {
128         /* Delete the coputer account */
129         Status = NetUserDel(NULL,
130                             ComputerAccountName);
131     }
132 
133     if (Status == NERR_Success)
134     {
135         PrintErrorMessage(ERROR_SUCCESS);
136     }
137     else
138     {
139         PrintErrorMessage(Status);
140         result = 1;
141     }
142 
143     return result;
144 }
145 
146 /* EOF */
147