From: "mrbff (Code Review)" Attention is currently required from: flichtenheld, plaisthos. mrbff would like plaisthos and flichtenheld to review t=
his change.
push-update-server: comment about `buf_string_compare_advanc=
e()` usage in `send_single_push_updaate()`
Change-Id: Icc20f3adccb56=
89f11df8888c526d0402b32d806
Signed-off-by: Marco Baffo <marco@...2787...=
...>
---
A src/openvpn/push_util.c
1 file changed, 303 insertio=
ns(+), 0 deletions(-)git pull ssh://gerrit.openvpn.net:29418/ope=
nvpn refs/changes/76/1176/1
diff --git a/src/openvpn/push_util.c b/=
src/openvpn/push_util.c
<=
p>To view, visit change 1176. To unsubscribe, or for help writing mail filters=
, visit settings.
new file mode 100644
--- /dev/null
+++ =
b/src/openvpn/push_util.c
@@ -0,0 +1,303 @@
+#ifdef HAVE_CONFIG_H
+#endif
+
+#include "push.h"
+#include "buffer.h"
+
+#ifdef ENABLE_MANAGEMENT
+#include "multi.h"
+#endif
+
+int
+process_incomi=
ng_push_update(struct context *c, unsigned int permission_mask,
<=
span style=3D"color: hsl(120, 100%, 40%);">+ un=
signed int *option_types_found, struct buffer *buf,
+ bool msg_sen=
der)
+{
+ const uint8_t ch =
=3D buf_read_u8(buf);
+ if (ch =3D=3D ',')
+ {
+ =
if (apply_push_options(c, &c->options, buf, permission_mask, =
option_types_found, c->c2.es,
+ true))
+ {
+ switch (c->options.push_continuation)=
span>
+ {<=
br>+ case 0:
+ case 1:=
+ =
ret =3D PUSH_MSG_UPDATE;
+ break;
+
+ =
case 2:
+ =
ret =3D PUSH_MSG_CONTINUATION;
+ break;
+ }
+ }
+ else if (!msg_sender)
+ {
+ throw_signal_soft(SIGUSR1, "Offending =
option received from server");
+ }
+ }
+ else=
if (ch =3D=3D '\0')
+ {
+ =
ret =3D PUSH_MSG_UPDATE;
+ }
+
+ return ret;
+}
+
+#ifdef ENABLE_MANAGEMENT
+/**
+ *=
Return index of last `,` or `0` if it didn't find any.
+ * If there is a comma at index `0`=
it's an error anyway
+ */
+static int=
+find_first_comma_of=
_next_bundle(const char *str, int ix)
+{
+=
while (ix > 0)
+ {
+ if (=
str[ix] =3D=3D ',')
+ {
+ =
return ix;
=
+ }
+ i=
x--;
+ }
+ return 0;
+}
+
+/* Allocate memory and assemble the final message */
+static struct buffer
+forge_msg(const char *src, const char =
*continuation, struct gc_arena *gc)
+{
+ =
int src_len =3D strlen(src);
+ int con_len =3D continuation ? strlen(continuation) : 0;
+ struct buffer buf =
=3D alloc_buf_gc(src_len + sizeof(push_update_cmd) + con_len + 2, gc);
+
+ buf_printf(&buf, "%s,%s%s&qu=
ot;, push_update_cmd, src, continuation ? continuation : "");
+
+ return buf;
+}
+
+static =
char *
+gc_strdup(con=
st char *src, struct gc_arena *gc)
+{
+ =
char *ret =3D gc_malloc((strlen(src) + 1) * sizeof(char), true, gc);
+
+ strcpy(ret, src);
+ return ret;
+}
+
+/* It s=
plit the messagge (if necessay) and fill msgs with the message chunks.
+ * Return `false` on fai=
lure an `true` on success.
+ */
+static bo=
ol
+message_splitter(=
const char *s, struct buffer *msgs, struct gc_arena *gc, const int safe_cap=
)
+{
+ if (!s || !*s)
+ {
+ return false;
+ }
+
+ cha=
r *str =3D gc_strdup(s, gc);
+ int i =3D 0;
+ int im =3D 0;
+
+ while (*str=
)
+ {+ /* + ',' - '=
/0' */
+ i=
f (strlen(str) > safe_cap)
+ {
+=
int ci =3D find_first_comma_of_next_bundle(str, safe_cap);
+ if (!ci)
+ {
+ /* if no comm=
as were found go to fail, do not send any message */
+ return false;
<=
span style=3D"color: hsl(120, 100%, 40%);">+ }
+ str[ci] =3D '\0';=
+ /* copy=
from i to (ci -1) */
+ msgs[im] =3D forge_msg(str, ",push-continuation 2",=
gc);
+ i =
=3D ci + 1;
+ =
}
+ else
+ {
+ if (im)
+ {
+ msgs[im] =3D forge_msg(st=
r, ",push-continuation 1", gc);
+ }
+ else
+ {
+ msgs[im] =3D forge_msg(str, NULL, gc);
+ i =3D strlen(str);+ }
+ str =3D &str[i];
+ im++;
+ }
+ return true;
+}
+
+/* send the me=
ssage(s) prepared to one single client */
+static bool
+send_single_push_update(struct context *c, struct buffer *msg=
s, unsigned int *option_types_found)
+{
+ =
if (!msgs[0].data || !*(msgs[0].data))
+ {
+ return false;
+ }
+ =
int i =3D -1;
+
+ while (msgs[++i].data=
&& *(msgs[i].data))
+ {
+ =
if (!send_control_channel_string(c, BSTR(&msgs[i]), D_PUSH))<=
br>+ {
+ return false;
<=
span style=3D"color: hsl(120, 100%, 40%);">+ }
+
+ /* After sending the control message, we update the =
options server-side in the client's context
+ * so pushed options like ifconfig/ifco=
nfig-ipv6 can actually work.
+ * If we don't do that, the packets arriving from the =
client with the new address will be
+ * rejected because the value in the option is an o=
ld one.
+ * F=
or the same reason we later update the vhash too in `send_push_update()` fu=
nction.
+ * =
Using `buf_string_compare_advance()` we mimic the behavior inside `process_=
incoming_push_msg()`.
+ * However, we don't need to check the return value here beca=
use we just want to `advance`,
+ * meaning we skip the `push_update_cmd' we added ea=
rlier. */
+ bu=
f_string_compare_advance(&msgs[i], push_update_cmd);
+ if (process_incoming_push_upda=
te(c, pull_permission_mask(c), option_types_found, &msgs[i], true) =3D=
=3D PUSH_MSG_ERROR)
+=
{
+ =
msg(M_WARN, "Failed to process push update message sent to client ID=
: %u",
+ =
c->c2.tls_multi ? c->c2.tls_multi->peer_id : UINT32_MAX);<=
/span>
+ continue=
;
+ }+ c->options.push_o=
ption_types_found |=3D *option_types_found;
+ if (!options_postprocess_pull(&c->op=
tions, c->c2.es))
=
+ {
+ =
msg(M_WARN, "Failed to post-process push update message sent to cli=
ent ID: %u",
+ =
c->c2.tls_multi ? c->c2.tls_multi->peer_id : UINT32_=
MAX);
+ }
+ }
+ return true;
+}
+
+int=
+send_push_update(st=
ruct multi_context *m, const void *target, const char *msg, const push_upda=
te_type type, const int push_bundle_size)
+{
+ if (!msg || !*msg || !m
+ || (!target && type !=3D UPT_BROADCAST))=
+ {
+ return -EINVAL;
+ }
+
+ struct gc_arena gc =3D gc_new();
+ /* extra space for possible trailing ifconfig an=
d push-continuation */
+ const int extra =3D 84 + sizeof(push_update_cmd);
+ /* push_bundle_size is the maximum=
size of a message, so if the message
+ * we want to send exceeds that size we have to split=
it into smaller messages */
+ const int safe_cap =3D push_bundle_size - extra;
+ struct buffer *msgs =3D gc_malloc((msgs_n=
um + 1) * sizeof(struct buffer), true, &gc);
+
+ unsigned int option_types_found =3D 0;
+
+ msgs[msgs_num].data =3D NULL;
+ if (!message_splitter(msg, msgs, &gc, =
safe_cap))
+ {
+ gc_free(&gc=
);
+ return -E=
INVAL;
+ }<=
br>+
+ if (type =3D=3D UPT_BY_CID)
+ struct multi_instance *mi =3D lookup_by_=
cid(m, *((unsigned long *)target));
+
+ =
if (!mi)
+ =
{
+ retu=
rn -ENOENT;
+ =
}
+
+ const char *old_ip =3D mi->=
;context.options.ifconfig_local;
+ const char *old_ipv6 =3D mi->context.options.ifconf=
ig_ipv6_local;
+ =
if (!mi->halt
+=
&& send_single_push_update(&mi->context, msgs, &=
amp;option_types_found))
+ {
+ =
if (option_types_found & OPT_P_UP)
+ {
+ update_vhash(m, mi, old_ip, old_ipv6);<=
/span>
+ }=
+ gc_free(&g=
c);
+ retu=
rn 1;
+ }
+ else
<=
span style=3D"color: hsl(120, 100%, 40%);">+ {
+ gc_free(&gc);
<=
span style=3D"color: hsl(120, 100%, 40%);">+ return 0;
+ }
+ }
+
+ int count =3D 0;
+ struct hash_iterator hi;
+ const struct hash_element *he;
+
+ hash_iterator_init(m->iter, &hi);
+ while ((he =3D hash_iterator_next(&am=
p;hi)))
+ {=
+ struct multi_insta=
nce *curr_mi =3D he->value;
+
+ =
if (curr_mi->halt)
+ {
+ =
continue;
+ =
}
+
+ /* Type is UPT_BROADCAST so=
we update every client */
+ option_types_found =3D 0;
+ const char *old_ip =3D curr_mi->context.opt=
ions.ifconfig_local;
=
+ const char *old_ipv6 =3D curr_mi->context.options.ifconfig_ipv6=
_local;
+ if (=
!send_single_push_update(&curr_mi->context, msgs, &option_types_=
found))
+ {
+ msg(M_CLIE=
NT, "ERROR: Peer ID: %u has not been updated",
+ curr_mi->context.c2=
.tls_multi ? curr_mi->context.c2.tls_multi->peer_id : UINT32_MAX);
+ continue;<=
/span>
+ }
=
+ if (option_types_found=
& OPT_P_UP)
+ =
{
+ u=
pdate_vhash(m, curr_mi, old_ip, old_ipv6);
+ }
+ count++;
+ }
+
+ hash_iterator_free(=
&hi);
+ gc_fre=
e(&gc);
+ retu=
rn count;
+}+
+#define RETURN_UPDATE_STATUS(n_sent) =
\
+ do =
\
+ { =
\
+ if ((n_sent) > 0) =
\
+ { =
\
+ =
msg(M_CLIENT, "SUCCESS: %d client(s) updated", (n_sent)); \
+ return tru=
e; \
+ } =
\
+ else =
\
+ { =
\
+ msg(M_CLIENT, "ERROR: =
no client updated"); \
+ return false; =
\
+=
} \
+ } while (0)<=
br>+
+
+bool
+manag=
ement_callback_send_push_update_broadcast(void *arg, const char *options)=
span>
+{
+ int n_sent =3D send_push_update(arg=
, NULL, options, UPT_BROADCAST, PUSH_BUNDLE_SIZE);
+
+ RETURN_UPDATE_STATUS(n_sent);
+}
+
+bool=
+management_callback_send_p=
ush_update_by_cid(void *arg, unsigned long cid, const char *options)=
+{
+ int n_sent =3D send_push_update(arg, &am=
p;cid, options, UPT_BY_CID, PUSH_BUNDLE_SIZE);
+
+ RETURN_UPDATE_STATUS(n_sent);
+}
+#endif /* ifdef ENABLE_MANAGEMENT */