1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS net command
4  * FILE:            base/applications/network/net/cmdSession.c
5  * PROGRAMMERS:     Eric Kohl <eric.kohl@reactos.org>
6  */
7 
8 #include "net.h"
9 
10 static
11 VOID
SecondsToDurationString(_Out_ PWSTR DurationString,_In_ size_t DurationStringSize,_In_ DWORD dwDuration)12 SecondsToDurationString(
13     _Out_ PWSTR DurationString,
14     _In_ size_t DurationStringSize,
15     _In_ DWORD dwDuration)
16 {
17     DWORD dwHours, dwRemainingSeconds, dwMinutes, dwSeconds;
18 
19     dwHours = dwDuration / 3600;
20     dwRemainingSeconds = dwDuration % 3600;
21     dwMinutes = dwRemainingSeconds / 60;
22     dwSeconds = dwRemainingSeconds % 60;
23 
24     StringCchPrintfW(DurationString, DurationStringSize, L"%02lu:%02lu:%02lu", dwHours, dwMinutes, dwSeconds);
25 }
26 
27 
28 NET_API_STATUS
EnumSessions(_In_ PWSTR pszComputerName,_In_ BOOL bList)29 EnumSessions(
30     _In_ PWSTR pszComputerName,
31     _In_ BOOL bList)
32 {
33     PSESSION_INFO_2 pBuffer = NULL;
34     WCHAR DurationBuffer[10];
35     DWORD dwRead = 0, dwTotal = 0, i;
36     DWORD ResumeHandle = 0;
37     NET_API_STATUS Status;
38 
39     Status = NetSessionEnum(pszComputerName,
40                             NULL,
41                             NULL,
42                             2,
43                             (LPBYTE*)&pBuffer,
44                             MAX_PREFERRED_LENGTH,
45                             &dwRead,
46                             &dwTotal,
47                             &ResumeHandle);
48     if ((Status != NERR_Success) && (Status != ERROR_MORE_DATA))
49     {
50 //        PrintMessageStringV(3502, Status);
51         ConPrintf(StdOut, L"System error %lu has occurred.\n\n", Status);
52         return Status;
53     }
54 
55     if (dwTotal == 0)
56     {
57         PrintMessageString(3683);
58     }
59     else
60     {
61         ConPuts(StdOut, L"\n");
62         PrintMessageString(4750);
63         PrintPadding(L'-', 79);
64         ConPuts(StdOut, L"\n");
65 
66         for (i = 0; i < dwRead; i++)
67         {
68             if (pBuffer[i].sesi2_cname)
69             {
70                 SecondsToDurationString(DurationBuffer,
71                                         ARRAYSIZE(DurationBuffer),
72                                         pBuffer[i].sesi2_idle_time);
73 
74                 ConPrintf(StdOut, L"%-22.22s %-20.20s %-17.17s %-5lu %-8.8s\n",
75                           pBuffer[i].sesi2_cname,
76                           pBuffer[i].sesi2_username,
77                           pBuffer[i].sesi2_cltype_name,
78                           pBuffer[i].sesi2_num_opens,
79                           DurationBuffer);
80             }
81         }
82     }
83 
84     NetApiBufferFree(pBuffer);
85 
86     return NERR_Success;
87 }
88 
89 
90 INT
cmdSession(_In_ INT argc,_In_ WCHAR ** argv)91 cmdSession(
92     _In_ INT argc,
93     _In_ WCHAR **argv)
94 {
95     PWSTR pszComputerName = NULL;
96     BOOL bList = FALSE;
97     BOOL bDelete = FALSE;
98     INT i = 0;
99     NET_API_STATUS Status;
100     INT result = 0;
101 
102     for (i = 2; i < argc; i++)
103     {
104         if (argv[i][0] == L'\\' && argv[i][1] == L'\\' && pszComputerName == NULL)
105         {
106             pszComputerName = argv[i];
107             i++;
108         }
109         else if (_wcsicmp(argv[i], L"/list") == 0)
110         {
111             bList = TRUE;
112             continue;
113         }
114         else if (_wcsicmp(argv[i], L"/delete") == 0)
115         {
116             bDelete = TRUE;
117             continue;
118         }
119         else if (_wcsicmp(argv[i], L"/help") == 0)
120         {
121             PrintMessageString(4381);
122             ConPuts(StdOut, L"\n");
123             PrintNetMessage(MSG_SESSION_SYNTAX);
124             PrintNetMessage(MSG_SESSION_HELP);
125             return 0;
126         }
127         else
128         {
129             PrintMessageString(4381);
130             ConPuts(StdOut, L"\n");
131             PrintNetMessage(MSG_SESSION_SYNTAX);
132             return 1;
133         }
134     }
135 
136     if (bDelete)
137         Status = NetSessionDel(pszComputerName, NULL, NULL);
138     else
139         Status = EnumSessions(pszComputerName, bList);
140 
141     if (Status == NERR_Success)
142     {
143         PrintErrorMessage(ERROR_SUCCESS);
144     }
145     else
146     {
147         PrintErrorMessage(Status);
148         result = 1;
149     }
150 
151     return result;
152 }
153 
154 /* EOF */
155