#ifndef _SAMBA_UTIL_H_
#define _SAMBA_UTIL_H_
-#if _SAMBA_BUILD_ == 4
-#include "../lib/util/charset/charset.h"
-#endif
-#include "../lib/util/attr.h"
+#include "lib/util/charset/charset.h"
+#include "lib/util/attr.h"
/* for TALLOC_CTX */
#include <talloc.h>
struct smbsrv_tcon;
-extern const char *logfile;
extern const char *panic_action;
-#include "../lib/util/time.h"
-#include "../lib/util/data_blob.h"
-#include "../lib/util/xfile.h"
-#include "../lib/util/mutex.h"
-#include "../lib/util/byteorder.h"
-#include "../lib/util/talloc_stack.h"
+#include "lib/util/time.h"
+#include "lib/util/data_blob.h"
+#include "lib/util/xfile.h"
+#include "lib/util/byteorder.h"
+#include "lib/util/talloc_stack.h"
/**
* assert macros
__FILE__, __LINE__, #b)); }} while (0)
#endif
-#if _SAMBA_BUILD_ == 4
-#ifdef VALGRIND
-#define strlen(x) valgrind_strlen(x)
-size_t valgrind_strlen(const char *s);
-#endif
-#endif
-
#ifndef ABS
#define ABS(a) ((a)>0?(a):(-(a)))
#endif
-#include "../lib/util/memory.h"
+#include "lib/util/memory.h"
+
+#include "lib/util/string_wrappers.h"
/**
* Write backtrace to debug log
/**
Something really nasty happened - panic !
**/
-_PUBLIC_ _NORETURN_ void smb_panic(const char *why);
+typedef void (*smb_panic_handler_t)(const char *why);
+
+_PUBLIC_ void fault_configure(smb_panic_handler_t panic_handler);
+_PUBLIC_ void fault_setup(void);
+_PUBLIC_ void fault_setup_disable(void);
+_PUBLIC_ void dump_core_setup(const char *progname, const char *logfile);
+_PUBLIC_ void smb_panic(const char *reason);
-#if _SAMBA_BUILD_ == 4
-/**
-setup our fault handlers
-**/
-_PUBLIC_ void fault_setup(const char *pname);
-#endif
/**
register a fault handler.
/* The following definitions come from lib/util/system.c */
-
-struct in_addr;
+void *sys_memalign( size_t align, size_t size );
/**************************************************************************
A wrapper for gethostbyname() that tries avoids looking up hostnames
_PUBLIC_ struct hostent *sys_gethostbyname(const char *name);
_PUBLIC_ struct in_addr sys_inet_makeaddr(int net, int host);
-/* The following definitions come from lib/util/genrand.c */
+/**
+ * Wrapper for fork used to invalid pid cache.
+ **/
+_PUBLIC_ pid_t sys_fork(void);
+/**
+ * Wrapper for getpid. Ensures we only do a system call *once*.
+ **/
+_PUBLIC_ pid_t sys_getpid(void);
+
+_PUBLIC_ int sys_getpeereid( int s, uid_t *uid);
+
+struct sockaddr;
+
+_PUBLIC_ int sys_getnameinfo(const struct sockaddr *psa,
+ int salen,
+ char *host,
+ size_t hostlen,
+ char *service,
+ size_t servlen,
+ int flags);
+_PUBLIC_ int sys_connect(int fd, const struct sockaddr * addr);
+
+/* The following definitions come from lib/util/genrand.c */
/**
Copy any user given reseed data.
**/
**/
_PUBLIC_ bool check_password_quality(const char *s);
+/**
+ * Generate a random text password.
+ */
+_PUBLIC_ char *generate_random_password(TALLOC_CTX *mem_ctx, size_t min, size_t max);
+
/**
Use the random number generator to generate a random string.
**/
*/
_PUBLIC_ char *generate_random_str(TALLOC_CTX *mem_ctx, size_t len);
+/**
+ * Generate an array of unique text strings all of the same length.
+ * The returned strings will be allocated.
+ * Returns NULL if the number of unique combinations cannot be created.
+ *
+ * Characters used are: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_-#.,
+ */
+_PUBLIC_ char** generate_unique_strs(TALLOC_CTX *mem_ctx, size_t len,
+ uint32_t num);
+
/* The following definitions come from lib/util/dprintf.c */
#if _SAMBA_BUILD_ == 4
/* The following definitions come from lib/util/util_str.c */
+bool next_token_talloc(TALLOC_CTX *ctx,
+ const char **ptr,
+ char **pp_buff,
+ const char *sep);
+
+/**
+ * Get the next token from a string, return false if none found. Handles
+ * double-quotes. This version does not trim leading separator characters
+ * before looking for a token.
+ */
+bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx,
+ const char **ptr,
+ char **pp_buff,
+ const char *sep);
+
/**
Trim the specified elements off the front and back of a string.
**/
_PUBLIC_ _PURE_ size_t count_chars(const char *s, char c);
-/**
- Safe string copy into a known length string. maxlength does not
- include the terminating zero.
-**/
-_PUBLIC_ char *safe_strcpy(char *dest,const char *src, size_t maxlength);
-
-/**
- Safe string cat into a string. maxlength does not
- include the terminating zero.
-**/
-_PUBLIC_ char *safe_strcat(char *dest, const char *src, size_t maxlength);
-
/**
Routine to get hex characters and turn them into a 16 byte array.
the array can be variable length, and any non-hex-numeric
**/
_PUBLIC_ void string_sub(char *s,const char *pattern, const char *insert, size_t len);
+_PUBLIC_ void string_sub_once(char *s, const char *pattern,
+ const char *insert, size_t len);
_PUBLIC_ char *string_sub_talloc(TALLOC_CTX *mem_ctx, const char *s,
const char *pattern, const char *insert);
**/
_PUBLIC_ void rfc1738_unescape(char *buf);
+
+/**
+ * rfc1738_escape
+ * Returns a static buffer that contains the RFC
+ * 1738 compliant, escaped version of the given url. (escapes unsafe and % characters)
+ **/
+_PUBLIC_ char *rfc1738_escape(TALLOC_CTX *mem_ctx, const char *url);
+
+/**
+ * rfc1738_escape_unescaped
+ *
+ * Returns a static buffer that contains
+ * the RFC 1738 compliant, escaped version of the given url (escapes unsafe chars only)
+ **/
+_PUBLIC_ char *rfc1738_escape_unescaped(TALLOC_CTX *mem_ctx, const char *url);
+
+/**
+ * rfc1738_escape_part
+ * Returns a static buffer that contains the RFC
+ * 1738 compliant, escaped version of the given url segment. (escapes
+ * unsafe, reserved and % chars) It would mangle the :// in http://,
+ * and mangle paths (because of /).
+ **/
+_PUBLIC_ char *rfc1738_escape_part(TALLOC_CTX *mem_ctx, const char *url);
+
/**
format a string into length-prefixed dotted domain format, as used in NBT
and in some ADS structures
*/
_PUBLIC_ bool conv_str_bool(const char * str, bool * val);
-#if _SAMBA_BUILD_ == 4
/**
* Convert a size specification like 16K into an integral number of bytes.
**/
-_PUBLIC_ bool conv_str_size(const char * str, uint64_t * val);
-#endif
+_PUBLIC_ bool conv_str_size_error(const char * str, uint64_t * val);
/**
* Parse a uint64_t value from a string
**/
_PUBLIC_ void string_replace(char *s, char oldc, char newc);
+/**
+ Base64 decode a string, place into a data blob. Caller to data_blob_free() the result.
+**/
+_PUBLIC_ DATA_BLOB base64_decode_data_blob(const char *s);
+/**
+ Base64 decode a string, inplace
+**/
+_PUBLIC_ void base64_decode_inplace(char *s);
+/**
+ Base64 encode a binary data blob into a string
+**/
+_PUBLIC_ char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data);
+
/**
* Compare 2 strings.
*
#define LIST_SEP " \t,\n\r"
#endif
+/**
+ build an empty (only NULL terminated) list of strings (for expansion with str_list_add() etc)
+*/
+_PUBLIC_ char **str_list_make_empty(TALLOC_CTX *mem_ctx);
+
+/**
+ place the only element 'entry' into a new, NULL terminated string list
+*/
+_PUBLIC_ char **str_list_make_single(TALLOC_CTX *mem_ctx,
+ const char *entry);
+
/**
build a null terminated list of strings from a input string and a
separator list. The separator list must contain characters less than
or equal to 0x2f for this to work correctly on multi-byte strings
*/
-_PUBLIC_ char **str_list_make(TALLOC_CTX *mem_ctx, const char *string, const char *sep);
+_PUBLIC_ char **str_list_make(TALLOC_CTX *mem_ctx, const char *string,
+ const char *sep);
/**
* build a null terminated list of strings from an argv-like input string
- * Entries are seperated by spaces and can be enclosed by quotes.
+ * Entries are separated by spaces and can be enclosed by quotes.
* Does NOT support escaping
*/
-_PUBLIC_ const char **str_list_make_shell(TALLOC_CTX *mem_ctx, const char *string, const char *sep);
+_PUBLIC_ char **str_list_make_shell(TALLOC_CTX *mem_ctx, const char *string, const char *sep);
/**
* join a list back to one string
*/
-_PUBLIC_ char *str_list_join(TALLOC_CTX *mem_ctx, const char **list, char seperator);
+_PUBLIC_ char *str_list_join(TALLOC_CTX *mem_ctx, const char **list, char separator);
/** join a list back to one (shell-like) string; entries
- * seperated by spaces, using quotes where necessary */
+ * separated by spaces, using quotes where necessary */
_PUBLIC_ char *str_list_join_shell(TALLOC_CTX *mem_ctx, const char **list, char sep);
/**
/**
Return true if all the elements of the list match exactly.
*/
-_PUBLIC_ bool str_list_equal(const char **list1, const char **list2);
+_PUBLIC_ bool str_list_equal(const char * const *list1, const char * const *list2);
/**
add an entry to a string list
return true if a string is in a list, case insensitively
*/
_PUBLIC_ bool str_list_check_ci(const char **list, const char *s);
+/**
+ append one list to another - expanding list1
+*/
+_PUBLIC_ const char **str_list_append(const char **list1,
+ const char * const *list2);
+
+/**
+ remove duplicate elements from a list
+*/
+_PUBLIC_ const char **str_list_unique(const char **list);
+
+/*
+ very useful when debugging complex list related code
+ */
+_PUBLIC_ void str_list_show(const char **list);
+
+
+/**
+ append one list to another - expanding list1
+ this assumes the elements of list2 are const pointers, so we can re-use them
+*/
+_PUBLIC_ const char **str_list_append_const(const char **list1,
+ const char **list2);
+
+/**
+ add an entry to a string list
+ this assumes s will not change
+*/
+_PUBLIC_ const char **str_list_add_const(const char **list, const char *s);
+
+/**
+ copy a string list
+ this assumes list will not change
+*/
+_PUBLIC_ const char **str_list_copy_const(TALLOC_CTX *mem_ctx,
+ const char **list);
+
+/**
+ * Needed for making an "unconst" list "const"
+ */
+_PUBLIC_ const char **const_str_list(char **list);
+
/* The following definitions come from lib/util/util_file.c */
_PUBLIC_ int fdprintf(int fd, const char *format, ...) PRINTF_ATTRIBUTE(2,3);
_PUBLIC_ bool large_file_support(const char *path);
+/*
+ compare two files, return true if the two files have the same content
+ */
+bool file_compare(const char *path1, const char *path2);
+
/* The following definitions come from lib/util/util.c */
/**
Sleep for a specified number of milliseconds.
**/
-_PUBLIC_ void msleep(unsigned int t);
-
-/**
- Get my own name, return in malloc'ed storage.
-**/
-_PUBLIC_ char* get_myname(void);
-
-/**
- Return true if a string could be a pure IP address.
-**/
-_PUBLIC_ bool is_ipaddress(const char *str);
+_PUBLIC_ void smb_msleep(unsigned int t);
/**
- Interpret an internet address or name into an IP address in 4 byte form.
+ Get my own name, return in talloc'ed storage.
**/
-_PUBLIC_ uint32_t interpret_addr(const char *str);
-
-/**
- A convenient addition to interpret_addr().
-**/
-_PUBLIC_ struct in_addr interpret_addr2(const char *str);
-
-/**
- Check if an IP is the 0.0.0.0.
-**/
-_PUBLIC_ bool is_zero_ip_v4(struct in_addr ip);
-
-/**
- Are two IPs on the same subnet?
-**/
-_PUBLIC_ bool same_net_v4(struct in_addr ip1,struct in_addr ip2,struct in_addr mask);
-
-_PUBLIC_ bool is_ipaddress_v4(const char *str);
+_PUBLIC_ char* get_myname(TALLOC_CTX *mem_ctx);
/**
Check if a process exists. Does this work on all unixes?
**/
_PUBLIC_ bool fcntl_lock(int fd, int op, off_t offset, off_t count, int type);
+/**
+ * Write dump of binary data to a callback
+ */
+void dump_data_cb(const uint8_t *buf, int len,
+ bool omit_zero_bytes,
+ void (*cb)(const char *buf, void *private_data),
+ void *private_data);
+
/**
* Write dump of binary data to the log file.
*
* Write dump of binary data to the log file.
*
* The data is only written if the log level is at least level.
- * 16 zero bytes in a row are ommited
+ * 16 zero bytes in a row are omitted
*/
_PUBLIC_ void dump_data_skip_zeros(int level, const uint8_t *buf, int len);
PROTOCOL_SMB2
};
-int ms_fnmatch(const char *pattern, const char *string, enum protocol_types protocol);
-
-/** a generic fnmatch function - uses for non-CIFS pattern matching */
-int gen_fnmatch(const char *pattern, const char *string);
#endif
-/* The following definitions come from lib/util/mutex.c */
-
+int ms_fnmatch_protocol(const char *pattern, const char *string, int protocol);
-/**
- register a set of mutex/rwlock handlers.
- Should only be called once in the execution of smbd.
-*/
-_PUBLIC_ bool register_mutex_handlers(const char *name, struct mutex_ops *ops);
+/** a generic fnmatch function - uses for non-CIFS pattern matching */
+int gen_fnmatch(const char *pattern, const char *string);
/* The following definitions come from lib/util/idtree.c */
/* The following definitions come from lib/util/become_daemon.c */
-#if _SAMBA_BUILD_ == 4
+/**
+ Close the low 3 fd's and open dev/null in their place
+**/
+_PUBLIC_ void close_low_fds(bool stderr_too);
+
/**
Become a daemon, discarding the controlling terminal.
**/
-_PUBLIC_ void become_daemon(bool fork);
-#endif
+_PUBLIC_ void become_daemon(bool do_fork, bool no_process_group, bool log_stdout);
/**
* Load a ini-style file.
bool (*pfunc)(const char *, const char *, void *),
void *userdata);
+bool unmap_file(void *start, size_t size);
+
+void print_asc(int level, const uint8_t *buf,int len);
+void print_asc_cb(const uint8_t *buf, int len,
+ void (*cb)(const char *buf, void *private_data),
+ void *private_data);
+
/**
- * Add-on to talloc_get_type
+ * Add an id to an array of ids.
+ *
+ * num should be a pointer to an integer that holds the current
+ * number of elements in ids. It will be updated by this function.
*/
-_PUBLIC_ void *talloc_check_name_abort(const void *ptr, const char *name);
-#define talloc_get_type_abort(ptr, type) \
- (type *)talloc_check_name_abort(ptr, #type)
-bool unmap_file(void *start, size_t size);
+bool add_uid_to_array_unique(TALLOC_CTX *mem_ctx, uid_t uid,
+ uid_t **uids, uint32_t *num_uids);
+bool add_gid_to_array_unique(TALLOC_CTX *mem_ctx, gid_t gid,
+ gid_t **gids, uint32_t *num_gids);
+
+/**
+ * Allocate anonymous shared memory of the given size
+ */
+void *anonymous_shared_allocate(size_t bufsz);
+void anonymous_shared_free(void *ptr);
+
+/*
+ run a command as a child process, with a timeout.
+
+ any stdout/stderr from the child will appear in the Samba logs with
+ the specified log levels
+
+ If callback is set then the callback is called on completion
+ with the return code from the command
+ */
+struct tevent_context;
+struct tevent_req;
+struct tevent_req *samba_runcmd_send(TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ struct timeval endtime,
+ int stdout_log_level,
+ int stderr_log_level,
+ const char * const *argv0, ...);
+int samba_runcmd_recv(struct tevent_req *req, int *perrno);
+
+#ifdef DEVELOPER
+void samba_start_debugger(void);
+#endif
+
+/**
+ * @brief Returns an absolute path to a file in the Samba modules directory.
+ *
+ * @param name File to find, relative to MODULESDIR.
+ *
+ * @retval Pointer to a string containing the full path.
+ **/
+char *modules_path(TALLOC_CTX *mem_ctx, const char *name);
+
+/**
+ * @brief Returns an absolute path to a file in the Samba data directory.
+ *
+ * @param name File to find, relative to CODEPAGEDIR.
+ *
+ * @retval Pointer to a talloc'ed string containing the full path.
+ **/
+char *data_path(TALLOC_CTX *mem_ctx, const char *name);
+
+/**
+ * @brief Returns the platform specific shared library extension.
+ *
+ * @retval Pointer to a const char * containing the extension.
+ **/
+const char *shlib_ext(void);
-void print_asc(int level, const uint8_t *buf,int len);
#endif /* _SAMBA_UTIL_H_ */