mirror of
https://github.com/samba-team/samba.git
synced 2025-05-10 02:50:25 +03:00
pdus, and then feeds them over either a "local" function call or a "remote" function call to an msrpc service. the "remote" msrpc daemon, on the other side of a unix socket, then calls the same "local" function that smbd would, if the msrpc service were being run from inside smbd. this allows a transition from local msrpc services (inside the same smbd process) to remote (over a unix socket). removed reference to pipes_struct in msrpc services. all msrpc processing functions take rpcsrv_struct which is a structure containing state info for the msrpc functions to decode and create pdus. created become_vuser() which does everything not related to connection_struct that become_user() does. removed, as best i could, connection_struct dependencies from the nt spoolss printing code. todo: remove dcinfo from rpcsrv_struct because this stores NETLOGON-specific info on a per-connection basis, and if the connection dies then so does the info, and that's a fairly serious problem. had to put pretty much everything that is in user_struct into parse_creds.c to feed unix user info over to the msrpc daemons. why? because it's expensive to do unix password/group database lookups, and it's definitely expensive to do nt user profile lookups, not to mention pretty difficult and if you did either of these it would introduce a complication / unnecessary interdependency. so, send uid/gid/num_groups/gid_t* + SID+num_rids+domain_group_rids* + unix username + nt username + nt domain + user session key etc. this is the MINIMUM info identified so far that's actually implemented. missing bits include the called and calling netbios names etc. (basically, anything that can be loaded into standard_sub() and standard_sub_basic()...) (This used to be commit aa3c659a8dba0437c17c60055a6ed30fdfecdb6d)
368 lines
9.7 KiB
C
368 lines
9.7 KiB
C
/*
|
|
Unix SMB/Netbios implementation.
|
|
Version 1.9.
|
|
Samba memory buffer functions
|
|
Copyright (C) Andrew Tridgell 1992-1997
|
|
Copyright (C) Luke Kenneth Casson Leighton 1996-1997
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
*/
|
|
|
|
/*******************************************************************
|
|
*
|
|
* Description: memory buffer / stream management.
|
|
* Author : Luke K C Leighton
|
|
* Created : Dec 1997
|
|
*
|
|
|
|
* this module is intended for use in streaming data in and out of
|
|
* buffers. it is intended that a single data stream be subdivided
|
|
* into manageable sections.
|
|
|
|
* for example, an rpc header contains a length field, but until the
|
|
* data has been created, the length is unknown. using this module,
|
|
* the header section can be tacked onto the front of the data memory
|
|
* list once the size of the data section preceding it is known.
|
|
|
|
* the "margin" can be used to over-run and retrospectively lengthen
|
|
* the buffer. this is to save time in some of the loops, where it is
|
|
* not particularly desirable to realloc data by 1, 2 or 4 bytes
|
|
* repetitively...
|
|
|
|
* each memory buffer contains a start and end offset. the end of
|
|
* one buffer should equal to the start of the next in the chain.
|
|
* (end - start = len, instead of end - start + 1 = len)
|
|
|
|
* the debug log levels are very high in some of the routines: you
|
|
* have no idea how boring it gets staring at debug output from these
|
|
|
|
********************************************************************/
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
extern int DEBUGLEVEL;
|
|
|
|
/*******************************************************************
|
|
initialise a memory buffer.
|
|
********************************************************************/
|
|
void mem_init(struct mem_buf *buf, int margin)
|
|
{
|
|
buf->dynamic = True;
|
|
buf->data = NULL;
|
|
buf->data_size = 0;
|
|
buf->data_used = 0;
|
|
|
|
buf->margin = margin;
|
|
|
|
buf->next = NULL;
|
|
|
|
buf->offset.start = 0;
|
|
buf->offset.end = 0x0;
|
|
}
|
|
|
|
/*******************************************************************
|
|
initialise a memory buffer.
|
|
|
|
dynamic indicates memory has been dynamically allocated.
|
|
if mem_free is called, the memory will be freed.
|
|
********************************************************************/
|
|
void mem_create(struct mem_buf *buf, char *data, int offset, int size, int margin, BOOL dynamic)
|
|
{
|
|
buf->dynamic = dynamic;
|
|
buf->data = data;
|
|
buf->data_size = size;
|
|
buf->data_used = size;
|
|
|
|
buf->margin = margin;
|
|
|
|
buf->next = NULL;
|
|
|
|
buf->offset.start = offset;
|
|
buf->offset.end = offset + size;
|
|
}
|
|
|
|
/*******************************************************************
|
|
allocate a memory buffer. assume it's empty
|
|
********************************************************************/
|
|
BOOL mem_alloc_data(struct mem_buf *buf, int size)
|
|
{
|
|
if (!buf->dynamic)
|
|
{
|
|
DEBUG(3,("mem_alloc_data: warning - memory buffer type is set to static\n"));
|
|
}
|
|
|
|
buf->data_size = size + buf->margin;
|
|
buf->data_used = size;
|
|
|
|
buf->data = (char*)malloc(buf->data_size);
|
|
|
|
if (buf->data == NULL && size != 0)
|
|
{
|
|
DEBUG(3,("mem_alloc: could not malloc size %d\n",
|
|
buf->data_size));
|
|
mem_init(buf, buf->margin);
|
|
|
|
return False;
|
|
}
|
|
|
|
bzero(buf->data, buf->data_size);
|
|
buf->offset.end = buf->offset.start + size;
|
|
|
|
return True;
|
|
}
|
|
|
|
/*******************************************************************
|
|
allocates a memory buffer structure
|
|
********************************************************************/
|
|
BOOL mem_buf_copy(char *copy_into, struct mem_buf *buf,
|
|
uint32 offset, uint32 len)
|
|
{
|
|
uint32 end = offset + len;
|
|
char *q = NULL;
|
|
uint32 data_len = mem_buf_len(buf);
|
|
uint32 start_offset = offset;
|
|
struct mem_buf **bcp = &buf;
|
|
|
|
if (buf == NULL || copy_into == NULL) return False;
|
|
|
|
DEBUG(200,("mem_buf_copy: data[%d..%d] offset %d len %d\n",
|
|
buf->offset.start, data_len, offset, len));
|
|
|
|
/* there's probably an off-by-one bug, here, and i haven't even tested the code :-) */
|
|
while (offset < end && ((q = mem_data(bcp, offset)) != NULL))
|
|
{
|
|
uint32 copy_len = (*bcp)->offset.end - offset;
|
|
|
|
DEBUG(200,("\tdata[%d..%d] - offset %d len %d\n",
|
|
(*bcp)->offset.start, (*bcp)->offset.end,
|
|
offset, copy_len));
|
|
|
|
memcpy(copy_into, q, copy_len);
|
|
|
|
offset += copy_len;
|
|
copy_into += copy_len;
|
|
}
|
|
|
|
if ((*bcp) != NULL)
|
|
{
|
|
DEBUG(200,("mem_buf_copy: copied %d bytes\n", offset - start_offset));
|
|
}
|
|
else
|
|
{
|
|
DEBUG(200,("mem_buf_copy: failed\n"));
|
|
}
|
|
|
|
return buf != NULL;
|
|
}
|
|
|
|
/*******************************************************************
|
|
allocates a memory buffer structure
|
|
********************************************************************/
|
|
BOOL mem_buf_init(struct mem_buf **buf, uint32 margin)
|
|
{
|
|
if (buf == NULL) return False;
|
|
|
|
if ((*buf) == NULL)
|
|
{
|
|
(*buf) = (struct mem_buf*)malloc(sizeof(**buf));
|
|
if ((*buf) != NULL)
|
|
{
|
|
mem_init((*buf), margin);
|
|
return True;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
(*buf)->margin = margin;
|
|
return True;
|
|
}
|
|
return False;
|
|
}
|
|
|
|
/*******************************************************************
|
|
frees up a memory buffer.
|
|
********************************************************************/
|
|
void mem_buf_free(struct mem_buf **buf)
|
|
{
|
|
if (buf == NULL) return;
|
|
if ((*buf) == NULL) return;
|
|
|
|
mem_free_data(*buf); /* delete memory data */
|
|
free(*buf); /* delete item */
|
|
(*buf) = NULL;
|
|
}
|
|
|
|
/*******************************************************************
|
|
frees a memory buffer chain. assumes that all items are malloced.
|
|
********************************************************************/
|
|
static void mem_free_chain(struct mem_buf **buf)
|
|
{
|
|
if (buf == NULL) return;
|
|
if ((*buf) == NULL) return;
|
|
|
|
if ((*buf)->next != NULL)
|
|
{
|
|
mem_free_chain(&((*buf)->next)); /* delete all other items in chain */
|
|
}
|
|
mem_buf_free(buf);
|
|
}
|
|
|
|
/*******************************************************************
|
|
frees a memory buffer.
|
|
********************************************************************/
|
|
void mem_free_data(struct mem_buf *buf)
|
|
{
|
|
if (buf == NULL) return;
|
|
|
|
if (buf->data != NULL && buf->dynamic)
|
|
{
|
|
free(buf->data); /* delete data in this structure */
|
|
buf->data = NULL;
|
|
}
|
|
mem_init(buf, buf->margin);
|
|
}
|
|
|
|
/*******************************************************************
|
|
reallocate a memory buffer, including a safety margin
|
|
********************************************************************/
|
|
BOOL mem_realloc_data(struct mem_buf *buf, size_t new_size)
|
|
{
|
|
char *new_data;
|
|
|
|
if (!buf->dynamic)
|
|
{
|
|
DEBUG(3,("mem_realloc_data: memory buffer has not been dynamically allocated!\n"));
|
|
return False;
|
|
}
|
|
|
|
if (new_size == 0)
|
|
{
|
|
mem_free_data(buf);
|
|
return True;
|
|
}
|
|
|
|
new_data = (char*)Realloc(buf->data, new_size + buf->margin);
|
|
|
|
if (new_data != NULL)
|
|
{
|
|
buf->data = new_data;
|
|
buf->data_size = new_size + buf->margin;
|
|
buf->data_used = new_size;
|
|
}
|
|
else if (buf->data_size <= new_size)
|
|
{
|
|
DEBUG(3,("mem_realloc: warning - could not realloc to %d(+%d)\n",
|
|
new_size, buf->margin));
|
|
|
|
buf->data_used = new_size;
|
|
}
|
|
else
|
|
{
|
|
DEBUG(3,("mem_realloc: error - could not realloc to %d\n",
|
|
new_size));
|
|
|
|
mem_free_data(buf);
|
|
return False;
|
|
}
|
|
|
|
buf->offset.end = buf->offset.start + new_size;
|
|
|
|
DEBUG(150,("mem_realloc_data: size: %d start: %d end: %d\n",
|
|
new_size, buf->offset.start, buf->offset.end));
|
|
return True;
|
|
}
|
|
|
|
/*******************************************************************
|
|
reallocate a memory buffer, retrospectively :-)
|
|
********************************************************************/
|
|
BOOL mem_grow_data(struct mem_buf **buf, BOOL io, int new_size, BOOL force_grow)
|
|
{
|
|
if (new_size + (*buf)->margin >= (*buf)->data_size)
|
|
{
|
|
if (!io || force_grow)
|
|
{
|
|
/* writing or forge realloc */
|
|
return mem_realloc_data((*buf), new_size);
|
|
}
|
|
else
|
|
{
|
|
}
|
|
}
|
|
return True;
|
|
}
|
|
|
|
/*******************************************************************
|
|
search for a memory buffer that falls within the specified offset
|
|
********************************************************************/
|
|
static BOOL mem_find(struct mem_buf **buf, uint32 offset)
|
|
{
|
|
struct mem_buf *f;
|
|
if (buf == NULL) return False;
|
|
|
|
f = *buf;
|
|
|
|
DEBUG(200,("mem_find: data[%d..%d] offset: %d\n",
|
|
f->offset.start, f->offset.end, offset));
|
|
|
|
while (f != NULL && offset >= f->offset.end)
|
|
{
|
|
DEBUG(200,("mem_find: next[%d..%d]\n",
|
|
f->offset.start, f->offset.end));
|
|
|
|
f = f->next;
|
|
}
|
|
|
|
(*buf) = f;
|
|
|
|
if (f != NULL)
|
|
{
|
|
DEBUG(200,("mem_find: found data[%d..%d]\n",
|
|
(*buf)->offset.start,(*buf)->offset.end));
|
|
}
|
|
|
|
return f != NULL;
|
|
}
|
|
|
|
|
|
/*******************************************************************
|
|
add up the lengths of all sections.
|
|
********************************************************************/
|
|
uint32 mem_buf_len(struct mem_buf *buf)
|
|
{
|
|
int len = 0;
|
|
while (buf != NULL)
|
|
{
|
|
len += buf->offset.end - buf->offset.start;
|
|
buf = buf->next;
|
|
}
|
|
return len;
|
|
}
|
|
|
|
|
|
/*******************************************************************
|
|
return the memory location specified by offset. may return NULL.
|
|
********************************************************************/
|
|
char *mem_data(struct mem_buf **buf, uint32 offset)
|
|
{
|
|
if (mem_find(buf, offset))
|
|
{
|
|
return &((*buf)->data[offset - (*buf)->offset.start]);
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
|