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
cmdComputer(INT argc,WCHAR ** argv)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             PrintMessageString(4381);
53             ConPuts(StdOut, L"\n");
54             PrintNetMessage(MSG_COMPUTER_SYNTAX);
55             return 0;
56         }
57 
58         if (_wcsicmp(argv[i], L"/help") == 0)
59         {
60             /* Print full help text*/
61             PrintMessageString(4381);
62             ConPuts(StdOut, L"\n");
63             PrintNetMessage(MSG_COMPUTER_SYNTAX);
64             PrintNetMessage(MSG_COMPUTER_HELP);
65             return 0;
66         }
67 
68         if (_wcsicmp(argv[i], L"/add") == 0)
69         {
70             bAdd = TRUE;
71             continue;
72         }
73         else if (_wcsicmp(argv[i], L"/del") == 0)
74         {
75             bDelete = TRUE;
76             continue;
77         }
78         else
79         {
80             PrintErrorMessage(3506/*, argv[i]*/);
81             return 1;
82         }
83     }
84 
85     if (pComputerName == NULL ||
86         (bAdd == FALSE && bDelete == FALSE) ||
87         (bAdd == TRUE && bDelete == TRUE))
88     {
89         PrintMessageString(4381);
90         ConPuts(StdOut, L"\n");
91         PrintNetMessage(MSG_COMPUTER_SYNTAX);
92         return 1;
93     }
94 
95     /*
96      * Create the computer account name:
97      *  Skip the leading '\\' and appand a '$'.
98      */
99     wcscpy(ComputerAccountName, &pComputerName[2]);
100     wcscat(ComputerAccountName, L"$");
101 
102     if (bAdd)
103     {
104         /*
105          * Create the computer password:
106          *   Skip the leading '\\', shorten to a maximum of 14 characters
107          *   and convert to lower case
108          */
109         wcsncpy(ComputerPassword, &pComputerName[2], LM20_PWLEN);
110         ComputerPassword[LM20_PWLEN] = UNICODE_NULL;
111         _wcslwr(ComputerPassword);
112 
113         /* Set the account data */
114         UserInfo.usri1_name = ComputerAccountName;
115         UserInfo.usri1_password = ComputerPassword;
116         UserInfo.usri1_password_age = 0;
117         UserInfo.usri1_priv = USER_PRIV_USER;
118         UserInfo.usri1_home_dir = NULL;
119         UserInfo.usri1_comment = NULL;
120         UserInfo.usri1_flags = UF_SCRIPT | UF_WORKSTATION_TRUST_ACCOUNT;
121         UserInfo.usri1_script_path = NULL;
122 
123         /* Add the computer account */
124         Status = NetUserAdd(NULL,
125                             1,
126                             (LPBYTE)&UserInfo,
127                             NULL);
128     }
129     else if (bDelete)
130     {
131         /* Delete the coputer account */
132         Status = NetUserDel(NULL,
133                             ComputerAccountName);
134     }
135 
136     if (Status == NERR_Success)
137     {
138         PrintErrorMessage(ERROR_SUCCESS);
139     }
140     else
141     {
142         PrintErrorMessage(Status);
143         result = 1;
144     }
145 
146     return result;
147 }
148 
149 /* EOF */
150