summaryrefslogtreecommitdiff
path: root/source4/lib
diff options
context:
space:
mode:
Diffstat (limited to 'source4/lib')
-rw-r--r--source4/lib/basic.mk42
-rw-r--r--source4/lib/gencache/gencache.c (renamed from source4/lib/gencache.c)0
-rw-r--r--source4/lib/util/README296
-rw-r--r--source4/lib/util/byteorder.h224
-rw-r--r--source4/lib/util/capability.c (renamed from source4/lib/capability.c)0
-rw-r--r--source4/lib/util/config.mk34
-rw-r--r--source4/lib/util/data_blob.c (renamed from source4/lib/data_blob.c)0
-rw-r--r--source4/lib/util/debug.c (renamed from source4/lib/debug.c)0
-rw-r--r--source4/lib/util/debug.h (renamed from source4/lib/debug.h)0
-rw-r--r--source4/lib/util/dprintf.c (renamed from source4/lib/dprintf.c)0
-rw-r--r--source4/lib/util/fault.c (renamed from source4/lib/fault.c)0
-rw-r--r--source4/lib/util/fsusage.c (renamed from source4/lib/fsusage.c)0
-rw-r--r--source4/lib/util/genrand.c (renamed from source4/lib/genrand.c)0
-rw-r--r--source4/lib/util/idtree.c (renamed from source4/lib/idtree.c)0
-rw-r--r--source4/lib/util/module.c (renamed from source4/lib/module.c)0
-rw-r--r--source4/lib/util/ms_fnmatch.c (renamed from source4/lib/ms_fnmatch.c)0
-rw-r--r--source4/lib/util/mutex.c (renamed from source4/lib/mutex.c)0
-rw-r--r--source4/lib/util/mutex.h (renamed from source4/lib/mutex.h)2
-rw-r--r--source4/lib/util/pidfile.c (renamed from source4/lib/pidfile.c)0
-rw-r--r--source4/lib/util/safe_string.h55
-rw-r--r--source4/lib/util/select.c (renamed from source4/lib/select.c)0
-rw-r--r--source4/lib/util/signal.c (renamed from source4/lib/signal.c)0
-rw-r--r--source4/lib/util/substitute.c (renamed from source4/lib/substitute.c)0
-rw-r--r--source4/lib/util/system.c (renamed from source4/lib/system.c)0
-rw-r--r--source4/lib/util/time.c (renamed from source4/lib/time.c)0
-rw-r--r--source4/lib/util/unix_privs.c (renamed from source4/lib/unix_privs.c)0
-rw-r--r--source4/lib/util/util.c (renamed from source4/lib/util.c)0
-rw-r--r--source4/lib/util/util.h81
-rw-r--r--source4/lib/util/util_file.c (renamed from source4/lib/util_file.c)0
-rw-r--r--source4/lib/util/util_getent.c (renamed from source4/lib/util_getent.c)0
-rw-r--r--source4/lib/util/util_pw.c (renamed from source4/lib/util_pw.c)0
-rw-r--r--source4/lib/util/util_sock.c (renamed from source4/lib/util_sock.c)0
-rw-r--r--source4/lib/util/util_str.c (renamed from source4/lib/util_str.c)0
-rw-r--r--source4/lib/util/util_strlist.c (renamed from source4/lib/util_strlist.c)0
-rw-r--r--source4/lib/util/util_unistr.c (renamed from source4/lib/util_unistr.c)0
-rw-r--r--source4/lib/util/xfile.c (renamed from source4/lib/xfile.c)0
-rw-r--r--source4/lib/util/xfile.h (renamed from source4/lib/xfile.h)0
-rw-r--r--source4/lib/version.c63
38 files changed, 695 insertions, 102 deletions
diff --git a/source4/lib/basic.mk b/source4/lib/basic.mk
index d5bff95f01..51ca3378a2 100644
--- a/source4/lib/basic.mk
+++ b/source4/lib/basic.mk
@@ -16,6 +16,7 @@ include socket_wrapper/config.mk
include appweb/config.mk
include replace/config.mk
include stream/config.mk
+include util/config.mk
##############################
# Start SUBSYSTEM LIBNETIF
@@ -57,50 +58,13 @@ OBJ_FILES = \
################################################
[SUBSYSTEM::GENCACHE]
-PRIVATE_PROTO_HEADER = gencache.h
+PRIVATE_PROTO_HEADER = gencache/gencache.h
OBJ_FILES = \
- gencache.o \
-
-##############################
-# Start SUBSYSTEM LIBBASIC
-[SUBSYSTEM::LIBBASIC]
-PRIVATE_PROTO_HEADER = basic.h
-OBJ_FILES = version.o \
- xfile.o \
- debug.o \
- fault.o \
- signal.o \
- system.o \
- time.o \
- genrand.o \
- dprintf.o \
- util_str.o \
- util_strlist.o \
- util_unistr.o \
- util_file.o \
- data_blob.o \
- util.o \
- util_sock.o \
- substitute.o \
- fsusage.o \
- ms_fnmatch.o \
- select.o \
- mutex.o \
- idtree.o \
- module.o
-REQUIRED_SUBSYSTEMS = \
- CHARSET LIBREPLACE LIBNETIF LIBCRYPTO EXT_LIB_DL LIBTALLOC \
- SOCKET_WRAPPER CONFIG
-# End SUBSYSTEM LIBBASIC
-##############################
+ gencache/gencache.o \
[SUBSYSTEM::DB_WRAP]
OBJ_FILES = db_wrap.o \
gendb.o
REQUIRED_SUBSYSTEMS = LIBLDB LIBTDB
-[SUBSYSTEM::PIDFILE]
-OBJ_FILES = pidfile.o
-[SUBSYSTEM::UNIX_PRIVS]
-OBJ_FILES = unix_privs.o
diff --git a/source4/lib/gencache.c b/source4/lib/gencache/gencache.c
index de8c47ada5..de8c47ada5 100644
--- a/source4/lib/gencache.c
+++ b/source4/lib/gencache/gencache.c
diff --git a/source4/lib/util/README b/source4/lib/util/README
new file mode 100644
index 0000000000..e52e04ed92
--- /dev/null
+++ b/source4/lib/util/README
@@ -0,0 +1,296 @@
+This directory contains convenience functions that are used heavily
+throughout Samba. None of these functions are SMB or Samba-specific.
+It's a bit to Samba what GLib is to the GNOME folks.
+
+(This file should be migrated to doxygen sometime.. )
+
+Also TODO: Remove dependency on loadparm
+
+XFILE wrapper functions
+=======================
+The XFILE implementation contains a clone of the standard UNIX stdio
+functions
+
+Debug functions
+===============
+DEBUG()
+
+Crash handlers
+==============
+void smb_panic(const char *why);
+
+Signal handling
+========================
+void BlockSignals(BOOL block,int signum);
+void (*CatchSignal(int signum,void (*handler)(int )))(int);
+void CatchChild(void);
+void CatchChildLeaveStatus(void);
+
+Wrappers
+========
+struct hostent *sys_gethostbyname(const char *name);
+const char *sys_inet_ntoa(struct ipv4_addr in);
+struct ipv4_addr sys_inet_makeaddr(int net, int host);
+void sys_select_signal(void);
+int sys_select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *tval);
+int sys_select_intr(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *tval);
+
+Time datastructures
+===================
+time_t get_time_t_max(void);
+void GetTimeOfDay(struct timeval *tval);
+time_t nt_time_to_unix(NTTIME nt);
+void unix_to_nt_time(NTTIME *nt, time_t t);
+BOOL null_time(time_t t);
+BOOL null_nttime(NTTIME t);
+void push_dos_date(uint8_t *buf, int offset, time_t unixdate, int zone_offset);
+void push_dos_date2(uint8_t *buf,int offset,time_t unixdate, int zone_offset);
+void push_dos_date3(uint8_t *buf,int offset,time_t unixdate, int zone_offset);
+time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset);
+time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset);
+time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset);
+char *http_timestring(TALLOC_CTX *mem_ctx, time_t t);
+char *timestring(TALLOC_CTX *mem_ctx, time_t t);
+const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt);
+void push_nttime(uint8_t *base, uint16_t offset, NTTIME t);
+NTTIME pull_nttime(uint8_t *base, uint16_t offset);
+NTTIME nttime_from_string(const char *s);
+int64_t usec_time_diff(struct timeval *tv1, struct timeval *tv2);
+struct timeval timeval_zero(void);
+BOOL timeval_is_zero(const struct timeval *tv);
+struct timeval timeval_current(void);
+struct timeval timeval_set(uint32_t secs, uint32_t usecs);
+struct timeval timeval_add(const struct timeval *tv,
+ uint32_t secs, uint32_t usecs);
+struct timeval timeval_sum(const struct timeval *tv1,
+ const struct timeval *tv2);
+struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs);
+int timeval_compare(const struct timeval *tv1, const struct timeval *tv2);
+BOOL timeval_expired(const struct timeval *tv);
+double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2);
+double timeval_elapsed(const struct timeval *tv);
+struct timeval timeval_min(const struct timeval *tv1,
+ const struct timeval *tv2);
+struct timeval timeval_max(const struct timeval *tv1,
+ const struct timeval *tv2);
+struct timeval timeval_until(const struct timeval *tv1,
+ const struct timeval *tv2);
+NTTIME timeval_to_nttime(const struct timeval *tv);
+int get_time_zone(time_t t);
+
+Random number generation
+========================
+void set_rand_reseed_callback(void (*fn)(int *));
+void set_need_random_reseed(void);
+void generate_random_buffer(uint8_t *out, int len);
+uint32_t generate_random(void);
+BOOL check_password_quality(const char *s);
+char *generate_random_str_list(TALLOC_CTX *mem_ctx, size_t len, const char *list);
+char *generate_random_str(TALLOC_CTX *mem_ctx, size_t len);
+
+String manipulation
+===================
+BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize);
+int strcasecmp_m(const char *s1, const char *s2);
+BOOL strequal(const char *s1, const char *s2);
+BOOL strcsequal(const char *s1,const char *s2);
+int strwicmp(const char *psz1, const char *psz2);
+void string_replace(char *s, char oldc, char newc);
+BOOL trim_string(char *s,const char *front,const char *back);
+size_t count_chars(const char *s, char c);
+char *safe_strcpy(char *dest,const char *src, size_t maxlength);
+char *safe_strcat(char *dest, const char *src, size_t maxlength);
+char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength);
+char *StrnCpy(char *dest,const char *src,size_t n);
+size_t strhex_to_str(char *p, size_t len, const char *strhex);
+DATA_BLOB strhex_to_data_blob(const char *strhex) ;
+void hex_encode(const unsigned char *buff_in, size_t len, char **out_hex_buffer);
+BOOL in_list(const char *s, const char *list, BOOL casesensitive);
+void string_free(char **s);
+BOOL string_set(char **dest, const char *src);
+void string_sub(char *s,const char *pattern, const char *insert, size_t len);
+void all_string_sub(char *s,const char *pattern,const char *insert, size_t len);
+char *strchr_m(const char *s, char c);
+char *strrchr_m(const char *s, char c);
+BOOL strhaslower(const char *string);
+BOOL strhasupper(const char *string);
+char *strlower_talloc(TALLOC_CTX *ctx, const char *src);
+char *strupper_talloc(TALLOC_CTX *ctx, const char *src);
+void strlower_m(char *s);
+void strupper_m(char *s);
+size_t strlen_m(const char *s);
+size_t strlen_m_term(const char *s);
+void rfc1738_unescape(char *buf);
+DATA_BLOB base64_decode_data_blob(TALLOC_CTX *mem_ctx, const char *s);
+void base64_decode_inplace(char *s);
+char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data);
+size_t valgrind_strlen(const char *s);
+const char *str_format_nbt_domain(TALLOC_CTX *mem_ctx, const char *s);
+BOOL add_string_to_array(TALLOC_CTX *mem_ctx,
+ const char *str, const char ***strings, int *num);
+int strcmp_safe(const char *s1, const char *s2);
+size_t ascii_len_n(const char *src, size_t n);
+char *attrib_string(TALLOC_CTX *mem_ctx, uint32_t attrib);
+BOOL set_boolean(const char *boolean_string, BOOL *boolean);
+BOOL conv_str_bool(const char * str, BOOL * val);
+BOOL conv_str_size(const char * str, uint64_t * val);
+BOOL conv_str_u64(const char * str, uint64_t * val);
+
+String list manipulation
+========================
+const char **str_list_make(TALLOC_CTX *mem_ctx, const char *string, const char *sep);
+const char **str_list_make_shell(TALLOC_CTX *mem_ctx, const char *string, const char *sep);
+char *str_list_join(TALLOC_CTX *mem_ctx, const char **list, char seperator);
+char *str_list_join_shell(TALLOC_CTX *mem_ctx, const char **list, char sep);
+size_t str_list_length(const char **list);
+const char **str_list_copy(TALLOC_CTX *mem_ctx, const char **list);
+BOOL str_list_equal(const char **list1, const char **list2);
+const char **str_list_add(const char **list, const char *s);
+void str_list_remove(const char **list, const char *s);
+BOOL str_list_check(const char **list, const char *s);
+BOOL str_list_check_ci(const char **list, const char *s);
+
+Unicode string manipulation
+===========================
+codepoint_t toupper_w(codepoint_t val);
+codepoint_t tolower_w(codepoint_t val);
+size_t utf16_len(const void *buf);
+size_t utf16_len_n(const void *src, size_t n);
+size_t ucs2_align(const void *base_ptr, const void *p, int flags);
+int codepoint_cmpi(codepoint_t c1, codepoint_t c2);
+
+File manipulation
+=================
+char *fgets_slash(char *s2,int maxlen,XFILE *f);
+char *afdgets(int fd, TALLOC_CTX *mem_ctx, size_t hint);
+char *fd_load(int fd, size_t *size, TALLOC_CTX *mem_ctx);
+char *file_load(const char *fname, size_t *size, TALLOC_CTX *mem_ctx);
+void *map_file(const char *fname, size_t size);
+char **file_lines_load(const char *fname, int *numlines, TALLOC_CTX *mem_ctx);
+char **fd_lines_load(int fd, int *numlines, TALLOC_CTX *mem_ctx);
+void file_lines_slashcont(char **lines);
+BOOL file_save(const char *fname, const void *packet, size_t length);
+BOOL file_exists(const char *path);
+int vfdprintf(int fd, const char *format, va_list ap) _PRINTF_ATTRIBUTE(2,0);
+int fdprintf(int fd, const char *format, ...) _PRINTF_ATTRIBUTE(2,3);
+
+Data Blob
+=========
+DATA_BLOB data_blob_named(const void *p, size_t length, const char *name);
+DATA_BLOB data_blob_talloc_named(TALLOC_CTX *mem_ctx, const void *p, size_t length, const char *name);
+DATA_BLOB data_blob_talloc_reference(TALLOC_CTX *mem_ctx, DATA_BLOB *blob);
+DATA_BLOB data_blob_talloc_zero(TALLOC_CTX *mem_ctx, size_t length);
+void data_blob_free(DATA_BLOB *d);
+void data_blob_clear(DATA_BLOB *d);
+void data_blob_clear_free(DATA_BLOB *d);
+BOOL data_blob_equal(const DATA_BLOB *d1, const DATA_BLOB *d2);
+char *data_blob_hex_string(TALLOC_CTX *mem_ctx, DATA_BLOB *blob);
+DATA_BLOB data_blob_string_const(const char *str);
+DATA_BLOB data_blob_const(const void *p, size_t length);
+NTSTATUS data_blob_realloc(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, size_t length);
+NTSTATUS data_blob_append(TALLOC_CTX *mem_ctx, DATA_BLOB *blob,
+ const void *p, size_t length);
+
+Random Utility functions
+========================
+const char *tmpdir(void);
+BOOL file_exist(const char *fname);
+time_t file_modtime(const char *fname);
+BOOL directory_exist(const char *dname);
+BOOL directory_create_or_exist(const char *dname, uid_t uid,
+ mode_t dir_perms);
+int set_blocking(int fd, BOOL set);
+void msleep(uint_t t);
+void become_daemon(BOOL Fork);
+void safe_free(void *p);
+BOOL is_myname(const char *name);
+char* get_myname(void);
+BOOL is_ipaddress(const char *str);
+uint32_t interpret_addr(const char *str);
+struct ipv4_addr interpret_addr2(const char *str);
+BOOL is_zero_ip(struct ipv4_addr ip);
+BOOL same_net(struct ipv4_addr ip1,struct ipv4_addr ip2,struct ipv4_addr mask);
+BOOL process_exists(pid_t pid);
+BOOL fcntl_lock(int fd, int op, off_t offset, off_t count, int type);
+void dump_data(int level, const uint8_t *buf,int len);
+void *smb_xmalloc(size_t size);
+void *smb_xmemdup(const void *p, size_t size);
+char *smb_xstrdup(const char *s);
+void *memdup(const void *p, size_t size);
+char *lock_path(TALLOC_CTX* mem_ctx, const char *name);
+char *lib_path(TALLOC_CTX* mem_ctx, const char *name);
+char *private_path(TALLOC_CTX* mem_ctx, const char *name);
+char *smbd_tmp_path(TALLOC_CTX *mem_ctx, const char *name);
+init_module_fn *load_samba_modules(TALLOC_CTX *mem_ctx, const char *subsystem);
+void dump_data_pw(const char *msg, const uint8_t * data, size_t len);
+BOOL all_zero(const uint8_t *ptr, uint_t size);
+void *realloc_array(void *ptr, size_t el_size, unsigned count);
+
+/* The following definitions come from lib/util/util_sock.c */
+
+void set_socket_options(int fd, const char *options);
+
+Variable substitution
+=====================
+void sub_set_context(struct substitute_context *subptr);
+void sub_set_remote_proto(const char *str);
+void sub_set_remote_arch(const char *str);
+void sub_set_user_name(const char *name);
+void standard_sub_basic(char *str,size_t len);
+char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name, const char *str);
+char *alloc_sub_basic(const char *smb_name, const char *str);
+char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
+ const char *input_string,
+ const char *username,
+ const char *domain,
+ uid_t uid,
+ gid_t gid);
+char *alloc_sub_specified(const char *input_string,
+ const char *username,
+ const char *domain,
+ uid_t uid,
+ gid_t gid);
+char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
+ int snum,
+ const char *user,
+ const char *connectpath,
+ gid_t gid,
+ const char *smb_name,
+ char *str);
+char *alloc_sub_advanced(int snum, const char *user,
+ const char *connectpath, gid_t gid,
+ const char *smb_name, char *str);
+void standard_sub_tcon(struct smbsrv_tcon *tcon, char *str, size_t len);
+char *talloc_sub_tcon(TALLOC_CTX *mem_ctx, struct smbsrv_tcon *tcon, char *str);
+char *alloc_sub_tcon(struct smbsrv_tcon *tcon, char *str);
+void standard_sub_snum(int snum, char *str, size_t len);
+
+/* The following definitions come from lib/util/fsusage.c */
+
+int sys_fsusage(const char *path, uint64_t *dfree, uint64_t *dsize);
+
+Microsoft-style filename matching
+=================================
+int ms_fnmatch(const char *pattern, const char *string, enum protocol_types protocol);
+int gen_fnmatch(const char *pattern, const char *string);
+
+Mutexes
+=======
+BOOL register_mutex_handlers(const char *name, struct mutex_ops *ops);
+
+Idtree
+======
+very efficient functions to manage mapping a id (such as a fnum) to
+a pointer. This is used for fnum and search id allocation.
+
+struct idr_context *idr_init(TALLOC_CTX *mem_ctx);
+int idr_get_new(struct idr_context *idp, void *ptr, int limit);
+int idr_get_new_above(struct idr_context *idp, void *ptr, int starting_id, int limit);
+int idr_get_new_random(struct idr_context *idp, void *ptr, int limit);
+void *idr_find(struct idr_context *idp, int id);
+int idr_remove(struct idr_context *idp, int id);
+
+Module loading
+==============
+init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path);
+BOOL run_init_functions(NTSTATUS (**fns) (void));
diff --git a/source4/lib/util/byteorder.h b/source4/lib/util/byteorder.h
new file mode 100644
index 0000000000..941dee9e88
--- /dev/null
+++ b/source4/lib/util/byteorder.h
@@ -0,0 +1,224 @@
+/*
+ Unix SMB/CIFS implementation.
+ SMB Byte handling
+ Copyright (C) Andrew Tridgell 1992-1998
+
+ 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.
+*/
+
+#ifndef _BYTEORDER_H
+#define _BYTEORDER_H
+
+/*
+ This file implements macros for machine independent short and
+ int manipulation
+
+Here is a description of this file that I emailed to the samba list once:
+
+> I am confused about the way that byteorder.h works in Samba. I have
+> looked at it, and I would have thought that you might make a distinction
+> between LE and BE machines, but you only seem to distinguish between 386
+> and all other architectures.
+>
+> Can you give me a clue?
+
+sure.
+
+The distinction between 386 and other architectures is only there as
+an optimisation. You can take it out completely and it will make no
+difference. The routines (macros) in byteorder.h are totally byteorder
+independent. The 386 optimsation just takes advantage of the fact that
+the x86 processors don't care about alignment, so we don't have to
+align ints on int boundaries etc. If there are other processors out
+there that aren't alignment sensitive then you could also define
+CAREFUL_ALIGNMENT=0 on those processors as well.
+
+Ok, now to the macros themselves. I'll take a simple example, say we
+want to extract a 2 byte integer from a SMB packet and put it into a
+type called uint16_t that is in the local machines byte order, and you
+want to do it with only the assumption that uint16_t is _at_least_ 16
+bits long (this last condition is very important for architectures
+that don't have any int types that are 2 bytes long)
+
+You do this:
+
+#define CVAL(buf,pos) (((uint8_t *)(buf))[pos])
+#define PVAL(buf,pos) ((uint_t)CVAL(buf,pos))
+#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
+
+then to extract a uint16_t value at offset 25 in a buffer you do this:
+
+char *buffer = foo_bar();
+uint16_t xx = SVAL(buffer,25);
+
+We are using the byteoder independence of the ANSI C bitshifts to do
+the work. A good optimising compiler should turn this into efficient
+code, especially if it happens to have the right byteorder :-)
+
+I know these macros can be made a bit tidier by removing some of the
+casts, but you need to look at byteorder.h as a whole to see the
+reasoning behind them. byteorder.h defines the following macros:
+
+SVAL(buf,pos) - extract a 2 byte SMB value
+IVAL(buf,pos) - extract a 4 byte SMB value
+SVALS(buf,pos) signed version of SVAL()
+IVALS(buf,pos) signed version of IVAL()
+
+SSVAL(buf,pos,val) - put a 2 byte SMB value into a buffer
+SIVAL(buf,pos,val) - put a 4 byte SMB value into a buffer
+SSVALS(buf,pos,val) - signed version of SSVAL()
+SIVALS(buf,pos,val) - signed version of SIVAL()
+
+RSVAL(buf,pos) - like SVAL() but for NMB byte ordering
+RSVALS(buf,pos) - like SVALS() but for NMB byte ordering
+RIVAL(buf,pos) - like IVAL() but for NMB byte ordering
+RIVALS(buf,pos) - like IVALS() but for NMB byte ordering
+RSSVAL(buf,pos,val) - like SSVAL() but for NMB ordering
+RSIVAL(buf,pos,val) - like SIVAL() but for NMB ordering
+RSIVALS(buf,pos,val) - like SIVALS() but for NMB ordering
+
+it also defines lots of intermediate macros, just ignore those :-)
+
+*/
+
+
+/*
+ on powerpc we can use the magic instructions to load/store
+ in little endian
+*/
+#if (defined(__powerpc__) && defined(__GNUC__))
+static __inline__ uint16_t ld_le16(const uint16_t *addr)
+{
+ uint16_t val;
+ __asm__ ("lhbrx %0,0,%1" : "=r" (val) : "r" (addr), "m" (*addr));
+ return val;
+}
+
+static __inline__ void st_le16(uint16_t *addr, const uint16_t val)
+{
+ __asm__ ("sthbrx %1,0,%2" : "=m" (*addr) : "r" (val), "r" (addr));
+}
+
+static __inline__ uint32_t ld_le32(const uint32_t *addr)
+{
+ uint32_t val;
+ __asm__ ("lwbrx %0,0,%1" : "=r" (val) : "r" (addr), "m" (*addr));
+ return val;
+}
+
+static __inline__ void st_le32(uint32_t *addr, const uint32_t val)
+{
+ __asm__ ("stwbrx %1,0,%2" : "=m" (*addr) : "r" (val), "r" (addr));
+}
+#define HAVE_ASM_BYTEORDER 1
+#endif
+
+
+
+#undef CAREFUL_ALIGNMENT
+
+/* we know that the 386 can handle misalignment and has the "right"
+ byteorder */
+#if defined(__i386__)
+#define CAREFUL_ALIGNMENT 0
+#endif
+
+#ifndef CAREFUL_ALIGNMENT
+#define CAREFUL_ALIGNMENT 1
+#endif
+
+#define CVAL(buf,pos) ((uint_t)(((const uint8_t *)(buf))[pos]))
+#define CVAL_NC(buf,pos) (((uint8_t *)(buf))[pos]) /* Non-const version of CVAL */
+#define PVAL(buf,pos) (CVAL(buf,pos))
+#define SCVAL(buf,pos,val) (CVAL_NC(buf,pos) = (val))
+
+#if HAVE_ASM_BYTEORDER
+
+#define _PTRPOS(buf,pos) (((const uint8_t *)buf)+(pos))
+#define SVAL(buf,pos) ld_le16((const uint16_t *)_PTRPOS(buf,pos))
+#define IVAL(buf,pos) ld_le32((const uint32_t *)_PTRPOS(buf,pos))
+#define SSVAL(buf,pos,val) st_le16((uint16_t *)_PTRPOS(buf,pos), val)
+#define SIVAL(buf,pos,val) st_le32((uint32_t *)_PTRPOS(buf,pos), val)
+#define SVALS(buf,pos) ((int16_t)SVAL(buf,pos))
+#define IVALS(buf,pos) ((int32_t)IVAL(buf,pos))
+#define SSVALS(buf,pos,val) SSVAL((buf),(pos),((int16_t)(val)))
+#define SIVALS(buf,pos,val) SIVAL((buf),(pos),((int32_t)(val)))
+
+#elif CAREFUL_ALIGNMENT
+
+#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
+#define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
+#define SSVALX(buf,pos,val) (CVAL_NC(buf,pos)=(uint8_t)((val)&0xFF),CVAL_NC(buf,pos+1)=(uint8_t)((val)>>8))
+#define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
+#define SVALS(buf,pos) ((int16_t)SVAL(buf,pos))
+#define IVALS(buf,pos) ((int32_t)IVAL(buf,pos))
+#define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16_t)(val)))
+#define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32_t)(val)))
+#define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16_t)(val)))
+#define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32_t)(val)))
+
+#else /* CAREFUL_ALIGNMENT */
+
+/* this handles things for architectures like the 386 that can handle
+ alignment errors */
+/*
+ WARNING: This section is dependent on the length of int16_t and int32_t
+ being correct
+*/
+
+/* get single value from an SMB buffer */
+#define SVAL(buf,pos) (*(const uint16_t *)((const char *)(buf) + (pos)))
+#define SVAL_NC(buf,pos) (*(uint16_t *)((char *)(buf) + (pos))) /* Non const version of above. */
+#define IVAL(buf,pos) (*(const uint32_t *)((const char *)(buf) + (pos)))
+#define IVAL_NC(buf,pos) (*(uint32_t *)((char *)(buf) + (pos))) /* Non const version of above. */
+#define SVALS(buf,pos) (*(const int16_t *)((const char *)(buf) + (pos)))
+#define SVALS_NC(buf,pos) (*(int16_t *)((char *)(buf) + (pos))) /* Non const version of above. */
+#define IVALS(buf,pos) (*(const int32_t *)((const char *)(buf) + (pos)))
+#define IVALS_NC(buf,pos) (*(int32_t *)((char *)(buf) + (pos))) /* Non const version of above. */
+
+/* store single value in an SMB buffer */
+#define SSVAL(buf,pos,val) SVAL_NC(buf,pos)=((uint16_t)(val))
+#define SIVAL(buf,pos,val) IVAL_NC(buf,pos)=((uint32_t)(val))
+#define SSVALS(buf,pos,val) SVALS_NC(buf,pos)=((int16_t)(val))
+#define SIVALS(buf,pos,val) IVALS_NC(buf,pos)=((int32_t)(val))
+
+#endif /* CAREFUL_ALIGNMENT */
+
+/* now the reverse routines - these are used in nmb packets (mostly) */
+#define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
+#define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
+
+#define RSVAL(buf,pos) SREV(SVAL(buf,pos))
+#define RSVALS(buf,pos) SREV(SVALS(buf,pos))
+#define RIVAL(buf,pos) IREV(IVAL(buf,pos))
+#define RIVALS(buf,pos) IREV(IVALS(buf,pos))
+#define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
+#define RSSVALS(buf,pos,val) SSVALS(buf,pos,SREV(val))
+#define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
+#define RSIVALS(buf,pos,val) SIVALS(buf,pos,IREV(val))
+
+/* Alignment macros. */
+#define ALIGN4(p,base) ((p) + ((4 - (PTR_DIFF((p), (base)) & 3)) & 3))
+#define ALIGN2(p,base) ((p) + ((2 - (PTR_DIFF((p), (base)) & 1)) & 1))
+
+
+/* macros for accessing SMB protocol elements */
+#define VWV(vwv) ((vwv)*2)
+
+/* 64 bit macros */
+#define SBVAL(p, ofs, v) (SIVAL(p,ofs,(v)&0xFFFFFFFF), SIVAL(p,(ofs)+4,((uint64_t)(v))>>32))
+#define BVAL(p, ofs) (IVAL(p,ofs) | (((uint64_t)IVAL(p,(ofs)+4)) << 32))
+
+#endif /* _BYTEORDER_H */
diff --git a/source4/lib/capability.c b/source4/lib/util/capability.c
index 0cebd33303..0cebd33303 100644
--- a/source4/lib/capability.c
+++ b/source4/lib/util/capability.c
diff --git a/source4/lib/util/config.mk b/source4/lib/util/config.mk
new file mode 100644
index 0000000000..79e145775d
--- /dev/null
+++ b/source4/lib/util/config.mk
@@ -0,0 +1,34 @@
+[SUBSYSTEM::LIBBASIC]
+PRIVATE_PROTO_HEADER = util_proto.h
+PUBLIC_HEADERS = util.h
+OBJ_FILES = xfile.o \
+ debug.o \
+ fault.o \
+ signal.o \
+ system.o \
+ time.o \
+ genrand.o \
+ dprintf.o \
+ util_str.o \
+ util_strlist.o \
+ util_unistr.o \
+ util_file.o \
+ data_blob.o \
+ util.o \
+ util_sock.o \
+ substitute.o \
+ fsusage.o \
+ ms_fnmatch.o \
+ select.o \
+ mutex.o \
+ idtree.o \
+ module.o
+REQUIRED_SUBSYSTEMS = \
+ CHARSET LIBREPLACE LIBCRYPTO EXT_LIB_DL LIBTALLOC \
+ SOCKET_WRAPPER CONFIG
+
+[SUBSYSTEM::PIDFILE]
+OBJ_FILES = pidfile.o
+
+[SUBSYSTEM::UNIX_PRIVS]
+OBJ_FILES = unix_privs.o
diff --git a/source4/lib/data_blob.c b/source4/lib/util/data_blob.c
index c6471fbf54..c6471fbf54 100644
--- a/source4/lib/data_blob.c
+++ b/source4/lib/util/data_blob.c
diff --git a/source4/lib/debug.c b/source4/lib/util/debug.c
index 9df6e573b0..9df6e573b0 100644
--- a/source4/lib/debug.c
+++ b/source4/lib/util/debug.c
diff --git a/source4/lib/debug.h b/source4/lib/util/debug.h
index 8ff937e7b9..8ff937e7b9 100644
--- a/source4/lib/debug.h
+++ b/source4/lib/util/debug.h
diff --git a/source4/lib/dprintf.c b/source4/lib/util/dprintf.c
index 64227148fd..64227148fd 100644
--- a/source4/lib/dprintf.c
+++ b/source4/lib/util/dprintf.c
diff --git a/source4/lib/fault.c b/source4/lib/util/fault.c
index 3670575dcc..3670575dcc 100644
--- a/source4/lib/fault.c
+++ b/source4/lib/util/fault.c
diff --git a/source4/lib/fsusage.c b/source4/lib/util/fsusage.c
index f817672544..f817672544 100644
--- a/source4/lib/fsusage.c
+++ b/source4/lib/util/fsusage.c
diff --git a/source4/lib/genrand.c b/source4/lib/util/genrand.c
index 1149314d0b..1149314d0b 100644
--- a/source4/lib/genrand.c
+++ b/source4/lib/util/genrand.c
diff --git a/source4/lib/idtree.c b/source4/lib/util/idtree.c
index a67a80940a..a67a80940a 100644
--- a/source4/lib/idtree.c
+++ b/source4/lib/util/idtree.c
diff --git a/source4/lib/module.c b/source4/lib/util/module.c
index 672d8df7ce..672d8df7ce 100644
--- a/source4/lib/module.c
+++ b/source4/lib/util/module.c
diff --git a/source4/lib/ms_fnmatch.c b/source4/lib/util/ms_fnmatch.c
index 699341bede..699341bede 100644
--- a/source4/lib/ms_fnmatch.c
+++ b/source4/lib/util/ms_fnmatch.c
diff --git a/source4/lib/mutex.c b/source4/lib/util/mutex.c
index 480ba92cc0..480ba92cc0 100644
--- a/source4/lib/mutex.c
+++ b/source4/lib/util/mutex.c
diff --git a/source4/lib/mutex.h b/source4/lib/util/mutex.h
index bb26c84f5d..018c134bcb 100644
--- a/source4/lib/mutex.h
+++ b/source4/lib/util/mutex.h
@@ -21,6 +21,8 @@
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
+struct mutex_ops;
+
/* To add a new read/write lock, add it to enum rwlock_id
*/
enum rwlock_id { RWLOCK_SMBD, /* global smbd lock */
diff --git a/source4/lib/pidfile.c b/source4/lib/util/pidfile.c
index 9a7c197f70..9a7c197f70 100644
--- a/source4/lib/pidfile.c
+++ b/source4/lib/util/pidfile.c
diff --git a/source4/lib/util/safe_string.h b/source4/lib/util/safe_string.h
new file mode 100644
index 0000000000..43e094467c
--- /dev/null
+++ b/source4/lib/util/safe_string.h
@@ -0,0 +1,55 @@
+/*
+ Unix SMB/CIFS implementation.
+ Safe string handling routines.
+ Copyright (C) Andrew Tridgell 1994-1998
+
+ 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.
+*/
+
+#ifndef _SAFE_STRING_H
+#define _SAFE_STRING_H
+
+#ifndef _SPLINT_ /* http://www.splint.org */
+/* Some macros to ensure people don't use buffer overflow vulnerable string
+ functions. */
+
+#ifdef bcopy
+#undef bcopy
+#endif /* bcopy */
+#define bcopy(src,dest,size) __ERROR__XX__NEVER_USE_BCOPY___;
+
+#ifdef strcpy
+#undef strcpy
+#endif /* strcpy */
+#define strcpy(dest,src) __ERROR__XX__NEVER_USE_STRCPY___;
+
+#ifdef strcat
+#undef strcat
+#endif /* strcat */
+#define strcat(dest,src) __ERROR__XX__NEVER_USE_STRCAT___;
+
+#ifdef sprintf
+#undef sprintf
+#endif /* sprintf */
+#define sprintf __ERROR__XX__NEVER_USE_SPRINTF__;
+
+#endif /* !_SPLINT_ */
+
+/* replace some string functions with multi-byte
+ versions */
+#define strlower(s) strlower_m(s)
+#define strupper(s) strupper_m(s)
+
+#endif
diff --git a/source4/lib/select.c b/source4/lib/util/select.c
index a1b2e04065..a1b2e04065 100644
--- a/source4/lib/select.c
+++ b/source4/lib/util/select.c
diff --git a/source4/lib/signal.c b/source4/lib/util/signal.c
index 6c0bb4007a..6c0bb4007a 100644
--- a/source4/lib/signal.c
+++ b/source4/lib/util/signal.c
diff --git a/source4/lib/substitute.c b/source4/lib/util/substitute.c
index 34a2ad9f82..34a2ad9f82 100644
--- a/source4/lib/substitute.c
+++ b/source4/lib/util/substitute.c
diff --git a/source4/lib/system.c b/source4/lib/util/system.c
index 655b4a1054..655b4a1054 100644
--- a/source4/lib/system.c
+++ b/source4/lib/util/system.c
diff --git a/source4/lib/time.c b/source4/lib/util/time.c
index 7721a2c456..7721a2c456 100644
--- a/source4/lib/time.c
+++ b/source4/lib/util/time.c
diff --git a/source4/lib/unix_privs.c b/source4/lib/util/unix_privs.c
index 3c0f319776..3c0f319776 100644
--- a/source4/lib/unix_privs.c
+++ b/source4/lib/util/unix_privs.c
diff --git a/source4/lib/util.c b/source4/lib/util/util.c
index 17dde332e1..17dde332e1 100644
--- a/source4/lib/util.c
+++ b/source4/lib/util/util.c
diff --git a/source4/lib/util/util.h b/source4/lib/util/util.h
new file mode 100644
index 0000000000..55d775e25c
--- /dev/null
+++ b/source4/lib/util/util.h
@@ -0,0 +1,81 @@
+/*
+ Unix SMB/CIFS implementation.
+ Utility functions for Samba
+ Copyright (C) Andrew Tridgell 1992-1999
+ Copyright (C) John H Terpstra 1996-1999
+ Copyright (C) Luke Kenneth Casson Leighton 1996-1999
+ Copyright (C) Paul Ashton 1998 - 1999
+
+ 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.
+*/
+
+#ifndef _SAMBA_UTIL_H_
+#define _SAMBA_UTIL_H_
+
+struct substitute_context;
+
+#include "util/xfile.h"
+#include "util/debug.h"
+#include "util/mutex.h"
+#include "util/byteorder.h"
+#include "util/util_proto.h"
+
+/* zero a structure */
+#define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
+
+/* zero a structure given a pointer to the structure */
+#define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
+
+/* zero a structure given a pointer to the structure - no zero check */
+#define ZERO_STRUCTPN(x) memset((char *)(x), 0, sizeof(*(x)))
+
+/* pointer difference macro */
+#define PTR_DIFF(p1,p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
+
+/* work out how many elements there are in a static array */
+#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
+
+/* assert macros */
+#define SMB_ASSERT(b) do { if (!(b)) { \
+ DEBUG(0,("PANIC: assert failed at %s(%d)\n", __FILE__, __LINE__)); \
+ smb_panic("assert failed"); }} while (0)
+
+#ifndef MIN
+#define MIN(a,b) ((a)<(b)?(a):(b))
+#endif
+
+#ifndef MAX
+#define MAX(a,b) ((a)>(b)?(a):(b))
+#endif
+
+#ifndef ABS
+#define ABS(a) ((a)>0?(a):(-(a)))
+#endif
+
+#ifndef SAFE_FREE /* Oh no this is also defined in tdb.h */
+/**
+ * Free memory if the pointer and zero the pointer.
+ *
+ * @note You are explicitly allowed to pass NULL pointers -- they will
+ * always be ignored.
+ **/
+#define SAFE_FREE(x) do { if ((x) != NULL) {free(discard_const_p(void *, (x))); (x)=NULL;} } while(0)
+#endif
+
+#define malloc_p(type) (type *)malloc(sizeof(type))
+#define malloc_array_p(type, count) (type *)realloc_array(NULL, sizeof(type), count)
+#define realloc_p(p, type, count) (type *)realloc_array(p, sizeof(type), count)
+
+#endif /* _SAMBA_UTIL_H_ */
diff --git a/source4/lib/util_file.c b/source4/lib/util/util_file.c
index 246b03b4aa..246b03b4aa 100644
--- a/source4/lib/util_file.c
+++ b/source4/lib/util/util_file.c
diff --git a/source4/lib/util_getent.c b/source4/lib/util/util_getent.c
index 9f58472fb8..9f58472fb8 100644
--- a/source4/lib/util_getent.c
+++ b/source4/lib/util/util_getent.c
diff --git a/source4/lib/util_pw.c b/source4/lib/util/util_pw.c
index 19893e396f..19893e396f 100644
--- a/source4/lib/util_pw.c
+++ b/source4/lib/util/util_pw.c
diff --git a/source4/lib/util_sock.c b/source4/lib/util/util_sock.c
index 8a65a27d02..8a65a27d02 100644
--- a/source4/lib/util_sock.c
+++ b/source4/lib/util/util_sock.c
diff --git a/source4/lib/util_str.c b/source4/lib/util/util_str.c
index b46787e3ea..b46787e3ea 100644
--- a/source4/lib/util_str.c
+++ b/source4/lib/util/util_str.c
diff --git a/source4/lib/util_strlist.c b/source4/lib/util/util_strlist.c
index ec6c58162f..ec6c58162f 100644
--- a/source4/lib/util_strlist.c
+++ b/source4/lib/util/util_strlist.c
diff --git a/source4/lib/util_unistr.c b/source4/lib/util/util_unistr.c
index b35822877c..b35822877c 100644
--- a/source4/lib/util_unistr.c
+++ b/source4/lib/util/util_unistr.c
diff --git a/source4/lib/xfile.c b/source4/lib/util/xfile.c
index 794e3f0f5e..794e3f0f5e 100644
--- a/source4/lib/xfile.c
+++ b/source4/lib/util/xfile.c
diff --git a/source4/lib/xfile.h b/source4/lib/util/xfile.h
index 89fa9d1e11..89fa9d1e11 100644
--- a/source4/lib/xfile.h
+++ b/source4/lib/util/xfile.h
diff --git a/source4/lib/version.c b/source4/lib/version.c
deleted file mode 100644
index b7aa81a081..0000000000
--- a/source4/lib/version.c
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- Unix SMB/CIFS implementation.
- Samba Version functions
-
- Copyright (C) Stefan Metzmacher 2003
-
- 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.
-*/
-
-#include "includes.h"
-#include "version.h"
-
-const char *samba_version_string(void)
-{
- const char *official_string = SAMBA_VERSION_OFFICIAL_STRING;
-#ifdef SAMBA_VERSION_RELEASE_NICKNAME
- const char *release_nickname = SAMBA_VERSION_RELEASE_NICKNAME;
-#else
- const char *release_nickname = NULL;
-#endif
-#ifdef SAMBA_VERSION_VENDOR_SUFFIX
- const char *vendor_suffix = SAMBA_VERSION_VENDOR_SUFFIX;
-#else
- const char *vendor_suffix = NULL;
-#endif
-#ifdef SAMBA_VERSION_VENDOR_PATCH
- const char *vendor_patch = SAMBA_VERSION_VENDOR_PATCH;
-#else
- const char *vendor_patch = NULL;
-#endif
- static char *samba_version;
- static BOOL init_samba_version;
-
- if (init_samba_version) {
- return samba_version;
- }
-
- samba_version = talloc_asprintf(talloc_autofree_context(),
- "%s%s%s%s%s%s%s%s",
- official_string,
- (vendor_suffix?"-":""),
- (vendor_suffix?vendor_suffix:""),
- (vendor_patch?"-":""),
- (vendor_patch?vendor_patch:""),
- (release_nickname?" (":""),
- (release_nickname?release_nickname:""),
- (release_nickname?")":""));
-
- init_samba_version = True;
- return samba_version;
-}