client-connect: Add CC_RET_DEFERRED and cope with deferred client-connect
PATCH v7 1/6 client-connect: Add CC_RET_DEFERRED and cope with deferred client-connect
This patch moves the state, that was previously tracked within the multi_connection_established() function, into struct client_connect_state. The multi_connection_established() function can now be exited and re-entered as many times as necessary - without losing the client-connect handling state.
The patch also adds the new return value CC_RET_DEFERRED which indicates that the handler couldn't complete immediately, and needs to be called later. At that point multi_connection_established() will exit without indicating completion.
Each client-connect handler now has an (optional) additional call-back: The call-back for handling the deferred case. If the main call-back returns CC_RET_DEFERRED, the next call to the handler will be through the deferred call-back.
Patch V3: Use a static struct in multi_instance instead of using
malloc/free and use two states (deffered with and without result) instead of one to eliminate the counter that was only tested for > 0.
Patch V5: Use new states in context_auth instead of the extra state
that the patch series previously used.
Patch V6: Restructure code to make it a bit more readable, rebase on
master.
Patch V7: move defferred bool into client connect handler calls, switch
to switch case
--- src/openvpn/multi.c | 157 +++++++++++++++++++++++++++++------------- src/openvpn/multi.h | 15 +++- src/openvpn/openvpn.h | 9 +++ 3 files changed, 132 insertions(+), 49 deletions(-)
src/openvpn/multi.c +109 -48
@@ -1713,8 +1713,11 @@ multi_client_connect_post_plugin(struct multi_context *m,enum client_connect_return multi_client_connect_mda (struct multi_context *m,struct multi_instance *mi, +bool deferred,unsigned int *option_types_found) { +/* We never return CC_RET_DEFERRED */ +ASSERT (!deferred);enum client_connect_return ret =CC_RET_SKIPPED ;#ifdef MANAGEMENT_DEF_AUTH if (mi->cc_config)
@@ -1854,8 +1857,13 @@ multi_client_set_protocol_options(struct context *c)static enum client_connect_return multi_client_connect_call_plugin_v1 (struct multi_context *m,struct multi_instance *mi, +bool deferred,unsigned int *option_types_found) { +if (deferred) + { +return CC_RET_FAILED ; + }enum client_connect_return ret =CC_RET_SKIPPED ;#ifdef ENABLE_PLUGIN ASSERT (m);
@@ -1907,8 +1915,13 @@ cleanup:static enum client_connect_return multi_client_connect_call_plugin_v2 (struct multi_context *m,struct multi_instance *mi, +bool deferred,unsigned int *option_types_found) { +if (deferred) + { +return CC_RET_FAILED ; + }enum client_connect_return ret =CC_RET_SKIPPED ;#ifdef ENABLE_PLUGIN ASSERT (m);
@@ -1949,8 +1962,13 @@ multi_client_connect_call_plugin_v2(struct multi_context *m,static enum client_connect_return multi_client_connect_call_script (struct multi_context *m,struct multi_instance *mi, +bool deferred,unsigned int *option_types_found) { +if (deferred) + { +return CC_RET_FAILED ; + }ASSERT (m);ASSERT (mi);
@@ -2173,8 +2191,12 @@ multi_client_connect_early_setup(struct multi_context *m,static enum client_connect_return multi_client_connect_source_ccd (struct multi_context *m,struct multi_instance *mi, +bool deferred,unsigned int *option_types_found) { +/* Since we never return a CC_RET_DEFERRED, this indicates a serious +* problem */ +ASSERT (!deferred);enum client_connect_return ret =CC_RET_SKIPPED ;if (mi->context.options.client_config_dir) {
@@ -2225,32 +2247,18 @@ multi_client_connect_source_ccd(struct multi_context *m,return ret; } -static inline bool -cc_check_return (int *cc_succeeded_count, -enum client_connect_return ret) -{ -if (ret ==CC_RET_SUCCEEDED ) - { - (*cc_succeeded_count)++; -return true; - } -else if (ret ==CC_RET_FAILED ) - { -return false; - } -else if (ret ==CC_RET_SKIPPED ) - { -return true; - } -else - { -ASSERT (0 ); - } -} -typedef enum client_connect_return (*multi_client_connect_handler ) (struct multi_context *m,struct multi_instance *mi, -unsigned int *option_types_found); +bool from_deferred,unsigned int *option_types_found); + +static const multi_client_connect_handler client_connect_handlers[] = { + multi_client_connect_source_ccd, + multi_client_connect_call_plugin_v1, + multi_client_connect_call_plugin_v2, + multi_client_connect_call_script, + multi_client_connect_mda, +NULL , +};/* * Called as soon as the SSL/TLS connection is authenticated.
@@ -2280,27 +2288,74 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)return ; } -multi_client_connect_handler handlers[] = { - multi_client_connect_source_ccd, - multi_client_connect_call_plugin_v1, - multi_client_connect_call_plugin_v2, - multi_client_connect_call_script, - multi_client_connect_mda, -NULL - }; +/* We are only called for the CAS_PENDING_x states, so we +* can ignore other states here */ +bool from_deferred = (mi->context.c2.context_auth !=CAS_PENDING ); -unsigned int option_types_found =0 ; +int *cur_handler_index = &mi->client_connect_defer_state.cur_handler_index; +unsigned int *option_types_found = + &mi->client_connect_defer_state.option_types_found; -int cc_succeeded = true;/* client connect script status */ -int cc_succeeded_count =0 ; -enum client_connect_return ret; +/* We are called for the first time */ +if (!from_deferred) + { + *cur_handler_index =0 ; + *option_types_found =0 ; +/* Initially we have no handler that has returned a result */ + mi->context.c2.context_auth =CAS_PENDING_DEFERRED ; -multi_client_connect_early_setup (m, mi); +multi_client_connect_early_setup (m, mi); + } -for (int i =0 ; cc_succeeded && handlers[i]; i++) +bool cc_succeeded = true; + +while (cc_succeeded + && client_connect_handlers[*cur_handler_index] !=NULL ) { - ret = handlers[i](m, mi, &option_types_found); - cc_succeeded =cc_check_return (&cc_succeeded_count, ret); +enum client_connect_return ret; + ret = client_connect_handlers[*cur_handler_index](m, mi, from_deferred, + option_types_found); + + from_deferred = false; + +switch (ret) + { +case CC_RET_SUCCEEDED : +/* +* Remember that we already had at least one handler +* returning a result should go to into deferred state +*/ + mi->context.c2.context_auth =CAS_PENDING_DEFERRED_PARTIAL ; +break ; + +case CC_RET_SKIPPED : +/* +* Move on with the next handler without modifying any +* other state +*/ +break ; + +case CC_RET_DEFERRED : +/* +* we already set client_connect_status to DEFERRED_RESULT or +* DEFERRED_NO_RESULT. We just return +* from the function as having client_connect_status +*/ +return ; + +case CC_RET_FAILED : +/* +* One handler failed. We abort the chain and set the final +* result to failed +*/ + cc_succeeded = false; +break ; + +default : +ASSERT (0 ); + } + + (*cur_handler_index)++; }/*
@@ -2312,18 +2367,24 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)msg (D_MULTI_ERRORS ,"MULTI: client has been rejected due to " "'disable' directive" ); cc_succeeded = false; - cc_succeeded_count =0 ; }if (cc_succeeded) { -multi_client_connect_late_setup (m, mi, option_types_found); +multi_client_connect_late_setup (m, mi, *option_types_found); }else { -/* set context-level authentication flag */ - mi->context.c2.context_auth = - cc_succeeded_count ?CAS_PARTIAL :CAS_FAILED ; +/* set context-level authentication flag to failed but remember +* if had a handler succeed (for cleanup) */ +if (mi->context.c2.context_auth ==CAS_PENDING_DEFERRED_PARTIAL ) + { + mi->context.c2.context_auth =CAS_PARTIAL ; + } +else + { + mi->context.c2.context_auth =CAS_FAILED ; + } }/* increment number of current authenticated clients */
@@ -2624,7 +2685,7 @@ multi_process_post(struct multi_context *m, struct multi_instance *mi, const uns {/* connection is "established" when SSL/TLS key negotiation succeeds * and (if specified) auth user/pass succeeds */ -if (mi->context.c2.context_auth ==CAS_PENDING +if (is_cas_pending (mi->context.c2.context_auth) &&CONNECTION_ESTABLISHED (&mi->context)) {multi_connection_established (m, mi);
@@ -3579,7 +3640,7 @@ management_client_auth(void *arg, {if (auth) { -if (mi->context.c2.context_auth ==CAS_PENDING ) +if (is_cas_pending (mi->context.c2.context_auth)) {set_cc_config (mi, cc_config); cc_config_owned = false;
@@ -3591,7 +3652,7 @@ management_client_auth(void *arg, {msg (D_MULTI_LOW ,"MULTI: connection rejected: %s, CLI:%s" , reason,np (client_reason)); } -if (mi->context.c2.context_auth !=CAS_PENDING ) +if (!is_cas_pending (mi->context.c2.context_auth)) {send_auth_failed (&mi->context, client_reason);/* mid-session reauth failed */ multi_schedule_context_wakeup (m, mi);
src/openvpn/multi.h +14 -1
@@ -62,6 +62,18 @@ struct deferred_signal_schedule_entrystruct timeval wakeup; }; +/** +* Detached client connection state. This is the state that is tracked while +* the client connect hooks are executed. +*/ +struct client_connect_defer_state +{ +/* Index of currently executed handler. */ +int cur_handler_index; +/* Remember which option classes where processed for delayed option +* handling. */ +unsigned int option_types_found; +};/** * Server-mode state structure for one single VPN tunnel.
@@ -108,7 +120,7 @@ struct multi_instance {struct context context;/**< The context structure storing state * for this VPN tunnel. */ - +struct client_connect_defer_state client_connect_defer_state;#ifdef ENABLE_ASYNC_PUSH int inotify_watch;/* watch descriptor for acf */ #endif
@@ -195,6 +207,7 @@ enum client_connect_return {CC_RET_FAILED ,CC_RET_SUCCEEDED , +CC_RET_DEFERRED , CC_RET_SKIPPED };
src/openvpn/openvpn.h +9 -0
@@ -217,6 +217,8 @@ struct context_1enum client_connect_status {CAS_SUCCEEDED =0 ,CAS_PENDING , +CAS_PENDING_DEFERRED , +CAS_PENDING_DEFERRED_PARTIAL ,/**< at least handler succeeded, no result yet*/ CAS_FAILED ,CAS_PARTIAL ,/**< Variant of CAS_FAILED: at least one * client-connect script/plugin succeeded
@@ -225,6 +227,13 @@ enum client_connect_status {*/ }; +static inline bool +is_cas_pending (enum client_connect_status cas) +{ +return cas ==CAS_PENDING || cas ==CAS_PENDING_DEFERRED + || cas ==CAS_PENDING_DEFERRED_PARTIAL ; +} +/** * Level 2 %context containing state that is reset on both \c SIGHUP and * \c SIGUSR1 restarts.
Signature · 1 line
2.26.2
PATCH v7 2/6 client-connect: Add deferred support to the client-connect script handler
From: Fabian Knittel <[email protected]...>
This patch introduces the concept of a return value file for the client-connect handlers. (This is very similar to the auth value file used during deferred authentication.) The file name is stored in the client_connect_state struct.
In addition, the patch also allows the storage of the client config file name in struct client_connect_state.
Both changes are used by the client-connect script handler to support deferred client-connection handling. The deferred return value file (deferred_ret_file) is passed to the actual script via the environment. If the script succeeds and writes the value for deferral into the deferred_ret_file, the handler knows to indicate deferral. Later on, the deferred handler checks whether the value of the deferred_ret_file has been updated to success or failure.
--- src/openvpn/multi.c | 226 +++++++++++++++++++++++++++++++++++++++++--- src/openvpn/multi.h | 12 +++ 2 files changed, 225 insertions(+), 13 deletions(-)
src/openvpn/multi.c +213 -13
@@ -1854,6 +1854,168 @@ multi_client_set_protocol_options(struct context *c) } } +/** +* Delete the temporary file for the return value of client connect +* It also removes it from it from client_connect_defer_state and +* environment +*/ +static void +ccs_delete_deferred_ret_file (struct multi_instance *mi) +{ +struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state); +if (ccs->deferred_ret_file) + { +setenv_del (mi->context.c2.es,"client_connect_deferred_file" ); +if (!platform_unlink (ccs->deferred_ret_file)) + { +msg (D_MULTI_ERRORS ,"MULTI: problem deleting temporary file: %s" , + ccs->deferred_ret_file); + } +free (ccs->deferred_ret_file); + ccs->deferred_ret_file =NULL ; + } +} + +/** +* Create a temporary file for the return value of client connect +* and puts it into the client_connect_defer_state and environment +* as "client_connect_deferred_file" +* +* @return boolean value if creation was successfull +*/ +static bool +ccs_gen_deferred_ret_file (struct multi_instance *mi) +{ +struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state); +struct gc_arena gc =gc_new (); +const char *fn; + +if (ccs->deferred_ret_file) + { +ccs_delete_deferred_ret_file (mi); + } + + fn =platform_create_temp_file (mi->context.options.tmp_dir,"ccr" , &gc); +if (!fn) + { +gc_free (&gc); +return false; + } + ccs->deferred_ret_file =string_alloc (fn,NULL ); + +setenv_str (mi->context.c2.es,"client_connect_deferred_file" , + ccs->deferred_ret_file); + +gc_free (&gc); +return true; +} + +/** +* Tests whether the deferred return value file exists and returns the +* contained return value. +* +* @return CC_RET_SKIPPED if the file does not exist or is empty. +* CC_RET_DEFERRED, CC_RET_SUCCEEDED or CC_RET_FAILED depending on +* the value stored in the file. +*/ +static enum client_connect_return +ccs_test_deferred_ret_file (struct multi_instance *mi) +{ +struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state); +enum client_connect_return ret =CC_RET_SKIPPED ; +FILE *fp =fopen (ccs->deferred_ret_file,"r" ); +if (fp) + { +const int c =fgetc (fp); +switch (c) + { +case '0' : + ret =CC_RET_FAILED ; +break ; + +case '1' : + ret =CC_RET_SUCCEEDED ; +break ; + +case '2' : + ret =CC_RET_DEFERRED ; +break ; + +case EOF : +if (feof (fp)) + { + ret =CC_RET_SKIPPED ; +break ; + } + +/* Not EOF, but other error fall through to error state */ +default : +/* We received an unknown/unexpected value. Assume failure. */ +msg (M_WARN ,"WARNING: Unknown/unexcepted value in deferred" +"client-connect resultfile" ); + ret =CC_RET_FAILED ; + } +fclose (fp); + } +return ret; +} + +/** +* Deletes the temporary file for the config directives of the client connect +* script and removes it into the client_connect_defer_state and environment +* +*/ +static void +ccs_delete_config_file (struct multi_instance *mi) +{ +struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state); +if (ccs->config_file) + { +setenv_del (mi->context.c2.es,"client_connect_config_file" ); +if (!platform_unlink (ccs->config_file)) + { +msg (D_MULTI_ERRORS ,"MULTI: problem deleting temporary file: %s" , + ccs->config_file); + } +free (ccs->config_file); + ccs->config_file =NULL ; + } +} + +/** +* Create a temporary file for the config directives of the client connect +* script and puts it into the client_connect_defer_state and environment +* as "client_connect_config_file" +* +* @return boolean value if creation was successfull +*/ +static bool +ccs_gen_config_file (struct multi_instance *mi) +{ +struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state); +struct gc_arena gc =gc_new (); +const char *fn; + +if (ccs->config_file) + { +ccs_delete_config_file (mi); + } + + fn =platform_create_temp_file (mi->context.options.tmp_dir,"cc" , &gc); +if (!fn) + { +gc_free (&gc); +return false; + } + ccs->config_file =string_alloc (fn,NULL ); + +setenv_str (mi->context.c2.es,"client_connect_config_file" , + ccs->config_file); + +gc_free (&gc); +return true; +} + static enum client_connect_return multi_client_connect_call_plugin_v1(struct multi_context *m, struct multi_instance *mi,
@@ -1954,7 +2116,39 @@ multi_client_connect_call_plugin_v2(struct multi_context *m,return ret; } +static enum client_connect_return +multi_client_connect_script_deferred (struct multi_context *m, +struct multi_instance *mi, +unsigned int *option_types_found) +{ +ASSERT (mi); +ASSERT (option_types_found); +struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state); +enum client_connect_return ret =CC_RET_SKIPPED ; + + ret =ccs_test_deferred_ret_file (mi); +if (ret ==CC_RET_SKIPPED ) + { +/* +* Skipped and deferred are equivalent in this context. +* skipped means that the called program has not yet +* written a return status implicitly needing more time +* while deferred is the explicit notification that it +* needs more time +*/ + ret =CC_RET_DEFERRED ; + } + +if (ret !=CC_RET_DEFERRED ) + { +ccs_delete_deferred_ret_file (mi); +multi_client_connect_post (m, mi, ccs->config_file, + option_types_found); +ccs_delete_config_file (mi); + } +return ret; +}/** * Runs the --client-connect script if one is defined.
@@ -1967,48 +2161,54 @@ multi_client_connect_call_script(struct multi_context *m, {if (deferred) { -return CC_RET_FAILED ; +return multi_client_connect_script_deferred (m, mi, option_types_found); }ASSERT (m);ASSERT (mi);enum client_connect_return ret =CC_RET_SKIPPED ; +struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);if (mi->context.options.client_connect_script) {struct argv argv =argv_new ();struct gc_arena gc =gc_new (); -const char *dc_file =NULL ;setenv_str (mi->context.c2.es,"script_type" ,"client-connect" ); - dc_file =platform_create_temp_file (mi->context.options.tmp_dir, -"cc" , &gc); -if (!dc_file) +if (!ccs_gen_config_file (mi) + || !ccs_gen_deferred_ret_file (mi)) { ret =CC_RET_FAILED ; goto cleanup; }argv_parse_cmd (&argv, mi->context.options.client_connect_script); -argv_printf_cat (&argv,"%s" , dc_file); +argv_printf_cat (&argv,"%s" , ccs->config_file);if (openvpn_run_script (&argv, mi->context.c2.es,0 ,"--client-connect" )) { -multi_client_connect_post (m, mi, dc_file, option_types_found); - ret =CC_RET_SUCCEEDED ; +if (ccs_test_deferred_ret_file (mi) ==CC_RET_DEFERRED ) + { + ret =CC_RET_DEFERRED ; + } +else + { +multi_client_connect_post (m, mi, ccs->config_file, + option_types_found); + ret =CC_RET_SUCCEEDED ; + } }else { ret =CC_RET_FAILED ; } - -if (!platform_unlink (dc_file)) +cleanup: +if (ret !=CC_RET_DEFERRED ) { -msg (D_MULTI_ERRORS ,"MULTI: problem deleting temporary file: %s" , - dc_file); +ccs_delete_config_file (mi); +ccs_delete_deferred_ret_file (mi); } -cleanup:argv_free (&argv);gc_free (&gc); }
src/openvpn/multi.h +12 -0
@@ -73,6 +73,18 @@ struct client_connect_defer_state/* Remember which option classes where processed for delayed option * handling. */ unsigned int option_types_found; + +/** +* The temporrary file name that contains the return status of the +* client-connect script if it exits with defer as status +*/ +char *deferred_ret_file; + +/** +* The temporary file name that contains the config directives +* returned by the client-connect script +*/ +char *config_file; };/**
Signature · 1 line
2.26.2
Re: [Openvpn-devel] [PATCH v7 2/6] client-connect: Add deferred support to the client-connect script handler
Hi,
Quoted 5 lines · Arne Schwabe
From: Fabian Knittel <[email protected]...>
This patch introduces the concept of a return value file for the client-connect handlers. (This is very similar to the auth value file used during deferred authentication.) The file name is stored in the
client_connect_statestruct.
[..]
The test rig found no explosions :-)
start client jobs...
22...
Test sets succeeded: 1 2 3 4 6 8.
Test sets failed: none.
23.small...
Test sets succeeded: 1 2 3 4.
Test sets failed: none.
23...
Test sets succeeded: 1 1a 1b 1d 2 2a 2b 2c 2d 3 4 5 6 8 8a 9.
Test sets failed: none.
24...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 4a 5 6 8 8a 9. Test sets failed: none.
master...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 5 5x 6 7 7x 8 8a 9 2f 4b. Test sets failed: none.
... but the new code is not excercised yet, of course.
Will see if I have something ready with async-ccs when Antonio has stared-at-code this :-)
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Re: [Openvpn-devel] [PATCH v7 2/6] client-connect: Add deferred support to the client-connect script handler
In reply to Arne SchwabeQuoted 41 lines · Arne Schwabe
From: Fabian Knittel <[email protected]...>
This patch introduces the concept of a return value file for the client-connect handlers. (This is very similar to the auth value file used during deferred authentication.) The file name is stored in the
client_connect_statestruct.In addition, the patch also allows the storage of the client config file name in struct
client_connect_state.Both changes are used by the client-connect script handler to support deferred client-connection handling. The deferred return value file (
deferred_ret_file) is passed to the actual script via the environment. If the script succeeds and writes the value for deferral into thedeferred_ret_file, the handler knows to indicate deferral. Later on, the deferred handler checks whether the value of thedeferred_ret_filehas been updated to success or failure.Signed-off-by Fabian Knittel <[email protected]...>Signed-off-by Arne Schwabe <[email protected]...>--- src/openvpn/multi.c | 226 +++++++++++++++++++++++++++++++++++++++++--- src/openvpn/multi.h | 12 +++ 2 files changed, 225 insertions(+), 13 deletions(-)src/openvpn/multi.c +10
-0@@ -1854,6 +1854,168 @@ multi_client_set_protocol_options(struct context *c) } } +/** +* Delete the temporary file for the return value of client connect +* It also removes it from it from client_connect_defer_state and +* environment +*/ +static void +ccs_delete_deferred_ret_file (struct multi_instance *mi) +{ +struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state); +if (ccs->deferred_ret_file)
src/openvpn/multi.c multi_client_set_protocol_options(struct context *c) · line 1876
Quoted 10 lines
src/openvpn/multi.c +10
-0+ { +setenv_del (mi->context.c2.es,"client_connect_deferred_file" ); +if (!platform_unlink (ccs->deferred_ret_file)) + { +msg (D_MULTI_ERRORS ,"MULTI: problem deleting temporary file: %s" , + ccs->deferred_ret_file); + } +free (ccs->deferred_ret_file); + ccs->deferred_ret_file =NULL ; + }
As discussed on IRC, previous patches (already applied) changed the style of many of our functions from:
if (x)
{
do something
}
to:
if (!x)
{
return
}
Now this patch is unfortunately introducing a set of functions all implemented using the old pattern.
I suggest them to be converted before we merge this patch.
I don't really have other comments about this patch, therefore it may go in once we get the code style right.
Regards,
Signature · 1 line
Antonio Quartulli
PATCH v7 3/6 client-connect: Use inotify for the deferred client-connect status file
In reply to Arne SchwabeAs we never do client-connect and authentication at the same time it is safe to reuse the existing fields for client-connect return status file
--- src/openvpn/multi.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
src/openvpn/multi.c +13 -3
@@ -2603,8 +2603,10 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)#ifdef ENABLE_ASYNC_PUSH /* -* Called when inotify event is fired, which happens when acf file is closed or deleted. -* Continues authentication and sends push_reply. +* Called when inotify event is fired, which happens when acf +* or connect-status file is closed or deleted. +* Continues authentication and sends push_reply +* (or be deferred again by client-connect) */ void multi_process_file_closed (struct multi_context *m,const unsigned int mpp_flags)
@@ -2890,7 +2892,15 @@ multi_process_post(struct multi_context *m, struct multi_instance *mi, const uns {multi_connection_established (m, mi); } - +#if defined(ENABLE_ASYNC_PUSH ) && defined(ENABLE_DEF_AUTH ) +if (is_cas_pending (mi->context.c2.context_auth) + && mi->client_connect_defer_state.deferred_ret_file) + { +add_inotify_file_watch (m, mi, m->top.c2.inotify_fd, + mi->client_connect_defer_state. + deferred_ret_file); + } +#endif /* tell scheduler to wake us up at some point in the future */ multi_schedule_context_wakeup (m, mi); }
Signature · 1 line
2.26.2
Re: [Openvpn-devel] [PATCH v7 3/6] client-connect: Use inotify for the deferred client-connect status file
Hi,
Quoted 3 lines · Arne Schwabe
As we never do client-connect and authentication at the same time it is safe to reuse the existing fields for client-connect return status file
I don't have anything with inotify prepared right now, so I can't really test this (it compiles *without* #define ENABLE_ASYNC_PUSH, this I can already attest).
It would be good to have Lev test this very thoroughly...
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...PATCH v7 4/6 client-connect: Add deferred support to the client-connect plugin v1 handler
In reply to Arne SchwabeFrom: Fabian Knittel <[email protected]...>
Uses the infrastructure provided and used in the previous patch to provide deferral support to the v1 client-connect plugin handler as well.
PATCH V3: Modify the API to also (optionally) call the plugin on a deferred call. This allows the plugin authors to be more flexible and make the V1 API more similar to the V2 API.
--- include/openvpn-plugin.h.in | 29 +++++++------- src/openvpn/multi.c | 78 ++++++++++++++++++++++++++----------- src/openvpn/plugin.c | 3 ++ 3 files changed, 73 insertions(+), 37 deletions(-)
include/openvpn-plugin.h.in +15 -14
@@ -116,20 +116,21 @@ extern "C" { * FUNC: openvpn_plugin_client_destructor_v1 (top-level "generic" client) * FUNC: openvpn_plugin_close_v1 */ -#define OPENVPN_PLUGIN_UP 0 -#define OPENVPN_PLUGIN_DOWN 1 -#define OPENVPN_PLUGIN_ROUTE_UP 2 -#define OPENVPN_PLUGIN_IPCHANGE 3 -#define OPENVPN_PLUGIN_TLS_VERIFY 4 -#define OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY 5 -#define OPENVPN_PLUGIN_CLIENT_CONNECT 6 -#define OPENVPN_PLUGIN_CLIENT_DISCONNECT 7 -#define OPENVPN_PLUGIN_LEARN_ADDRESS 8 -#define OPENVPN_PLUGIN_CLIENT_CONNECT_V2 9 -#define OPENVPN_PLUGIN_TLS_FINAL 10 -#define OPENVPN_PLUGIN_ENABLE_PF 11 -#define OPENVPN_PLUGIN_ROUTE_PREDOWN 12 -#define OPENVPN_PLUGIN_N 13 +#define OPENVPN_PLUGIN_UP 0 +#define OPENVPN_PLUGIN_DOWN 1 +#define OPENVPN_PLUGIN_ROUTE_UP 2 +#define OPENVPN_PLUGIN_IPCHANGE 3 +#define OPENVPN_PLUGIN_TLS_VERIFY 4 +#define OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY 5 +#define OPENVPN_PLUGIN_CLIENT_CONNECT 6 +#define OPENVPN_PLUGIN_CLIENT_DISCONNECT 7 +#define OPENVPN_PLUGIN_LEARN_ADDRESS 8 +#define OPENVPN_PLUGIN_CLIENT_CONNECT_V2 9 +#define OPENVPN_PLUGIN_TLS_FINAL 10 +#define OPENVPN_PLUGIN_ENABLE_PF 11 +#define OPENVPN_PLUGIN_ROUTE_PREDOWN 12 +#define OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER 13 +#define OPENVPN_PLUGIN_N 14 /* * Build a mask out of a set of plug-in types.
src/openvpn/multi.c +55 -23
108 lines not on this page · raw message
src/openvpn/plugin.c +3 -0
@@ -104,6 +104,9 @@ plugin_type_name(const int type)case OPENVPN_PLUGIN_CLIENT_CONNECT_V2 :return "PLUGIN_CLIENT_CONNECT" ; +case OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER : +return "PLUGIN_CLIENT_CONNECT" ; +case OPENVPN_PLUGIN_CLIENT_DISCONNECT :return "PLUGIN_CLIENT_DISCONNECT" ;
Signature · 1 line
2.26.2
Re: [Openvpn-devel] [PATCH v7 4/6] client-connect: Add deferred support to the client-connect plugin v1 handler
Hi,
Quoted 4 lines · Arne Schwabe
From: Fabian Knittel <[email protected]...>
Uses the infrastructure provided and used in the previous patch to provide deferral support to the v1 client-connect plugin handler as well.
start client jobs...
22...
Test sets succeeded: 1 2 3 4 6 8.
Test sets failed: none.
23.small...
Test sets succeeded: 1 2 3 4.
Test sets failed: none.
23...
Test sets succeeded: 1 1a 1b 1d 2 2a 2b 2c 2d 3 4 5 6 8 8a 9.
Test sets failed: none.
24...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 4a 5 6 8 8a 9. Test sets failed: none.
master...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 5 5x 6 7 7x 8 8a 9 2f 4b. Test sets failed: none.
(not actually testing any particulars of this patch yet, just keeping the test rig busy)
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...PATCH v7 5/6 client-connect: Implement deferred connect support for plugin API v2
In reply to Arne SchwabeThe V2 API is simpler than the V1 API since there is no passing of data via files. This also means that with the current API the V2 API cannot support async notify via files. Adding a file just for async notify seems very hacky and when needed we should implement a better option when async is needed for the plugin V2 API.
--- include/openvpn-plugin.h.in | 3 ++- src/openvpn/multi.c | 36 ++++++++++++++++++++++++------------ src/openvpn/plugin.c | 3 +++ 3 files changed, 29 insertions(+), 13 deletions(-)
include/openvpn-plugin.h.in +2 -1
@@ -130,7 +130,8 @@ extern "C" { #define OPENVPN_PLUGIN_ENABLE_PF 11 #define OPENVPN_PLUGIN_ROUTE_PREDOWN 12 #define OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER 13 -#define OPENVPN_PLUGIN_N 14 +#define OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER_V2 14 +#define OPENVPN_PLUGIN_N 15 /* * Build a mask out of a set of plug-in types.
src/openvpn/multi.c +24 -12
@@ -2112,36 +2112,48 @@ multi_client_connect_call_plugin_v2(struct multi_context *m,bool deferred, unsigned int *option_types_found) { - if (deferred) - { - return CC_RET_FAILED; - } enum client_connect_return ret = CC_RET_SKIPPED;#ifdef ENABLE_PLUGIN ASSERT (m);ASSERT (mi);ASSERT (option_types_found); +int call = deferred ?OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER_V2 : +OPENVPN_PLUGIN_CLIENT_CONNECT_V2 ;/* V2 callback, use a plugin_return struct for passing back return info */ -if (plugin_defined (mi->context.plugins,OPENVPN_PLUGIN_CLIENT_CONNECT_V2 )) +if (plugin_defined (mi->context.plugins, call)) {struct plugin_return pr;plugin_return_init (&pr); -if (plugin_call (mi->context.plugins,OPENVPN_PLUGIN_CLIENT_CONNECT_V2 , -NULL , &pr, mi->context.c2.es) - !=OPENVPN_PLUGIN_FUNC_SUCCESS ) +int plug_ret =plugin_call (mi->context.plugins, call, +NULL , &pr, mi->context.c2.es); +if (plug_ret ==OPENVPN_PLUGIN_FUNC_SUCCESS ) { -msg (M_WARN ,"WARNING: client-connect-v2 plugin call failed" ); - ret =CC_RET_FAILED ; +multi_client_connect_post_plugin (m, mi, &pr, option_types_found); + ret =CC_RET_SUCCEEDED ; + } +else if (plug_ret ==OPENVPN_PLUGIN_FUNC_DEFERRED ) + { + ret =CC_RET_DEFERRED ; +if (!(plugin_defined (mi->context.plugins, +OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER_V2 ))) + { +msg (M_WARN ,"A plugin that defers from the " +"OPENVPN_PLUGIN_CLIENT_CONNECT_V2 call must also " +"declare support for " +"OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER_V2" ); + ret =CC_RET_FAILED ; + } }else { -multi_client_connect_post_plugin (m, mi, &pr, option_types_found); - ret =CC_RET_SUCCEEDED ; +msg (M_WARN ,"WARNING: client-connect-v2 plugin call failed" ); + ret =CC_RET_FAILED ; } +plugin_return_free (&pr); }#endif /* ifdef ENABLE_PLUGIN */
src/openvpn/plugin.c +3 -0
@@ -107,6 +107,9 @@ plugin_type_name(const int type)case OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER :return "PLUGIN_CLIENT_CONNECT" ; +case OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER_V2 : +return "PLUGIN_CLIENT_CONNECT" ; +case OPENVPN_PLUGIN_CLIENT_DISCONNECT :return "PLUGIN_CLIENT_DISCONNECT" ;
Signature · 1 line
2.26.2
Re: [Openvpn-devel] [PATCH v7 5/6] client-connect: Implement deferred connect support for plugin API v2
Hi,
Quoted 5 lines · Arne Schwabe
The V2 API is simpler than the V1 API since there is no passing of data via files. This also means that with the current API the V2 API cannot support async notify via files. Adding a file just for async notify seems very hacky and when needed we should implement a better option when async is needed for the plugin V2 API.
Test reports... not actually *using* the API yet, just confirming that nothing else broke.
22...
Test sets succeeded: 1 2 3 4 6 8.
Test sets failed: none.
23.small...
Test sets succeeded: 1 2 3 4.
Test sets failed: none.
23...
Test sets succeeded: 1 1a 1b 1d 2 2a 2b 2c 2d 3 4 5 6 8 8a 9.
Test sets failed: none.
24...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 4a 5 6 8 8a 9. Test sets failed: none.
master...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 5 5x 6 7 7x 8 8a 9 2f 4b. Test sets failed: none.
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...PATCH v7 6/6 client-connect: Add documentation for the deferred client connect feature
In reply to Arne SchwabePatch V5: Fix typos, clarify man page section about deferred client-connect
script. Add section to Changes.rst
--- Changes.rst | 4 +++ doc/openvpn.8 | 55 +++++++++++++++++++++++++++++++++++-- include/openvpn-plugin.h.in | 21 ++++++++++---- 3 files changed, 71 insertions(+), 9 deletions(-)
Changes.rst +4 -0
@@ -3,6 +3,10 @@ Overview of changes in 2.5 New features ------------ +Deferred client-connect + client-connect and the connect plugin API allow now asynchronous/deferred + return of the configuration file in the same way as the auth-plugin. + Client-specific tls-crypt keys (``--tls-crypt-v2``) ``tls-crypt-v2`` adds the ability to supply each client with a unique tls-crypt key. This allows large organisations and VPN providers to profit
doc/openvpn.8 +52 -3
76 lines not on this page · raw message
include/openvpn-plugin.h.in +15 -6
@@ -557,12 +557,21 @@ OPENVPN_PLUGIN_DEF openvpn_plugin_handle_t OPENVPN_PLUGIN_FUNC(openvpn_plugin_op * OPENVPN_PLUGIN_FUNC_SUCCESS on success, OPENVPN_PLUGIN_FUNC_ERROR on failure * * In addition, OPENVPN_PLUGIN_FUNC_DEFERRED may be returned by - * OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY. This enables asynchronous - * authentication where the plugin (or one of its agents) may indicate - * authentication success/failure some number of seconds after the return - * of the OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY handler by writing a single - * char to the file named by auth_control_file in the environmental variable - * list (envp). + * OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY, OPENVPN_PLUGIN_CLIENT_CONNECT and + * OPENVPN_PLUGIN_CLIENT_CONNECT_V2. This enables asynchronous + * authentication or client connect where the plugin (or one of its agents) + * may indicate authentication success/failure or client configuration some + * number of seconds after the return of the function handler. + * For OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY and OPENVPN_PLUGIN_CLIENT_CONNECT + * this is done by writing a single char to the file named by + * auth_control_file/client_connect_deferred_file + * in the environmental variable list (envp). + * + * In addition the OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER and + * OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER_V2 are called when OpenVPN tries to + * get the deferred result. For a V2 call implementing this function is + * required as information is not passed by files. For the normal version + * the call is optional. * * first char of auth_control_file: * '0' -- indicates auth failure
Signature · 1 line
2.26.2
Hi,
Quoted 4 lines · Arne Schwabe
This patch moves the state, that was previously tracked within the
multi_connection_established()function, into structclient_connect_state. Themulti_connection_established()function can now be exited and re-entered as many times as necessary - without losing the client-connect handling state.
This looks much nicer now :-) - I haven't done a full review yet, but I fed it to the test machine, and it seem to have liked it. Including "2.2 client" and "2.3 --enable-small client" now... *brag*
22...
Test sets succeeded: 1 2 3 4 6 8.
Test sets failed: none.
23.small...
Test sets succeeded: 1 2 3 4.
Test sets failed: none.
23...
Test sets succeeded: 1 1a 1b 1d 2 2a 2b 2c 2d 3 4 5 6 8 8a 9.
Test sets failed: none.
24...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 4a 5 6 8 8a 9. Test sets failed: none.
master...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 5 5x 6 7 7x 8 8a 9 2f 4b. Test sets failed: none.
The test setup is using --client-connect and CCD/ files, but no CC plugin yet (nor management console). So it's not excercising all these code parts very well.
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Hi,
Quoted 25 lines · Arne Schwabe
This patch moves the state, that was previously tracked within the
multi_connection_established()function, into structclient_connect_state. Themulti_connection_established()function can now be exited and re-entered as many times as necessary - without losing the client-connect handling state.The patch also adds the new return value
CC_RET_DEFERREDwhich indicates that the handler couldn't complete immediately, and needs to be called later. At that pointmulti_connection_established()will exit without indicating completion.Each client-connect handler now has an (optional) additional call-back: The call-back for handling the deferred case. If the main call-back returns
CC_RET_DEFERRED, the next call to the handler will be through the deferred call-back.Signed-off-by Fabian Knittel <[email protected]...>Patch V3: Use a static struct in
multi_instanceinstead of using
malloc/free and use two states (deffered with and without result) instead of one to eliminate the counter that was only tested for > 0.Patch V5: Use new states in
context_authinstead of the extra state
that the patch series previously used.
Quoted 7 lines
Patch V6: Restructure code to make it a bit more readable, rebase on
master.Patch V7: move defferred bool into client connect handler calls, switch
to switch caseSigned-off-by Arne Schwabe <[email protected]...>
Haven't done a full test, this is why we have "Gert and his rig"[tm], but the code looks good and I can't spot anything that may trigger my personal alarm.
This version is much cleaner that what it looked like before. Thanks Arne for reworking the patch once again.
Signature · 1 line
Antonio Quartulli
Your patch has been applied to the master branch.
Tested on the test rig, stared-at-code by antonio, and commit-message-adjusted by me :-) (a few "defferred" and integrating the new call convention)
commit dfb40edc4acae5f17b0062ecb13ad1fa760ed529
Author: Arne Schwabe Date: Thu Jul 16 15:43:10 2020 +0200
client-connect: Add CC_RET_DEFERRED and cope with deferred client-connect
Signed-off-by: Fabian Knittel <[email protected]...>
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Antonio Quartulli <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg20395.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
Your patch has been applied to the master branch.
Tested on the test rig, stared-at-code by antonio, and commit-message-adjusted by me :-) (a few "defferred" and integrating the new call convention).
Pushed, then went out to write a plugin to excercise this a bit more... and lo and behold, breakage!
If I run a server that has *both* a plugin with the connect handler configured (which succeeds) and a client-connect script (which fails), it will sort of "stare into nothingness".
With only the client-connect script configured, I get:
2020-07-17 18:35:10 us=536817 ... WARNING: Failed running command (--client-connect): external program exited with error status: 10 2020-07-17 18:35:11 us=751714 ... PUSH: Received control message: 'PUSH_REQUEST' 2020-07-17 18:35:11 us=751860 ... SENT CONTROL [cron2-freebsd-tc-amd64]: 'AUTH_FAILED' (status=1)
"all good"
with both configured, I get:
2020-07-17 18:32:34 us=478215 PLUGIN_CALL: POST /root/t_server/tun-udp-p2mp-112-mask/sample-client-connect.so/PLUGIN_CLIENT_CONNECT status=0 2020-07-17 18:32:34 us=478331 OPTIONS IMPORT: reading client specific options from: /tmp/openvpn_cc_36b55f67d059a540483662f8e736189.tmp 2020-07-17 18:32:34 us=478562 PLUGIN sample-cc: OPENVPN_PLUGIN_CLIENT_CONNECT_V2 2020-07-17 18:32:34 us=478670 PLUGIN_CALL: POST /root/t_server/tun-udp-p2mp-112-mask/sample-client-connect.so/PLUGIN_CLIENT_CONNECT status=0 2020-07-17 18:32:34 us=490057 WARNING: Failed running command (--client-connect): external program exited with error status: 10 2020-07-17 18:32:35 us=618131 ... PUSH: Received control message: 'PUSH_REQUEST' 2020-07-17 18:32:40 us=801869 ... PUSH: Received control message: 'PUSH_REQUEST' 2020-07-17 18:32:45 us=948480 ... PUSH: Received control message: 'PUSH_REQUEST' 2020-07-17 18:32:50 us=990555 ... PUSH: Received control message: 'PUSH_REQUEST' 2020-07-17 18:32:56 us=51592 ... PUSH: Received control message: 'PUSH_REQUEST' 2020-07-17 18:33:01 us=311645 ... PUSH: Received control message: 'PUSH_REQUEST' 2020-07-17 18:33:06 us=612242 ... PUSH: Received control message: 'PUSH_REQUEST' 2020-07-17 18:33:11 us=811682 ... PUSH: Received control message: 'PUSH_REQUEST' 2020-07-17 18:33:17 us=11695 ... PUSH: Received control message: 'PUSH_REQUEST' 2020-07-17 18:33:22 us=151679 ... PUSH: Received control message: 'PUSH_REQUEST' 2020-07-17 18:33:27 us=311636 ... PUSH: Received control message: 'PUSH_REQUEST' 2020-07-17 18:33:32 us=351693 ... PUSH: Received control message: 'PUSH_REQUEST' 2020-07-17 18:33:47 us=513879 MULTI: multi_create_instance called 2020-07-17 18:33:47 us=514020 194.97.140.21:51169 Re-using SSL/TLS context
(client retrying after timeout)
Now, if I make the *plugin* fail, it works as requested...
2020-07-17 18:47:21 us=372780 PLUGIN sample-cc: env has UV_WANT_CC_FAIL=10 -> fail 2020-07-17 18:47:21 us=372800 PLUGIN_CALL: POST /root/t_server/tun-udp-p2mp-112-mask/sample-client-connect.so/PLUGIN_CLIENT_CONNECT status=1 2020-07-17 18:47:21 us=372812 PLUGIN_CALL: plugin function PLUGIN_CLIENT_CONNECT failed with status 1: /root/t_server/tun-udp-p2mp-112-mask/sample-client-connect.so 2020-07-17 18:47:21 us=372824 WARNING: client-connect plugin call failed 2020-07-17 18:47:22 us=479574 ... PUSH: Received control message: 'PUSH_REQUEST' 2020-07-17 18:47:22 us=479634 ... Delayed exit in 5 seconds 2020-07-17 18:47:22 us=479661 ... SENT CONTROL [cron2-freebsd-tc-amd64]: 'AUTH_FAILED' (status=1)
- so it seems something in the chain of things is not good if "more than one" is there.
commit dfb40edc4acae5f17b0062ecb13ad1fa760ed529
Author: Arne Schwabe Date: Thu Jul 16 15:43:10 2020 +0200
client-connect: Add CC_RET_DEFERRED and cope with deferred client-connect
Signed-off-by: Fabian Knittel <[email protected]...>
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Antonio Quartulli <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg20395.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering