2013-05-16 04:06:05 +04:00
|
|
|
/**
|
|
|
|
* xrdp: A Remote Desktop Protocol server.
|
|
|
|
*
|
|
|
|
* Copyright (C) Laxmikant Rashinkar 2013 LK.Rashinkar@gmail.com
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* manage I/O for redirected file system and devices
|
|
|
|
*/
|
|
|
|
|
2017-03-03 07:33:23 +03:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
|
|
|
#include <config_ac.h>
|
|
|
|
#endif
|
|
|
|
|
2020-10-08 04:55:08 +03:00
|
|
|
#include "chansrv.h"
|
2013-05-16 04:06:05 +04:00
|
|
|
#include "parse.h"
|
|
|
|
#include "os_calls.h"
|
2020-12-21 15:36:00 +03:00
|
|
|
#include "string_calls.h"
|
2013-05-16 04:06:05 +04:00
|
|
|
#include "irp.h"
|
|
|
|
|
|
|
|
IRP *g_irp_head = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new IRP and append to linked list
|
|
|
|
*
|
|
|
|
* @return new IRP or NULL on error
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2020-11-30 03:36:20 +03:00
|
|
|
IRP *devredir_irp_new(void)
|
2013-05-16 04:06:05 +04:00
|
|
|
{
|
|
|
|
IRP *irp;
|
|
|
|
IRP *irp_last;
|
|
|
|
|
2020-10-08 04:55:08 +03:00
|
|
|
LOG_DEVEL(LOG_LEVEL_DEBUG, "entered");
|
2013-05-16 04:06:05 +04:00
|
|
|
|
|
|
|
/* create new IRP */
|
2016-06-22 02:30:18 +03:00
|
|
|
irp = g_new0(IRP, 1);
|
|
|
|
if (irp == NULL)
|
2013-05-16 04:06:05 +04:00
|
|
|
{
|
2020-10-08 04:55:08 +03:00
|
|
|
LOG_DEVEL(LOG_LEVEL_ERROR, "system out of memory!");
|
2013-05-16 04:06:05 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* insert at end of linked list */
|
|
|
|
if ((irp_last = devredir_irp_get_last()) == NULL)
|
|
|
|
{
|
|
|
|
/* list is empty, this is the first entry */
|
|
|
|
g_irp_head = irp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
irp_last->next = irp;
|
|
|
|
irp->prev = irp_last;
|
|
|
|
}
|
|
|
|
|
2020-10-08 04:55:08 +03:00
|
|
|
LOG_DEVEL(LOG_LEVEL_DEBUG, "new IRP=%p", irp);
|
2013-05-16 04:06:05 +04:00
|
|
|
return irp;
|
|
|
|
}
|
|
|
|
|
2013-06-03 02:33:51 +04:00
|
|
|
/**
|
2019-11-14 17:43:57 +03:00
|
|
|
* Create a new IRP with a copied pathname, and append to linked list.
|
|
|
|
*
|
|
|
|
* Allocation is made in such a way that the IRP can be freed with a single
|
|
|
|
* free() operation
|
2013-06-03 02:33:51 +04:00
|
|
|
*
|
|
|
|
* @return new IRP or NULL on error
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2020-11-30 03:36:20 +03:00
|
|
|
IRP *devredir_irp_with_pathname_new(const char *pathname)
|
2013-06-03 02:33:51 +04:00
|
|
|
{
|
2019-11-14 17:43:57 +03:00
|
|
|
unsigned int len = g_strlen(pathname);
|
2020-11-30 03:36:20 +03:00
|
|
|
IRP *irp = devredir_irp_with_pathnamelen_new(len);
|
2019-11-14 17:43:57 +03:00
|
|
|
if (irp != NULL)
|
|
|
|
{
|
|
|
|
g_strcpy(irp->pathname, pathname);
|
|
|
|
}
|
2013-06-03 02:33:51 +04:00
|
|
|
|
2019-11-14 17:43:57 +03:00
|
|
|
return irp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new IRP with space allocated for a pathname, and append to
|
|
|
|
* linked list.
|
|
|
|
*
|
|
|
|
* Allocation is made in such a way that the IRP can be freed with a single
|
|
|
|
* free() operation
|
|
|
|
*
|
|
|
|
* @return new IRP or NULL on error
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2020-11-30 03:36:20 +03:00
|
|
|
IRP *devredir_irp_with_pathnamelen_new(unsigned int pathnamelen)
|
2019-11-14 17:43:57 +03:00
|
|
|
{
|
|
|
|
IRP *irp;
|
|
|
|
IRP *irp_last;
|
|
|
|
|
2020-10-08 04:55:08 +03:00
|
|
|
LOG_DEVEL(LOG_LEVEL_DEBUG, "entered");
|
2013-06-03 02:33:51 +04:00
|
|
|
|
2019-11-14 17:43:57 +03:00
|
|
|
/* create new IRP with space on end for the pathname and a terminator */
|
|
|
|
irp = (IRP *)g_malloc(sizeof(IRP) + (pathnamelen + 1), 1);
|
|
|
|
if (irp == NULL)
|
|
|
|
{
|
2020-10-08 04:55:08 +03:00
|
|
|
LOG_DEVEL(LOG_LEVEL_ERROR, "system out of memory!");
|
2019-11-14 17:43:57 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-06-03 02:33:51 +04:00
|
|
|
|
2019-11-14 17:43:57 +03:00
|
|
|
irp->pathname = (char *)irp + sizeof(IRP); /* Initialise pathname pointer */
|
2013-06-03 02:33:51 +04:00
|
|
|
|
2019-11-14 17:43:57 +03:00
|
|
|
/* insert at end of linked list */
|
|
|
|
if ((irp_last = devredir_irp_get_last()) == NULL)
|
|
|
|
{
|
|
|
|
/* list is empty, this is the first entry */
|
|
|
|
g_irp_head = irp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
irp_last->next = irp;
|
|
|
|
irp->prev = irp_last;
|
|
|
|
}
|
2013-06-03 02:33:51 +04:00
|
|
|
|
2020-10-08 04:55:08 +03:00
|
|
|
LOG_DEVEL(LOG_LEVEL_DEBUG, "new IRP=%p", irp);
|
2019-11-14 17:43:57 +03:00
|
|
|
return irp;
|
2013-06-03 02:33:51 +04:00
|
|
|
}
|
|
|
|
|
2013-05-16 04:06:05 +04:00
|
|
|
/**
|
|
|
|
* Delete specified IRP from linked list
|
|
|
|
*
|
|
|
|
* @return 0 on success, -1 on failure
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
int devredir_irp_delete(IRP *irp)
|
|
|
|
{
|
|
|
|
IRP *lirp = g_irp_head;
|
|
|
|
|
|
|
|
if ((irp == NULL) || (lirp == NULL))
|
2020-11-30 03:36:20 +03:00
|
|
|
{
|
2013-05-16 04:06:05 +04:00
|
|
|
return -1;
|
2020-11-30 03:36:20 +03:00
|
|
|
}
|
2013-05-16 04:06:05 +04:00
|
|
|
|
2020-10-08 04:55:08 +03:00
|
|
|
LOG_DEVEL(LOG_LEVEL_DEBUG, "irp=%p completion_id=%d type=%d",
|
2013-05-19 06:44:16 +04:00
|
|
|
irp, irp->CompletionId, irp->completion_type);
|
|
|
|
|
2013-05-16 04:06:05 +04:00
|
|
|
devredir_irp_dump(); // LK_TODO
|
|
|
|
|
|
|
|
while (lirp)
|
|
|
|
{
|
|
|
|
if (lirp == irp)
|
2020-11-30 03:36:20 +03:00
|
|
|
{
|
2013-05-16 04:06:05 +04:00
|
|
|
break;
|
2020-11-30 03:36:20 +03:00
|
|
|
}
|
2013-05-16 04:06:05 +04:00
|
|
|
|
|
|
|
lirp = lirp->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lirp == NULL)
|
2020-11-30 03:36:20 +03:00
|
|
|
{
|
|
|
|
return -1; /* did not find specified irp */
|
|
|
|
}
|
2013-05-16 04:06:05 +04:00
|
|
|
|
|
|
|
if (lirp->prev == NULL)
|
|
|
|
{
|
|
|
|
/* we are at head of linked list */
|
|
|
|
if (lirp->next == NULL)
|
|
|
|
{
|
|
|
|
/* only one element in list */
|
|
|
|
g_free(lirp);
|
|
|
|
g_irp_head = NULL;
|
|
|
|
devredir_irp_dump(); // LK_TODO
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
lirp->next->prev = NULL;
|
|
|
|
g_irp_head = lirp->next;
|
|
|
|
g_free(lirp);
|
|
|
|
}
|
|
|
|
else if (lirp->next == NULL)
|
|
|
|
{
|
|
|
|
/* we are at tail of linked list */
|
|
|
|
lirp->prev->next = NULL;
|
|
|
|
g_free(lirp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* we are in between */
|
|
|
|
lirp->prev->next = lirp->next;
|
|
|
|
lirp->next->prev = lirp->prev;
|
|
|
|
g_free(lirp);
|
|
|
|
}
|
|
|
|
|
|
|
|
devredir_irp_dump(); // LK_TODO
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return IRP containing specified completion_id
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
IRP *devredir_irp_find(tui32 completion_id)
|
|
|
|
{
|
|
|
|
IRP *irp = g_irp_head;
|
|
|
|
|
|
|
|
while (irp)
|
|
|
|
{
|
|
|
|
if (irp->CompletionId == completion_id)
|
2013-05-19 06:44:16 +04:00
|
|
|
{
|
2020-10-08 04:55:08 +03:00
|
|
|
LOG_DEVEL(LOG_LEVEL_DEBUG, "returning irp=%p", irp);
|
2013-05-16 04:06:05 +04:00
|
|
|
return irp;
|
2013-05-19 06:44:16 +04:00
|
|
|
}
|
2013-05-16 04:06:05 +04:00
|
|
|
|
|
|
|
irp = irp->next;
|
|
|
|
}
|
|
|
|
|
2020-10-08 04:55:08 +03:00
|
|
|
LOG_DEVEL(LOG_LEVEL_DEBUG, "returning irp=NULL");
|
2013-05-16 04:06:05 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-11-30 03:36:20 +03:00
|
|
|
IRP *devredir_irp_find_by_fileid(tui32 FileId)
|
2013-05-16 04:06:05 +04:00
|
|
|
{
|
|
|
|
IRP *irp = g_irp_head;
|
|
|
|
|
|
|
|
while (irp)
|
|
|
|
{
|
|
|
|
if (irp->FileId == FileId)
|
2013-05-19 06:44:16 +04:00
|
|
|
{
|
2020-10-08 04:55:08 +03:00
|
|
|
LOG_DEVEL(LOG_LEVEL_DEBUG, "returning irp=%p", irp);
|
2013-05-16 04:06:05 +04:00
|
|
|
return irp;
|
2013-05-19 06:44:16 +04:00
|
|
|
}
|
2013-05-16 04:06:05 +04:00
|
|
|
|
|
|
|
irp = irp->next;
|
|
|
|
}
|
|
|
|
|
2020-10-08 04:55:08 +03:00
|
|
|
LOG_DEVEL(LOG_LEVEL_DEBUG, "returning irp=NULL");
|
2013-05-16 04:06:05 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return last IRP in linked list
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2020-11-30 03:36:20 +03:00
|
|
|
IRP *devredir_irp_get_last(void)
|
2013-05-16 04:06:05 +04:00
|
|
|
{
|
|
|
|
IRP *irp = g_irp_head;
|
|
|
|
|
|
|
|
while (irp)
|
|
|
|
{
|
|
|
|
if (irp->next == NULL)
|
2020-11-30 03:36:20 +03:00
|
|
|
{
|
2013-05-16 04:06:05 +04:00
|
|
|
break;
|
2020-11-30 03:36:20 +03:00
|
|
|
}
|
2013-05-16 04:06:05 +04:00
|
|
|
|
|
|
|
irp = irp->next;
|
|
|
|
}
|
|
|
|
|
2020-10-08 04:55:08 +03:00
|
|
|
LOG_DEVEL(LOG_LEVEL_DEBUG, "returning irp=%p", irp);
|
2013-05-16 04:06:05 +04:00
|
|
|
return irp;
|
|
|
|
}
|
|
|
|
|
2016-12-23 20:52:22 +03:00
|
|
|
void devredir_irp_dump(void)
|
2013-05-16 04:06:05 +04:00
|
|
|
{
|
|
|
|
IRP *irp = g_irp_head;
|
|
|
|
|
2020-10-08 04:55:08 +03:00
|
|
|
LOG_DEVEL(LOG_LEVEL_DEBUG, "------- dumping IRPs --------");
|
2013-05-16 04:06:05 +04:00
|
|
|
while (irp)
|
|
|
|
{
|
2020-10-08 04:55:08 +03:00
|
|
|
LOG_DEVEL(LOG_LEVEL_DEBUG, " completion_id=%d\tcompletion_type=%d\tFileId=%d",
|
2013-05-16 04:06:05 +04:00
|
|
|
irp->CompletionId, irp->completion_type, irp->FileId);
|
|
|
|
|
|
|
|
irp = irp->next;
|
|
|
|
}
|
2020-10-08 04:55:08 +03:00
|
|
|
LOG_DEVEL(LOG_LEVEL_DEBUG, "------- dumping IRPs done ---");
|
2013-05-16 04:06:05 +04:00
|
|
|
}
|