2 * PROJECT: Local Security Authority Server DLL
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/lsasrv/session.c
5 * PURPOSE: Logon session management routines
6 * COPYRIGHT: Copyright 2013 Eric Kohl
11 typedef struct _LSAP_LOGON_SESSION
15 } LSAP_LOGON_SESSION
, *PLSAP_LOGON_SESSION
;
18 /* GLOBALS *****************************************************************/
20 LIST_ENTRY SessionListHead
;
23 /* FUNCTIONS ***************************************************************/
26 LsapInitLogonSessions(VOID
)
28 InitializeListHead(&SessionListHead
);
35 LsapGetLogonSession(IN PLUID LogonId
)
37 PLIST_ENTRY SessionEntry
;
38 PLSAP_LOGON_SESSION CurrentSession
;
40 SessionEntry
= SessionListHead
.Flink
;
41 while (SessionEntry
!= &SessionListHead
)
43 CurrentSession
= CONTAINING_RECORD(SessionEntry
,
46 if (RtlEqualLuid(&CurrentSession
->LogonId
, LogonId
))
47 return CurrentSession
;
49 SessionEntry
= SessionEntry
->Flink
;
57 LsapSetLogonSessionData(IN PLUID LogonId
)
59 PLSAP_LOGON_SESSION Session
;
63 Session
= LsapGetLogonSession(LogonId
);
65 return STATUS_NO_SUCH_LOGON_SESSION
;
68 return STATUS_SUCCESS
;
74 LsapCreateLogonSession(IN PLUID LogonId
)
76 PLSAP_LOGON_SESSION Session
;
80 /* Fail, if a session already exists */
81 if (LsapGetLogonSession(LogonId
) != NULL
)
82 return STATUS_LOGON_SESSION_COLLISION
;
84 /* Allocate a new session entry */
85 Session
= RtlAllocateHeap(RtlGetProcessHeap(),
87 sizeof(LSAP_LOGON_SESSION
));
89 return STATUS_INSUFFICIENT_RESOURCES
;
91 /* Initialize the session entry */
92 RtlCopyLuid(&Session
->LogonId
, LogonId
);
94 /* Insert the new session into the session list */
95 InsertTailList(&SessionListHead
, &Session
->Entry
);
98 return STATUS_SUCCESS
;
104 LsapDeleteLogonSession(IN PLUID LogonId
)
106 PLSAP_LOGON_SESSION Session
;
110 /* Fail, if the session does not exist */
111 Session
= LsapGetLogonSession(LogonId
);
113 return STATUS_NO_SUCH_LOGON_SESSION
;
115 /* Remove the session entry from the list */
116 RemoveEntryList(&Session
->Entry
);
119 /* Free the session entry */
120 RtlFreeHeap(RtlGetProcessHeap(), 0, Session
);
122 return STATUS_SUCCESS
;
127 LsapEnumLogonSessions(IN OUT PLSA_API_MSG RequestMsg
)
129 OBJECT_ATTRIBUTES ObjectAttributes
;
130 HANDLE ProcessHandle
= NULL
;
131 PLIST_ENTRY SessionEntry
;
132 PLSAP_LOGON_SESSION CurrentSession
;
134 ULONG i
, Length
, MemSize
;
135 PVOID ClientBaseAddress
= NULL
;
138 TRACE("LsapEnumLogonSessions()\n");
140 Length
= SessionCount
* sizeof(LUID
);
141 SessionList
= RtlAllocateHeap(RtlGetProcessHeap(),
144 if (SessionList
== NULL
)
145 return STATUS_INSUFFICIENT_RESOURCES
;
148 SessionEntry
= SessionListHead
.Flink
;
149 while (SessionEntry
!= &SessionListHead
)
151 CurrentSession
= CONTAINING_RECORD(SessionEntry
,
155 RtlCopyLuid(&SessionList
[i
],
156 &CurrentSession
->LogonId
);
158 SessionEntry
= SessionEntry
->Flink
;
162 InitializeObjectAttributes(&ObjectAttributes
,
168 Status
= NtOpenProcess(&ProcessHandle
,
169 PROCESS_VM_READ
| PROCESS_VM_WRITE
| PROCESS_VM_OPERATION
| PROCESS_DUP_HANDLE
,
171 &RequestMsg
->h
.ClientId
);
172 if (!NT_SUCCESS(Status
))
174 TRACE("NtOpenProcess() failed (Status %lx)\n", Status
);
179 Status
= NtAllocateVirtualMemory(ProcessHandle
,
185 if (!NT_SUCCESS(Status
))
187 TRACE("NtAllocateVirtualMemory() failed (Status %lx)\n", Status
);
191 Status
= NtWriteVirtualMemory(ProcessHandle
,
196 if (!NT_SUCCESS(Status
))
198 TRACE("NtWriteVirtualMemory() failed (Status %lx)\n", Status
);
202 RequestMsg
->EnumLogonSessions
.Reply
.LogonSessionCount
= SessionCount
;
203 RequestMsg
->EnumLogonSessions
.Reply
.LogonSessionBuffer
= ClientBaseAddress
;
206 if (ProcessHandle
!= NULL
)
207 NtClose(ProcessHandle
);
209 if (SessionList
!= NULL
)
210 RtlFreeHeap(RtlGetProcessHeap(), 0, SessionList
);