The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
sync_touch.c
Go to the documentation of this file.
1 /*
2  * This program is is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or (at
5  * your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 
17 /**
18  * @file src/modules/proto_ldap_sync/sync_touch.c
19  *
20  * @brief Touch entries, causing them to be re-processed by the proto_ldap_sync module.
21  *
22  * @author Arran Cudbard-Bell
23  *
24  * @copyright 2017 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
25  */
26 RCSID("$Id: c3215168ca67607be9913eb082d077197ff06402 $")
27 
29 
30 #include <freeradius-devel/ldap/base.h>
31 #include <freeradius-devel/util/debug.h>
32 
33 typedef struct {
34  uint64_t id; //!< Bitfield ID.
35  bool master; //!< Server is a master.
36  char const *uri;
37  bool start_tls; //!< Whether we should use the StartTLS extension.
38 } sync_host_t;
39 
40 
41 typedef struct {
42  char const *bind_dn;
43  char const *bind_pw;
45 
46 int main(int argc, char **argv)
47 {
48  int c;
50  int ret;
51  int sockfd;
52 
53  conf = talloc_zero(NULL, sync_touch_conf_t);
54  conf->proto = IPPROTO_UDP;
55  conf->dict_dir = DICTDIR;
56  conf->raddb_dir = RADDBDIR;
57  conf->secret = talloc_strdup(conf, "testing123");
58  conf->timeout = fr_time_delta_from_sec(3);
59  conf->retries = 5;
60 
61 #ifndef NDEBUG
62  if (fr_fault_setup(autofree, getenv("PANIC_ACTION"), argv[0]) < 0) {
63  fr_perror("sync_touch");
64  fr_exit_now(EXIT_FAILURE);
65  }
66 #endif
67 
68  talloc_set_log_stderr();
69 
70  while ((c = getopt(argc, argv, "46c:d:D:f:Fhi:l:n:p:qr:sS:t:vx")) != -1) switch (c) {
71  case 'S':
72  {
73  char *p;
74  fp = fopen(optarg, "r");
75  if (!fp) {
76  ERROR("Error opening %s: %s", optarg, fr_syserror(errno));
77  fr_exit_now(EXIT_FAILURE);
78  }
79  if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
80  ERROR("Error reading %s: %s", optarg, fr_syserror(errno));
81  fr_exit_now(EXIT_FAILURE);
82  }
83  fclose(fp);
84 
85  /* truncate newline */
86  p = filesecret + strlen(filesecret) - 1;
87  while ((p >= filesecret) &&
88  (*p < ' ')) {
89  *p = '\0';
90  --p;
91  }
92 
93  if (strlen(filesecret) < 2) {
94  ERROR("Secret in %s is too short", optarg);
95  fr_exit_now(EXIT_FAILURE);
96  }
97  talloc_free(conf->secret);
98  conf->secret = talloc_strdup(conf, filesecret);
99  }
100  break;
101 
102  case 't':
103  if (fr_time_delta_from_str(&conf->timeout, optarg, strlen(optarg), FR_TIME_RES_SEC) < 0) {
104  PERROR("Failed parsing timeout value");
105  fr_exit_now(EXIT_FAILURE);
106  }
107  break;
108 
109  case 'v':
110  DEBUG("%s", sync_touch_version);
111  fr_exit_now(0);
112 
113  case 'x':
114  fr_debug_lvl++;
115  break;
116 
117  case 'h':
118  default:
119  usage();
120  }
121  argc -= (optind - 1);
122  argv += (optind - 1);
123 
124  if ((argc < 2) || ((conf->secret == NULL) && (argc < 3))) {
125  ERROR("Insufficient arguments");
126  usage();
127  }
128  /*
129  * Mismatch between the binary and the libraries it depends on
130  */
132  fr_perror("sync_touch");
133  fr_exit_now(EXIT_FAILURE);
134  }
135 
136  if (!fr_dict_global_ctx_init(NULL, true, dict_dir)) {
137  fr_perror("sync_touch");
138  fr_exit_now(EXIT_FAILURE);
139  }
140 
141  if (fr_dict_internal_afrom_file(&conf->dict, FR_DICTIONARY_FILE, __FILE__) < 0) {
142  fr_perror("sync_touch");
143  fr_exit_now(EXIT_FAILURE);
144  }
145 
146  if (fr_dict_read(dict_freeradius, conf->raddb_dir, FR_DICTIONARY_FILE) == -1) {
147  fr_perror("sync_touch");
148  fr_exit_now(EXIT_FAILURE);
149  }
150  fr_strerror_clear(); /* Clear the error buffer */
151 
152  fr_set_signal(SIGPIPE, rs_signal_stop);
153  fr_set_signal(SIGINT, rs_signal_stop);
154  fr_set_signal(SIGTERM, rs_signal_stop);
155 #ifdef SIGQUIT
156  fr_set_signal(SIGQUIT, rs_signal_stop);
157 #endif
158 
159  DEBUG("%s - Starting pass_persist read loop", sync_touch_version);
160  ret = sync_touch_send_recv(conf, sockfd);
161  DEBUG("Read loop done");
162 
163 finish:
164  /*
165  * Everything should be parented from conf
166  */
167  talloc_free(conf);
168 
169  return ret;
170 }
#define USES_APPLE_DEPRECATED_API
Definition: build.h:468
#define RCSID(id)
Definition: build.h:481
int fr_fault_setup(TALLOC_CTX *ctx, char const *cmd, char const *program)
Registers signal handlers to execute panic_action on fatal signal.
Definition: debug.c:1242
#define fr_exit_now(_x)
Exit without calling atexit() handlers, producing a log message in debug builds.
Definition: debug.h:234
#define ERROR(fmt,...)
Definition: dhcpclient.c:41
static fr_dict_t const * dict_freeradius
Definition: dhcpclient.c:79
static int sockfd
Definition: dhcpclient.c:56
#define DEBUG(fmt,...)
Definition: dhcpclient.c:39
static NEVER_RETURNS void usage(void)
Definition: dhcpclient.c:114
int fr_dict_internal_afrom_file(fr_dict_t **out, char const *internal_name, char const *dependent)
(Re-)Initialize the special internal dictionary
int fr_dict_read(fr_dict_t *dict, char const *dict_dir, char const *filename)
Read supplementary attribute definitions into an existing dictionary.
fr_dict_gctx_t * fr_dict_global_ctx_init(TALLOC_CTX *ctx, bool free_at_exit, char const *dict_dir)
Initialise the global protocol hashes.
Definition: dict_util.c:4392
#define PERROR(_fmt,...)
Definition: log.h:228
talloc_free(reap)
int fr_debug_lvl
Definition: log.c:43
int fr_set_signal(int sig, sig_t func)
Sets a signal handler using sigaction if available, else signal.
Definition: misc.c:47
static TALLOC_CTX * autofree
Definition: radclient-ng.c:107
static rs_t * conf
Definition: radsniff.c:53
static void rs_signal_stop(UNUSED int sig)
Definition: radsnmp.c:154
char const * bind_pw
Definition: sync_touch.c:43
int main(int argc, char **argv)
Definition: sync_touch.c:46
bool master
Server is a master.
Definition: sync_touch.c:35
char const * uri
Definition: sync_touch.c:36
bool start_tls
Whether we should use the StartTLS extension.
Definition: sync_touch.c:37
uint64_t id
Bitfield ID.
Definition: sync_touch.c:34
char const * bind_dn
Definition: sync_touch.c:42
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition: syserror.c:243
fr_slen_t fr_time_delta_from_str(fr_time_delta_t *out, char const *in, size_t inlen, fr_time_res_t hint)
Create fr_time_delta_t from a string.
Definition: time.c:445
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition: time.h:590
@ FR_TIME_RES_SEC
Definition: time.h:50
#define FR_DICTIONARY_FILE
Definition: conf.h:7
void fr_perror(char const *fmt,...)
Print the current error to stderr with a prefix.
Definition: strerror.c:733
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
Definition: strerror.c:577
int fr_check_lib_magic(uint64_t magic)
Check if the application linking to the library has the correct magic number.
Definition: version.c:40
#define RADIUSD_MAGIC_NUMBER
Definition: version.h:81