util.h revision b114bcc370c8d78b5e9f43963cfa91213901c3be
967e5f3c25249c779575864692935627004d3f9eChristian Maeder/*
967e5f3c25249c779575864692935627004d3f9eChristian Maeder Authors:
81d182b21020b815887e9057959228546cf61b6bChristian Maeder Simo Sorce <ssorce@redhat.com>
f11f713bebd8e1e623a0a4361065df256033de47Christian Maeder
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu Copyright (C) 2009 Red Hat
967e5f3c25249c779575864692935627004d3f9eChristian Maeder
3f69b6948966979163bdfe8331c38833d5d90ecdChristian Maeder This program is free software; you can redistribute it and/or modify
967e5f3c25249c779575864692935627004d3f9eChristian Maeder it under the terms of the GNU General Public License as published by
89054b2b95a3f92e78324dc852f3d34704e2ca49Christian Maeder the Free Software Foundation; either version 3 of the License, or
f3a94a197960e548ecd6520bb768cb0d547457bbChristian Maeder (at your option) any later version.
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder This program is distributed in the hope that it will be useful,
967e5f3c25249c779575864692935627004d3f9eChristian Maeder but WITHOUT ANY WARRANTY; without even the implied warranty of
967e5f3c25249c779575864692935627004d3f9eChristian Maeder MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
967e5f3c25249c779575864692935627004d3f9eChristian Maeder GNU General Public License for more details.
967e5f3c25249c779575864692935627004d3f9eChristian Maeder
fd896e2068ad7e50aed66ac18c3720ea7ff2619fChristian Maeder You should have received a copy of the GNU General Public License
7221c71b38c871ce66eee4537cb681d468308dfbChristian Maeder along with this program. If not, see <http://www.gnu.org/licenses/>.
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder*/
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder#ifndef __SSSD_UTIL_H__
ac19f8695aa1b2d2d1cd1319da2530edd8f46a96Christian Maeder#define __SSSD_UTIL_H__
8b9fda012e5ee53b7b2320c0638896a0ff6e99f3Christian Maeder
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder#include "config.h"
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder#include <stdio.h>
e1839fb37a3a2ccd457464cb0dcc5efd466dbe22Christian Maeder#include <stdint.h>
4ef2a978e66e2246ff0b7f00c77deb7aabb28b8eChristian Maeder#include <stdbool.h>
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder#include <unistd.h>
fd896e2068ad7e50aed66ac18c3720ea7ff2619fChristian Maeder#include <string.h>
967e5f3c25249c779575864692935627004d3f9eChristian Maeder#include <strings.h>
8e9c3881fb6e710b1e08bf5ac8ff9d393df2e74eChristian Maeder#include <ctype.h>
e0f1794e365dd347e97b37d7d22b2fce27296fa1Christian Maeder#include <errno.h>
72b9099aeec0762bae4546db3bc4b48721027bf4Christian Maeder#include <libintl.h>
72b9099aeec0762bae4546db3bc4b48721027bf4Christian Maeder#include <limits.h>
72b9099aeec0762bae4546db3bc4b48721027bf4Christian Maeder#include <locale.h>
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder#include <time.h>
ad270004874ce1d0697fb30d7309f180553bb315Christian Maeder#include <pcre.h>
fd896e2068ad7e50aed66ac18c3720ea7ff2619fChristian Maeder#include <sys/types.h>
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder#include <sys/stat.h>
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder#include <arpa/inet.h>
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder#include <netinet/in.h>
72b9099aeec0762bae4546db3bc4b48721027bf4Christian Maeder
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder#include <talloc.h>
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder#include <tevent.h>
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder#include <ldb.h>
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder#include <dhash.h>
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
51fb5d7edd9369c367dda2f8b15ddd6f8a146606Christian Maeder#include "confdb/confdb.h"
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder#include "util/atomic_io.h"
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder#include "util/util_errors.h"
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder#include "util/util_safealign.h"
51fb5d7edd9369c367dda2f8b15ddd6f8a146606Christian Maeder#include "util/sss_format.h"
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder#define _(STRING) gettext (STRING)
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder#define ENUM_INDICATOR "*"
72b9099aeec0762bae4546db3bc4b48721027bf4Christian Maeder
72b9099aeec0762bae4546db3bc4b48721027bf4Christian Maeder#define CLEAR_MC_FLAG "clear_mc_flag"
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder#ifdef HAVE_FUNCTION_ATTRIBUTE_FORMAT
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder#define SSS_ATTRIBUTE_PRINTF(a1, a2) __attribute__ ((format (printf, a1, a2)))
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder#else
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder#define SSS_ATTRIBUTE_PRINTF(a1, a2)
7a879b08ae0ca30006f9be887a73212b07f10204Christian Maeder#endif
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maederextern const char *debug_prg_name;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maederextern int debug_level;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maederextern int debug_timestamps;
67d92da5e9610aabad39055a16031154b4dc3748Christian Maederextern int debug_microseconds;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maederextern int debug_to_file;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maederextern int debug_to_stderr;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maederextern const char *debug_log_file;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maedervoid debug_fn(const char *file,
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder long line,
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder const char *function,
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder int level,
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder const char *format, ...) SSS_ATTRIBUTE_PRINTF(5,6);
67d92da5e9610aabad39055a16031154b4dc3748Christian Maederint debug_convert_old_level(int old_level);
7a879b08ae0ca30006f9be887a73212b07f10204Christian Maedererrno_t set_debug_file_from_fd(const int fd);
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder#define SSS_DOM_ENV "_SSS_DOM"
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder
a89389521ddf76109168a0b339031575aafbd512Christian Maeder#define SSSDBG_FATAL_FAILURE 0x0010 /* level 0 */
a89389521ddf76109168a0b339031575aafbd512Christian Maeder#define SSSDBG_CRIT_FAILURE 0x0020 /* level 1 */
a89389521ddf76109168a0b339031575aafbd512Christian Maeder#define SSSDBG_OP_FAILURE 0x0040 /* level 2 */
a89389521ddf76109168a0b339031575aafbd512Christian Maeder#define SSSDBG_MINOR_FAILURE 0x0080 /* level 3 */
a89389521ddf76109168a0b339031575aafbd512Christian Maeder#define SSSDBG_CONF_SETTINGS 0x0100 /* level 4 */
a89389521ddf76109168a0b339031575aafbd512Christian Maeder#define SSSDBG_FUNC_DATA 0x0200 /* level 5 */
a89389521ddf76109168a0b339031575aafbd512Christian Maeder#define SSSDBG_TRACE_FUNC 0x0400 /* level 6 */
a89389521ddf76109168a0b339031575aafbd512Christian Maeder#define SSSDBG_TRACE_LIBS 0x1000 /* level 7 */
a89389521ddf76109168a0b339031575aafbd512Christian Maeder#define SSSDBG_TRACE_INTERNAL 0x2000 /* level 8 */
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian Maeder#define SSSDBG_TRACE_ALL 0x4000 /* level 9 */
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian Maeder#define SSSDBG_IMPORTANT_INFO SSSDBG_OP_FAILURE
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder#define SSSDBG_INVALID -1
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian Maeder#define SSSDBG_UNRESOLVED 0
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian Maeder#define SSSDBG_MASK_ALL 0xFFF0 /* enable all debug levels */
ceef5f7843a1f96fe5a62e0f6880e38b3d5f4708Christian Maeder#define SSSDBG_DEFAULT SSSDBG_FATAL_FAILURE
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian Maeder
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian Maeder#define SSSDBG_TIMESTAMP_UNRESOLVED -1
ceef5f7843a1f96fe5a62e0f6880e38b3d5f4708Christian Maeder#define SSSDBG_TIMESTAMP_DEFAULT 1
ceef5f7843a1f96fe5a62e0f6880e38b3d5f4708Christian Maeder
ceef5f7843a1f96fe5a62e0f6880e38b3d5f4708Christian Maeder#define SSSDBG_MICROSECONDS_UNRESOLVED -1
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian Maeder#define SSSDBG_MICROSECONDS_DEFAULT 0
ceef5f7843a1f96fe5a62e0f6880e38b3d5f4708Christian Maeder
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian Maeder#define SSSD_DEBUG_OPTS \
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder {"debug-level", 'd', POPT_ARG_INT, &debug_level, 0, \
e77eadd37125110f448dd5ddec7da5b78d35285fChristian Maeder _("Debug level"), NULL}, \
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder {"debug-to-files", 'f', POPT_ARG_NONE, &debug_to_file, 0, \
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian Maeder _("Send the debug output to files instead of stderr"), NULL }, \
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian Maeder {"debug-to-stderr", 0, POPT_ARG_NONE | POPT_ARGFLAG_DOC_HIDDEN, &debug_to_stderr, 0, \
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian Maeder _("Send the debug output to stderr directly."), NULL }, \
ceef5f7843a1f96fe5a62e0f6880e38b3d5f4708Christian Maeder {"debug-timestamps", 0, POPT_ARG_INT, &debug_timestamps, 0, \
ceef5f7843a1f96fe5a62e0f6880e38b3d5f4708Christian Maeder _("Add debug timestamps"), NULL}, \
ceef5f7843a1f96fe5a62e0f6880e38b3d5f4708Christian Maeder {"debug-microseconds", 0, POPT_ARG_INT, &debug_microseconds, 0, \
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian Maeder _("Show timestamps with microseconds"), NULL},
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian Maeder
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder/** \def DEBUG(level, format, ...)
67086e0fe40a985c5e8a3cf50e611f43234580c2Christian Maeder \brief macro to generate debug messages
e77eadd37125110f448dd5ddec7da5b78d35285fChristian Maeder
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder \param level the debug level, please use one of the SSSDBG_* macros
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder \param format the debug message format string, should result in a
67086e0fe40a985c5e8a3cf50e611f43234580c2Christian Maeder newline-terminated message
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder \param ... the debug message format arguments
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder*/
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder#define DEBUG(level, format, ...) do { \
d48085f765fca838c1d972d2123601997174583dChristian Maeder int __debug_macro_level = level; \
d48085f765fca838c1d972d2123601997174583dChristian Maeder if (DEBUG_IS_SET(__debug_macro_level)) { \
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder debug_fn(__FILE__, __LINE__, __FUNCTION__, \
d48085f765fca838c1d972d2123601997174583dChristian Maeder __debug_macro_level, \
d48085f765fca838c1d972d2123601997174583dChristian Maeder format, ##__VA_ARGS__); \
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maeder } \
d48085f765fca838c1d972d2123601997174583dChristian Maeder} while (0)
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder/** \def DEBUG_IS_SET(level)
e7ce154edb906685b3fa7f6c0a764e18a4658068Christian Maeder \brief checks whether level is set in debug_level
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder
9b30898b139ee02f97ac933b6d935ef0a4206921Christian Maeder \param level the debug level, please use one of the SSSDBG*_ macros
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder*/
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder#define DEBUG_IS_SET(level) (debug_level & (level) || \
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder (debug_level == SSSDBG_UNRESOLVED && \
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder (level & (SSSDBG_FATAL_FAILURE | \
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder SSSDBG_CRIT_FAILURE))))
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder#define DEBUG_INIT(dbg_lvl) do { \
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder if (dbg_lvl != SSSDBG_INVALID) { \
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder debug_level = debug_convert_old_level(dbg_lvl); \
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder } else { \
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder debug_level = SSSDBG_UNRESOLVED; \
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder } \
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder\
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder talloc_set_log_fn(talloc_log_fn); \
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder} while (0)
c5c193a80459071696b68baf835f1b88f0f8c82eChristian Maeder
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder/* CLI tools shall debug to stderr even when SSSD was compiled with journald
967e5f3c25249c779575864692935627004d3f9eChristian Maeder * support
18b513ff41708f24e1a7407f36b719add813ffeaChristian Maeder */
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder#define DEBUG_CLI_INIT(dbg_lvl) do { \
a89e661aad28f1b39f4fc9f9f9a4d46074234123Christian Maeder DEBUG_INIT(dbg_lvl); \
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder debug_to_stderr = 1; \
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder} while (0)
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maeder
3c8d067accf18572352351ec42ff905c7297a8a5Christian Maeder#define PRINT(fmt, ...) fprintf(stdout, gettext(fmt), ##__VA_ARGS__)
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maeder#define ERROR(fmt, ...) fprintf(stderr, gettext(fmt), ##__VA_ARGS__)
f2ee9fc53048ea92bad79e3f5d292d83efd7f8beMihai Codescu
3c8d067accf18572352351ec42ff905c7297a8a5Christian Maeder#ifndef discard_const
81d182b21020b815887e9057959228546cf61b6bChristian Maeder#define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maeder#endif
242397ba0f1cc490e892130bf0df239deeecf5daChristian Maeder
2e9985cd67e4f2414becb670ef33b8f16513e41dChristian Maeder#ifndef NULL
242397ba0f1cc490e892130bf0df239deeecf5daChristian Maeder#define NULL 0
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maeder#endif
d769b9ca726a9b50d661847c0e58c41d6ef334b4Christian Maeder
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder#define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maeder
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder#define SSSD_MAIN_OPTS SSSD_DEBUG_OPTS
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder
4072adb8c5d2c86123e8e1c1918263968f187829Christian Maeder#define SSSD_SERVER_OPTS(uid, gid) \
4072adb8c5d2c86123e8e1c1918263968f187829Christian Maeder {"uid", 0, POPT_ARG_INT, &uid, 0, \
4072adb8c5d2c86123e8e1c1918263968f187829Christian Maeder _("The user ID to run the server as"), NULL}, \
4072adb8c5d2c86123e8e1c1918263968f187829Christian Maeder {"gid", 0, POPT_ARG_INT, &gid, 0, \
4072adb8c5d2c86123e8e1c1918263968f187829Christian Maeder _("The group ID to run the server as"), NULL},
4072adb8c5d2c86123e8e1c1918263968f187829Christian Maeder
4072adb8c5d2c86123e8e1c1918263968f187829Christian Maeder#define FLAGS_NONE 0x0000
4072adb8c5d2c86123e8e1c1918263968f187829Christian Maeder#define FLAGS_DAEMON 0x0001
4072adb8c5d2c86123e8e1c1918263968f187829Christian Maeder#define FLAGS_INTERACTIVE 0x0002
4072adb8c5d2c86123e8e1c1918263968f187829Christian Maeder#define FLAGS_PID_FILE 0x0004
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder
842eedc62639561781b6c33533d1949693ef6cc5Christian Maeder#ifndef talloc_zfree
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maeder#define talloc_zfree(ptr) do { talloc_free(discard_const(ptr)); ptr = NULL; } while(0)
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maeder#endif
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maeder
842eedc62639561781b6c33533d1949693ef6cc5Christian Maeder#ifndef discard_const_p
bfa9e03532243ceb487f0384d0f6a447f1ce7670Till Mossakowski#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
967e5f3c25249c779575864692935627004d3f9eChristian Maeder# define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
967e5f3c25249c779575864692935627004d3f9eChristian Maeder#else
f2ee9fc53048ea92bad79e3f5d292d83efd7f8beMihai Codescu# define discard_const_p(type, ptr) ((type *)(ptr))
967e5f3c25249c779575864692935627004d3f9eChristian Maeder#endif
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder#endif
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder#define TEVENT_REQ_RETURN_ON_ERROR(req) do { \
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder enum tevent_req_state TRROEstate; \
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder uint64_t TRROEerr; \
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder \
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder if (tevent_req_is_error(req, &TRROEstate, &TRROEerr)) { \
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder if (TRROEstate == TEVENT_REQ_USER_ERROR) { \
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder return TRROEerr; \
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder } \
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder return ERR_INTERNAL; \
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder } \
967e5f3c25249c779575864692935627004d3f9eChristian Maeder} while (0)
967e5f3c25249c779575864692935627004d3f9eChristian Maeder
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder#define OUT_OF_ID_RANGE(id, min, max) \
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder (id == 0 || (min && (id < min)) || (max && (id > max)))
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder#include "util/dlinklist.h"
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder/* From debug.c */
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maedervoid ldb_debug_messages(void *context, enum ldb_debug_level level,
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder const char *fmt, va_list ap);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maederint chown_debug_file(const char *filename, uid_t uid, gid_t gid);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maederint open_debug_file_ex(const char *filename, FILE **filep, bool want_cloexec);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maederint open_debug_file(void);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maederint rotate_debug_files(void);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maedervoid talloc_log_fn(const char *msg);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder/* From sss_log.c */
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder#define SSS_LOG_EMERG 0 /* system is unusable */
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder#define SSS_LOG_ALERT 1 /* action must be taken immediately */
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder#define SSS_LOG_CRIT 2 /* critical conditions */
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder#define SSS_LOG_ERR 3 /* error conditions */
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder#define SSS_LOG_WARNING 4 /* warning conditions */
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder#define SSS_LOG_NOTICE 5 /* normal but significant condition */
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder#define SSS_LOG_INFO 6 /* informational */
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder#define SSS_LOG_DEBUG 7 /* debug-level messages */
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maedervoid sss_log(int priority, const char *format, ...) SSS_ATTRIBUTE_PRINTF(2, 3);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maedervoid sss_log_ext(int priority, int facility, const char *format, ...) SSS_ATTRIBUTE_PRINTF(3, 4);
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder/* from server.c */
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maederstruct main_context {
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder struct tevent_context *event_ctx;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder struct confdb_ctx *confdb_ctx;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder pid_t parent_pid;
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder};
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder
ad187062b0009820118c1b773a232e29b879a2faChristian Maedererrno_t server_common_rotate_logs(struct confdb_ctx *confdb,
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder const char *conf_entry);
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maederint die_if_parent_died(void);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maederint pidfile(const char *path, const char *name);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maederint server_setup(const char *name, int flags,
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder uid_t uid, gid_t gid,
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder const char *conf_entry,
9659c509ce5e78adc51d7b02a76274eddcba9338Christian Maeder struct main_context **main_ctx);
9659c509ce5e78adc51d7b02a76274eddcba9338Christian Maedervoid server_loop(struct main_context *main_ctx);
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maedervoid orderly_shutdown(int status);
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder/* from signal.c */
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder#include <signal.h>
9659c509ce5e78adc51d7b02a76274eddcba9338Christian Maedervoid BlockSignals(bool block, int signum);
9659c509ce5e78adc51d7b02a76274eddcba9338Christian Maedervoid (*CatchSignal(int signum,void (*handler)(int )))(int);
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder/* from memory.c */
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maedertypedef int (void_destructor_fn_t)(void *);
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maederstruct mem_holder {
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder void *mem;
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder void_destructor_fn_t *fn;
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder};
97ee7048e63953c5617342ce38c30cbcb35cc0beChristian Maeder
97ee7048e63953c5617342ce38c30cbcb35cc0beChristian Maedervoid *sss_mem_attach(TALLOC_CTX *mem_ctx,
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder void *ptr,
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder void_destructor_fn_t *fn);
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder
a74f814d3b445eadad6f68737a98a7a303698affChristian Maederint password_destructor(void *memctx);
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder/* from usertools.c */
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maederchar *get_uppercase_realm(TALLOC_CTX *memctx, const char *name);
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder
a74f814d3b445eadad6f68737a98a7a303698affChristian Maederstruct sss_names_ctx {
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder char *re_pattern;
2dfc7b04f2db681992ca04175f2beb0f127c9844Christian Maeder char *fq_fmt;
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder pcre *re;
a74f814d3b445eadad6f68737a98a7a303698affChristian Maeder};
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder/* initialize sss_names_ctx directly from arguments */
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maederint sss_names_init_from_args(TALLOC_CTX *mem_ctx,
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder const char *re_pattern,
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder const char *fq_fmt,
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder struct sss_names_ctx **out);
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder
/* initialize sss_names_ctx from domain configuration */
int sss_names_init(TALLOC_CTX *mem_ctx,
struct confdb_ctx *cdb,
const char *domain,
struct sss_names_ctx **out);
int sss_parse_name(TALLOC_CTX *memctx,
struct sss_names_ctx *snctx,
const char *orig, char **_domain, char **_name);
int sss_parse_name_const(TALLOC_CTX *memctx,
struct sss_names_ctx *snctx, const char *orig,
const char **_domain, const char **_name);
int sss_parse_name_for_domains(TALLOC_CTX *memctx,
struct sss_domain_info *domains,
const char *default_domain,
const char *orig, char **domain, char **name);
char *
sss_get_cased_name(TALLOC_CTX *mem_ctx, const char *orig_name,
bool case_sensitive);
errno_t
sss_get_cased_name_list(TALLOC_CTX *mem_ctx, const char * const *orig,
bool case_sensitive, const char ***_cased);
/* Return fully-qualified name according to the fq_fmt. The name is allocated using
* talloc on top of mem_ctx
*/
char *
sss_tc_fqname(TALLOC_CTX *mem_ctx, struct sss_names_ctx *nctx,
struct sss_domain_info *domain, const char *name);
/* Return fully-qualified name according to the fq_fmt. The name is allocated using
* talloc on top of mem_ctx. In contrast to sss_tc_fqname() sss_tc_fqname2()
* expects the domain and flat domain name as separate arguments.
*/
char *
sss_tc_fqname2(TALLOC_CTX *mem_ctx, struct sss_names_ctx *nctx,
const char *dom_name, const char *flat_dom_name,
const char *name);
/* Return fully-qualified name formatted according to the fq_fmt. The buffer in "str" is
* "size" bytes long. Returns the number of bytes written on success or a negative
* value of failure.
*
* Pass a zero size to calculate the length that would be needed by the fully-qualified
* name.
*/
int
sss_fqname(char *str, size_t size, struct sss_names_ctx *nctx,
struct sss_domain_info *domain, const char *name);
/* Subdomains use fully qualified names in the cache while primary domains use
* just the name. Return either of these for a specified domain or subdomain
*/
char *
sss_get_domain_name(TALLOC_CTX *mem_ctx, const char *orig_name,
struct sss_domain_info *dom);
/* from backup-file.c */
int backup_file(const char *src, int dbglvl);
/* check_file()
* Verify that a file has certain permissions and/or is of a certain
* file type. This function can be used to determine if a file is a
* symlink.
* Warning: use of this function implies a potential race condition
* Opening a file before or after checking it does NOT guarantee that
* it is still the same file. Additional checks should be performed
* on the caller_stat_buf to ensure that it has the same device and
* inode to minimize impact. Permission changes may have occurred,
* however.
*/
errno_t check_file(const char *filename,
uid_t uid, gid_t gid, mode_t mode, mode_t mask,
struct stat *caller_stat_buf, bool follow_symlink);
/* check_fd()
* Verify that an open file descriptor has certain permissions and/or
* is of a certain file type. This function CANNOT detect symlinks,
* as the file is already open and symlinks have been traversed. This
* is the safer way to perform file checks and should be preferred
* over check_file for nearly all situations.
*/
errno_t check_fd(int fd, uid_t uid, gid_t gid,
mode_t mode, mode_t mask,
struct stat *caller_stat_buf);
/* check_and_open_readonly()
* Utility function to open a file and verify that it has certain
* permissions and is of a certain file type. This function wraps
* check_fd(), and is considered race-condition safe.
*/
errno_t check_and_open_readonly(const char *filename, int *fd,
uid_t uid, gid_t gid,
mode_t mode, mode_t mask);
/* from util.c */
#define SSS_NO_LINKLOCAL 0x01
#define SSS_NO_LOOPBACK 0x02
#define SSS_NO_MULTICAST 0x04
#define SSS_NO_BROADCAST 0x08
#define SSS_NO_SPECIAL \
(SSS_NO_LINKLOCAL|SSS_NO_LOOPBACK|SSS_NO_MULTICAST|SSS_NO_BROADCAST)
/* These two functions accept addr in network order */
bool check_ipv4_addr(struct in_addr *addr, uint8_t check);
bool check_ipv6_addr(struct in6_addr *addr, uint8_t check);
const char * const * get_known_services(void);
errno_t sss_user_by_name_or_uid(const char *input, uid_t *_uid, gid_t *_gid);
int split_on_separator(TALLOC_CTX *mem_ctx, const char *str,
const char sep, bool trim, bool skip_empty,
char ***_list, int *size);
char **parse_args(const char *str);
errno_t sss_hash_create(TALLOC_CTX *mem_ctx,
unsigned long count,
hash_table_t **tbl);
errno_t sss_hash_create_ex(TALLOC_CTX *mem_ctx,
unsigned long count,
hash_table_t **tbl,
unsigned int directory_bits,
unsigned int segment_bits,
unsigned long min_load_factor,
unsigned long max_load_factor,
hash_delete_callback *delete_callback,
void *delete_private_data);
/**
* @brief Add two list of strings
*
* Create a new NULL-termintated list of strings by adding two lists together.
*
* @param[in] mem_ctx Talloc memory context for the new list.
* @param[in] l1 First NULL-termintated list of strings.
* @param[in] l2 Second NULL-termintated list of strings.
* @param[in] copy_strings If set to 'true' the list items will be copied
* otherwise only the pointers to the items are
* copied.
* @param[out] new_list New NULL-terminated list of strings. Must be freed
* with talloc_free() by the caller. If copy_strings
* is 'true' the new elements will be freed as well.
*/
errno_t add_strings_lists(TALLOC_CTX *mem_ctx, const char **l1, const char **l2,
bool copy_strings, char ***_new_list);
/* Copy a NULL-terminated string list
* Returns NULL on out of memory error or invalid input
*/
char **dup_string_list(TALLOC_CTX *memctx, const char **str_list);
/* Take two string lists (terminated on a NULL char*)
* and return up to three arrays of strings based on
* shared ownership.
*
* Pass NULL to any return type you don't care about
*/
errno_t diff_string_lists(TALLOC_CTX *memctx,
char **string1,
char **string2,
char ***string1_only,
char ***string2_only,
char ***both_strings);
/* Sanitize an input string (e.g. a username) for use in
* an LDAP/LDB filter
* Returns a newly-constructed string attached to mem_ctx
* It will fail only on an out of memory condition, where it
* will return ENOMEM.
*/
errno_t sss_filter_sanitize(TALLOC_CTX *mem_ctx,
const char *input,
char **sanitized);
errno_t sss_filter_sanitize_for_dom(TALLOC_CTX *mem_ctx,
const char *input,
struct sss_domain_info *dom,
char **sanitized,
char **lc_sanitized);
char *
sss_escape_ip_address(TALLOC_CTX *mem_ctx, int family, const char *addr);
/* This function only removes first and last
* character if the first character was '['.
*
* NOTE: This means, that ipv6addr must NOT be followed
* by port number.
*/
errno_t
remove_ipv6_brackets(char *ipv6addr);
errno_t add_string_to_list(TALLOC_CTX *mem_ctx, const char *string,
char ***list_p);
bool string_in_list(const char *string, char **list, bool case_sensitive);
/**
* @brief Safely zero a segment of memory,
* prevents the compiler from optimizing out
*
* @param data The address of buffer to wipe
* @param size Size of the buffer
*/
void safezero(void *data, size_t size);
int domain_to_basedn(TALLOC_CTX *memctx, const char *domain, char **basedn);
bool is_host_in_domain(const char *host, const char *domain);
/* from nscd.c */
enum nscd_db {
NSCD_DB_PASSWD,
NSCD_DB_GROUP
};
int flush_nscd_cache(enum nscd_db flush_db);
errno_t sss_nscd_parse_conf(const char *conf_path);
/* from sss_tc_utf8.c */
char *
sss_tc_utf8_str_tolower(TALLOC_CTX *mem_ctx, const char *s);
uint8_t *
sss_tc_utf8_tolower(TALLOC_CTX *mem_ctx, const uint8_t *s, size_t len, size_t *_nlen);
bool sss_string_equal(bool cs, const char *s1, const char *s2);
/* len includes terminating '\0' */
struct sized_string {
const char *str;
size_t len;
};
void to_sized_string(struct sized_string *out, const char *in);
/* from domain_info.c */
struct sss_domain_info *get_domains_head(struct sss_domain_info *domain);
struct sss_domain_info *get_next_domain(struct sss_domain_info *domain,
bool descend);
struct sss_domain_info *find_domain_by_name(struct sss_domain_info *domain,
const char *name,
bool match_any);
struct sss_domain_info *find_domain_by_sid(struct sss_domain_info *domain,
const char *sid);
struct sss_domain_info*
sss_get_domain_by_sid_ldap_fallback(struct sss_domain_info *domain,
const char* sid);
struct sss_domain_info *
find_domain_by_object_name(struct sss_domain_info *domain,
const char *object_name);
bool subdomain_enumerates(struct sss_domain_info *parent,
const char *sd_name);
struct sss_domain_info *new_subdomain(TALLOC_CTX *mem_ctx,
struct sss_domain_info *parent,
const char *name,
const char *realm,
const char *flat_name,
const char *id,
bool mpg,
bool enumerate,
const char *forest);
errno_t sssd_domain_init(TALLOC_CTX *mem_ctx,
struct confdb_ctx *cdb,
const char *domain_name,
const char *db_path,
struct sss_domain_info **_domain);
#define IS_SUBDOMAIN(dom) ((dom)->parent != NULL)
#define DOM_HAS_VIEWS(dom) ((dom)->has_views)
errno_t sss_write_domain_mappings(struct sss_domain_info *domain);
errno_t get_dom_names(TALLOC_CTX *mem_ctx,
struct sss_domain_info *start_dom,
char ***_dom_names,
int *_dom_names_count);
/* from util_lock.c */
errno_t sss_br_lock_file(int fd, size_t start, size_t len,
int num_tries, useconds_t wait);
#include "io.h"
#ifdef HAVE_PAC_RESPONDER
#define BUILD_WITH_PAC_RESPONDER true
#else
#define BUILD_WITH_PAC_RESPONDER false
#endif
/* from well_known_sids.c */
errno_t well_known_sid_to_name(const char *sid, const char **dom,
const char **name);
errno_t name_to_well_known_sid(const char *dom, const char *name,
const char **sid);
/* from string_utils.c */
char * sss_replace_space(TALLOC_CTX *mem_ctx,
const char *orig_name,
const char replace_char);
char * sss_reverse_replace_space(TALLOC_CTX *mem_ctx,
const char *orig_name,
const char replace_char);
/* from become_user.c */
errno_t become_user(uid_t uid, gid_t gid);
struct sss_creds;
errno_t switch_creds(TALLOC_CTX *mem_ctx,
uid_t uid, gid_t gid,
int num_gids, gid_t *gids,
struct sss_creds **saved_creds);
errno_t restore_creds(struct sss_creds *saved_creds);
/* from sss_semanage.c */
int set_seuser(const char *login_name, const char *seuser_name,
const char *mlsrange);
int del_seuser(const char *login_name);
#endif /* __SSSD_UTIL_H__ */