diff --git a/changes-entries/md-retry-after.txt b/changes-entries/md-retry-after.txt new file mode 100644 index 00000000000..63de6bc7f21 --- /dev/null +++ b/changes-entries/md-retry-after.txt @@ -0,0 +1,4 @@ + *) mod_md: Honour the Retry-After header of ACME responses, as RFC 8555 + recommends, when polling orders and authorizations, after finalizing an + order and when a request is rate limited. PR 70282 + [Arturo Bernal] diff --git a/modules/md/md_acme.c b/modules/md/md_acme.c index 764342f7835..8554d3bf5cb 100644 --- a/modules/md/md_acme.c +++ b/modules/md/md_acme.c @@ -170,12 +170,27 @@ apr_status_t md_acme_init(apr_pool_t *p, const char *base, int init_ssl) return init_ssl? md_crypt_init(p) : APR_SUCCESS; } +apr_time_t md_acme_retry_after(const struct apr_table_t *headers) +{ + const char *value = headers? apr_table_get(headers, "Retry-After") : NULL; + const char *date = headers? apr_table_get(headers, "Date") : NULL; + apr_time_t now = apr_time_now(); + apr_interval_time_t delay; + + if (!value || APR_SUCCESS != md_util_parse_retry_after(&delay, value, date, now)) { + return 0; + } + return (now > 0 && delay > APR_INT64_MAX - now)? APR_INT64_MAX : now + delay; +} + static apr_status_t inspect_problem(md_acme_req_t *req, const md_http_response_t *res) { const char *ctype; md_json_t *problem = NULL; apr_status_t rv; + /* e.g. rateLimited, RFC 8555 6.6 */ + req->result->retry_at = md_acme_retry_after(req->resp_hdrs); ctype = apr_table_get(req->resp_hdrs, "content-type"); ctype = md_util_parse_ct(res->req->pool, ctype); if (ctype && !strcmp(ctype, "application/problem+json")) { @@ -490,6 +505,7 @@ void md_acme_report_result(md_acme_t *acme, apr_status_t rv, struct md_result_t else { md_result_problem_set(result, acme->last->status, acme->last->problem, acme->last->detail, acme->last->subproblems); + result->retry_at = acme->last->retry_at; } } diff --git a/modules/md/md_acme.h b/modules/md/md_acme.h index 76c27a88dd0..f9797bea3d9 100644 --- a/modules/md/md_acme.h +++ b/modules/md/md_acme.h @@ -168,6 +168,14 @@ apr_status_t md_acme_setup(md_acme_t *acme, struct md_result_t *result); void md_acme_report_result(md_acme_t *acme, apr_status_t rv, struct md_result_t *result); +/** + * Get the local time a response asks the client to wait for with its + * Retry-After header (RFC 9110, 10.2.3). An HTTP-date is read against the + * Date header of the response, so a skewed clock of the server does not matter. + * @return the time, or 0 if there is no valid Retry-After + */ +apr_time_t md_acme_retry_after(const struct apr_table_t *headers); + /**************************************************************************************************/ /* account handling */ diff --git a/modules/md/md_acme_authz.c b/modules/md/md_acme_authz.c index 75c4bbe098e..e9bdaf9a1c1 100644 --- a/modules/md/md_acme_authz.c +++ b/modules/md/md_acme_authz.c @@ -112,6 +112,38 @@ static int copy_challenge_error(void *baton, size_t index, md_json_t *json) return 1; } +typedef struct { + apr_pool_t *p; + md_acme_authz_t *authz; + md_json_t *json; +} authz_get_ctx; + +static apr_status_t on_authz_got(md_acme_t *acme, apr_pool_t *p, const apr_table_t *hdrs, + md_json_t *body, void *baton) +{ + authz_get_ctx *ctx = baton; + + (void)acme; + (void)p; + ctx->json = md_json_clone(ctx->p, body); + ctx->authz->retry_at = md_acme_retry_after(hdrs); + return APR_SUCCESS; +} + +static apr_status_t authz_get_json(md_json_t **pjson, md_acme_authz_t *authz, + md_acme_t *acme, apr_pool_t *p) +{ + authz_get_ctx ctx; + apr_status_t rv; + + ctx.p = p; + ctx.authz = authz; + ctx.json = NULL; + rv = md_acme_GET(acme, authz->url, NULL, on_authz_got, NULL, NULL, 1, &ctx); + *pjson = (APR_SUCCESS == rv)? ctx.json : NULL; + return rv; +} + apr_status_t md_acme_authz_update(md_acme_authz_t *authz, md_acme_t *acme, apr_pool_t *p) { md_json_t *json; @@ -132,7 +164,8 @@ apr_status_t md_acme_authz_update(md_acme_authz_t *authz, md_acme_t *acme, apr_p err = "unable to parse response"; log_level = MD_LOG_ERR; - if (APR_SUCCESS == (rv = md_acme_get_json(&json, acme, authz->url, 1, p)) + authz->retry_at = 0; + if (APR_SUCCESS == (rv = authz_get_json(&json, authz, acme, p)) && (s = md_json_gets(json, MD_KEY_STATUS, NULL))) { authz->domain = md_json_gets(json, MD_KEY_IDENTIFIER, MD_KEY_VALUE, NULL); @@ -208,8 +241,8 @@ static apr_status_t authz_http_set(md_acme_t *acme, apr_pool_t *p, const apr_tab (void)acme; (void)p; - (void)hdrs; (void)body; + ctx->authz->retry_at = md_acme_retry_after(hdrs); md_log_perror(MD_LOG_MARK, MD_LOG_TRACE1, 0, ctx->p, "updated authz %s", ctx->authz->url); return APR_SUCCESS; } diff --git a/modules/md/md_acme_authz.h b/modules/md/md_acme_authz.h index d74beebab68..74349bb040d 100644 --- a/modules/md/md_acme_authz.h +++ b/modules/md/md_acme_authz.h @@ -53,6 +53,7 @@ struct md_acme_authz_t { const char *error_detail; const struct md_json_t *error_subproblems; struct md_json_t *resource; + apr_time_t retry_at; /* not persisted: Retry-After of the last response */ }; #define MD_FN_HTTP01 "acme-http-01.txt" diff --git a/modules/md/md_acme_drive.c b/modules/md/md_acme_drive.c index bef07e52d91..f51b30255ce 100644 --- a/modules/md/md_acme_drive.c +++ b/modules/md/md_acme_drive.c @@ -304,6 +304,8 @@ static apr_status_t csr_req(md_acme_t *acme, const md_http_response_t *res, void apr_status_t rv = APR_SUCCESS; (void)acme; + /* the order may still be processing, the CA says when to look again */ + ad->order->retry_at = md_acme_retry_after(res->headers); location = apr_table_get(res->headers, "location"); if (!location) return rv; diff --git a/modules/md/md_acme_order.c b/modules/md/md_acme_order.c index 22d84a2594a..2fbffccd74d 100644 --- a/modules/md/md_acme_order.c +++ b/modules/md/md_acme_order.c @@ -267,12 +267,13 @@ typedef struct { const char *ari_cert_id; apr_array_header_t *domains; md_result_t *result; + apr_time_t retry_at; } order_ctx_t; #define ORDER_CTX_INIT(ctx, p, o, a, n, d, pf, cid, r) \ (ctx)->p = (p); (ctx)->order = (o); (ctx)->acme = (a); \ (ctx)->name = (n); (ctx)->domains = d; (ctx)->profile = pf; \ - (ctx)->ari_cert_id = cid;(ctx)->result = r + (ctx)->ari_cert_id = cid;(ctx)->result = r; (ctx)->retry_at = 0 static apr_status_t identifier_to_json(void *value, md_json_t *json, apr_pool_t *p, void *baton) { @@ -323,6 +324,7 @@ static apr_status_t on_order_upd(md_acme_t *acme, apr_pool_t *p, const apr_table } order_update_from_json(ctx->order, body, ctx->p); + ctx->order->retry_at = md_acme_retry_after(hdrs); out: return rv; } @@ -391,7 +393,8 @@ apr_status_t md_acme_order_await_ready(md_acme_order_t *order, md_acme_t *acme, ORDER_CTX_INIT(&ctx, p, order, acme, md->name, NULL, NULL, NULL, result); md_result_activity_setn(result, "Waiting for order to become ready"); - rv = md_util_try(await_ready, &ctx, 0, timeout, 0, 0, 1); + rv = md_util_try_with_retry_after(await_ready, &ctx, 0, timeout, 0, 0, 1, + &order->retry_at); md_result_log(result, MD_LOG_DEBUG); return rv; } @@ -434,7 +437,8 @@ apr_status_t md_acme_order_await_valid(md_acme_order_t *order, md_acme_t *acme, ORDER_CTX_INIT(&ctx, p, order, acme, md->name, NULL, NULL, NULL, result); md_result_activity_setn(result, "Waiting for finalized order to become valid"); - rv = md_util_try(await_valid, &ctx, 0, timeout, 0, 0, 1); + rv = md_util_try_with_retry_after(await_valid, &ctx, 0, timeout, 0, 0, 1, + &order->retry_at); md_result_log(result, MD_LOG_DEBUG); return rv; } @@ -479,6 +483,9 @@ apr_status_t md_acme_order_start_challenges(md_acme_order_t *order, md_acme_t *a if (APR_SUCCESS != rv) { goto leave; } + if (authz->retry_at > order->retry_at) { + order->retry_at = authz->retry_at; + } add_setup_token(order, setup_token); md_acme_order_save(store, p, MD_SG_STAGING, md->name, order, 0); break; @@ -510,6 +517,7 @@ static apr_status_t check_challenges(void *baton, int attempt) apr_status_t rv = APR_SUCCESS; int i; + ctx->retry_at = 0; for (i = 0; i < ctx->order->authz_urls->nelts; ++i) { url = APR_ARRAY_IDX(ctx->order->authz_urls, i, const char*); md_log_perror(MD_LOG_MARK, MD_LOG_DEBUG, rv, ctx->p, "%s: check AUTHZ at %s (attempt %d)", @@ -524,6 +532,7 @@ static apr_status_t check_challenges(void *baton, int attempt) break; case MD_ACME_AUTHZ_S_PENDING: rv = APR_EAGAIN; + ctx->retry_at = authz->retry_at; md_log_perror(MD_LOG_MARK, MD_LOG_DEBUG, rv, ctx->p, "%s: status pending at %s", authz->domain, authz->url); goto leave; @@ -561,9 +570,13 @@ apr_status_t md_acme_order_monitor_authzs(md_acme_order_t *order, md_acme_t *acm apr_status_t rv; ORDER_CTX_INIT(&ctx, p, order, acme, md->name, NULL, NULL, NULL, result); + ctx.retry_at = order->retry_at; md_result_activity_printf(result, "Monitoring challenge status for %s", md->name); - rv = md_util_try(check_challenges, &ctx, 0, timeout, 0, 0, 1); + rv = md_util_try_with_retry_after(check_challenges, &ctx, 0, timeout, 0, 0, 1, + &ctx.retry_at); + /* that was the hint for the authorizations, not for the order */ + order->retry_at = 0; md_log_perror(MD_LOG_MARK, MD_LOG_DEBUG, rv, p, "%s: checked authorizations", md->name); return rv; } diff --git a/modules/md/md_acme_order.h b/modules/md/md_acme_order.h index 2d08cd9861a..4c132a554c9 100644 --- a/modules/md/md_acme_order.h +++ b/modules/md/md_acme_order.h @@ -33,6 +33,7 @@ struct md_acme_order_t { apr_pool_t *p; const char *url; md_acme_order_st status; + apr_time_t retry_at; /* not persisted: Retry-After of the last response */ struct apr_array_header_t *authz_urls; struct apr_array_header_t *challenge_setups; struct md_json_t *json; diff --git a/modules/md/md_acmev2_drive.c b/modules/md/md_acmev2_drive.c index facb080f5e3..b84c42e2943 100644 --- a/modules/md/md_acmev2_drive.c +++ b/modules/md/md_acmev2_drive.c @@ -148,6 +148,12 @@ static apr_status_t ad_setup_order(md_proto_driver_t *d, md_result_t *result, in /**************************************************************************************************/ /* ACMEv2 renewal */ +/* A wait cut short by a shutdown is not a failed renewal, do not report it as one. */ +static void wait_failed(md_result_t *result, apr_status_t rv, const char *msg) +{ + md_result_set(result, rv, APR_STATUS_IS_EINTR(rv)? "Interrupted by shutdown" : msg); +} + apr_status_t md_acmev2_drive_renew(md_acme_driver_t *ad, md_proto_driver_t *d, md_result_t *result) { apr_status_t rv = APR_SUCCESS; @@ -208,14 +214,14 @@ apr_status_t md_acmev2_drive_renew(md_acme_driver_t *ad, md_proto_driver_t *d, m rv = md_acme_order_monitor_authzs(ad->order, ad->acme, d->md, ad->authz_monitor_timeout, result, d->p); if (APR_SUCCESS != rv) { - md_result_set(result, rv, "Error waiting on domain names to be validated"); + wait_failed(result, rv, "Error waiting on domain names to be validated"); goto leave; } rv = md_acme_order_await_ready(ad->order, ad->acme, d->md, ad->authz_monitor_timeout, result, d->p); if (APR_SUCCESS != rv) { - md_result_set(result, rv, "Error waiting for order to become ready"); + wait_failed(result, rv, "Error waiting for order to become ready"); goto leave; } @@ -228,7 +234,7 @@ apr_status_t md_acmev2_drive_renew(md_acme_driver_t *ad, md_proto_driver_t *d, m rv = md_acme_order_await_valid(ad->order, ad->acme, d->md, ad->authz_monitor_timeout, result, d->p); if (APR_SUCCESS != rv) { - md_result_set(result, rv, "Error waiting for order to become valid."); + wait_failed(result, rv, "Error waiting for order to become valid."); goto leave; } diff --git a/modules/md/md_result.c b/modules/md/md_result.c index 64a2f70bac8..c156c71d72d 100644 --- a/modules/md/md_result.c +++ b/modules/md/md_result.c @@ -210,6 +210,7 @@ void md_result_assign(md_result_t *dest, const md_result_t *src) dest->detail = src->detail; dest->activity = src->activity; dest->ready_at = src->ready_at; + dest->retry_at = src->retry_at; dest->subproblems = src->subproblems; } @@ -220,6 +221,7 @@ void md_result_dup(md_result_t *dest, const md_result_t *src) dest->detail = src->detail? apr_pstrdup(dest->p, src->detail) : NULL; dest->activity = src->activity? apr_pstrdup(dest->p, src->activity) : NULL; dest->ready_at = src->ready_at; + dest->retry_at = src->retry_at; dest->subproblems = src->subproblems? md_json_clone(dest->p, src->subproblems) : NULL; on_change(dest); } diff --git a/modules/md/md_result.h b/modules/md/md_result.h index e83bdd22236..0f57e468cf2 100644 --- a/modules/md/md_result.h +++ b/modules/md/md_result.h @@ -35,6 +35,7 @@ struct md_result_t { const struct md_json_t *subproblems; const char *activity; apr_time_t ready_at; + apr_time_t retry_at; /* earliest time the server asked us to come back, or 0 */ md_result_change_cb *on_change; void *on_change_data; md_result_raise_cb *on_raise; diff --git a/modules/md/md_status.c b/modules/md/md_status.c index da89b832ca2..767b7667d2c 100644 --- a/modules/md/md_status.c +++ b/modules/md/md_status.c @@ -678,10 +678,26 @@ void md_job_end_run(md_job_t *job, md_result_t *result) job->dirty = 1; md_job_log_append(job, "finished", NULL, NULL); } + else if (APR_STATUS_IS_EINTR(result->status)) { + /* Cancelled by a shutdown, not a failed run: no error is counted + * and no delay applies, the next process starts over. */ + job->next_run = 0; + job->dirty = 1; + } else { + apr_time_t now = apr_time_now(); + apr_time_t retry_at = result->retry_at; + ++job->error_runs; job->dirty = 1; - job->next_run = apr_time_now() + md_job_delay_on_errors(job, job->error_runs, result->problem); + job->next_run = now + md_job_delay_on_errors(job, job->error_runs, result->problem); + /* honour a Retry-After of the CA, but not for longer than we ever back off */ + if (retry_at > now + apr_time_from_sec(24*60*60)) { + retry_at = now + apr_time_from_sec(24*60*60); + } + if (retry_at > job->next_run) { + job->next_run = retry_at; + } } job_observation_end(job); } diff --git a/modules/md/md_util.c b/modules/md/md_util.c index 35a74082dee..01a08eb2769 100644 --- a/modules/md/md_util.c +++ b/modules/md/md_util.c @@ -1037,11 +1037,304 @@ apr_status_t md_util_abs_http_uri_check(apr_pool_t *p, const char *uri, const ch return rv; } +/* Retry-After header parsing ***********************************************************************/ + +/* HTTP-date as in RFC 9110, section 5.6.7: IMF-fixdate, rfc850-date or + * asctime-date. Unlike apr_date_parse_http() the whole value must match. */ +static int ra_digits(const char *s, int n) +{ + int v = 0; + + while (n-- > 0) { + if (*s < '0' || *s > '9') { + return -1; + } + v = v * 10 + (*s++ - '0'); + } + return v; +} + +static int ra_month(const char *s) +{ + static const char months[] = "JanFebMarAprMayJunJulAugSepOctNovDec"; + int i; + + for (i = 0; i < 12; ++i) { + if (!memcmp(s, months + 3 * i, 3)) { + return i; + } + } + return -1; +} + +/* day-name, or day-name-l if len is more than 3, must match case sensitively */ +static int ra_day_name(const char *s, apr_size_t len) +{ + static const char *const names[] = { + "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" + }; + int i; + + for (i = 0; i < 7; ++i) { + if (strlen(names[i]) == len ? !memcmp(s, names[i], len) + : (len == 3 && !memcmp(s, names[i], 3))) { + return 1; + } + } + return 0; +} + +static int ra_time_of_day(const char *s, int *h, int *m, int *sec) +{ + if (s[2] != ':' || s[5] != ':') { + return 0; + } + *h = ra_digits(s, 2); + *m = ra_digits(s + 3, 2); + *sec = ra_digits(s + 6, 2); + /* a leap second, 60, is syntactically valid */ + return *h >= 0 && *h < 24 && *m >= 0 && *m < 60 && *sec >= 0 && *sec <= 60; +} + +static apr_int64_t ra_days_from_civil(apr_int64_t y, int m, int d) +{ + apr_int64_t era, yoe, doy; + + y -= m <= 2; + era = (y >= 0 ? y : y - 399) / 400; + yoe = y - era * 400; + doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1; + return era * 146097 + yoe * 365 + yoe / 4 - yoe / 100 + doy - 719468; +} + +static int ra_date_ok(int year, int mon, int mday) +{ + static const int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + int leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; + + return mday >= 1 && mday <= days[mon] + (mon == 1 && leap); +} + +/* The year of t, using the proleptic Gregorian calendar. */ +static apr_int64_t ra_year_of(apr_time_t t) +{ + apr_int64_t secs = t / APR_USEC_PER_SEC; + apr_int64_t days, era, doe, yoe, doy; + + if (t % APR_USEC_PER_SEC < 0) { + --secs; + } + days = secs / 86400; + if (secs % 86400 < 0) { + --days; + } + days += 719468; + era = (days >= 0 ? days : days - 146096) / 146097; + doe = days - era * 146097; + yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; + doy = doe - (365 * yoe + yoe / 4 - yoe / 100); + return yoe + era * 400 + ((5 * doy + 2) / 153 >= 10); +} + +static int ra_http_date(const char *s, apr_size_t len, apr_time_t now, + apr_time_t *when) +{ + int year, mon, mday, h, m, sec; + const char *tod; + apr_size_t comma; + + if (len == 29 && s[3] == ',') { + /* IMF-fixdate: Sun, 06 Nov 1994 08:49:37 GMT */ + if (!ra_day_name(s, 3) || s[4] != ' ' || s[7] != ' ' || s[11] != ' ' + || s[16] != ' ' || memcmp(s + 25, " GMT", 4)) { + return 0; + } + mday = ra_digits(s + 5, 2); + mon = ra_month(s + 8); + year = ra_digits(s + 12, 4); + tod = s + 17; + } + else if (len == 24 && s[3] == ' ') { + /* asctime-date: Sun Nov 6 08:49:37 1994 */ + if (!ra_day_name(s, 3) || s[7] != ' ' || s[10] != ' ' || s[19] != ' ') { + return 0; + } + mon = ra_month(s + 4); + mday = s[8] == ' ' ? ra_digits(s + 9, 1) : ra_digits(s + 8, 2); + year = ra_digits(s + 20, 4); + tod = s + 11; + } + else { + /* rfc850-date: Sunday, 06-Nov-94 08:49:37 GMT */ + for (comma = 6; comma <= 9 && comma < len && s[comma] != ','; ++comma) + ; + if (comma > 9 || len != comma + 24 || !ra_day_name(s, comma) + || s[comma + 1] != ' ' || s[comma + 4] != '-' || s[comma + 8] != '-' + || s[comma + 11] != ' ' || memcmp(s + comma + 20, " GMT", 4)) { + return 0; + } + mday = ra_digits(s + comma + 2, 2); + mon = ra_month(s + comma + 5); + year = ra_digits(s + comma + 9, 2); + tod = s + comma + 12; + if (year >= 0) { + /* two digit year: not more than 50 years in the future */ + apr_int64_t now_year = ra_year_of(now); + + /* keep the arithmetic below in range for any clock */ + if (now_year < 1970) { + now_year = 1970; + } + else if (now_year > 9999) { + now_year = 9999; + } + year += now_year / 100 * 100; + while (year + 100 <= now_year + 50) { + year += 100; + } + while (year > now_year + 50) { + year -= 100; + } + } + } + if (year < 0 || mon < 0 || mday < 0 || !ra_date_ok(year, mon, mday) + || !ra_time_of_day(tod, &h, &m, &sec)) { + return 0; + } + *when = apr_time_from_sec(ra_days_from_civil(year, mon + 1, mday) * 86400 + + h * 3600 + m * 60 + sec); + return 1; +} + +static const char *ra_trim(const char *s, apr_size_t *len) +{ + while (*s == ' ' || *s == '\t') { + ++s; + } + *len = strlen(s); + while (*len && (s[*len - 1] == ' ' || s[*len - 1] == '\t')) { + --*len; + } + return s; +} + +apr_status_t md_util_parse_retry_after(apr_interval_time_t *delay, + const char *value, const char *date, + apr_time_t now) +{ + apr_size_t len, dlen; + apr_time_t when, ref = now; + + if (!value) { + return APR_EINVAL; + } + value = ra_trim(value, &len); + if (!len) { + return APR_EINVAL; + } + + if (*value >= '0' && *value <= '9') { + const apr_int64_t max = APR_INT64_MAX / APR_USEC_PER_SEC; + apr_int64_t secs = 0; + apr_size_t i; + + for (i = 0; i < len; ++i) { + if (value[i] < '0' || value[i] > '9') { + return APR_EINVAL; + } + if (secs <= (max - (value[i] - '0')) / 10) { + secs = secs * 10 + (value[i] - '0'); + } + else { + secs = max + 1; + } + } + *delay = secs > max ? APR_INT64_MAX : apr_time_from_sec(secs); + return APR_SUCCESS; + } + + if (!ra_http_date(value, len, now, &when)) { + return APR_EINVAL; + } + if (date) { + date = ra_trim(date, &dlen); + if (!ra_http_date(date, dlen, now, &ref)) { + ref = now; + } + } + if (when <= ref) { + *delay = 0; + } + else if (ref < 0 && when > APR_INT64_MAX + ref) { + *delay = APR_INT64_MAX; + } + else { + *delay = when - ref; + } + return APR_SUCCESS; +} + /* try and retry for a while **********************************************************************/ +static md_util_interrupt_fn *interrupt_fn; +static void *interrupt_baton; + +void md_util_interrupt_set(md_util_interrupt_fn *fn, void *baton) +{ + interrupt_fn = fn; + interrupt_baton = baton; +} + +static int interrupted(void) +{ + return interrupt_fn && interrupt_fn(interrupt_baton); +} + +/* Sleep in slices, so a long Retry-After does not delay a shutdown. */ +static apr_status_t interruptible_sleep(apr_interval_time_t duration) +{ + const apr_interval_time_t slice = apr_time_from_msec(100); + + while (duration > 0) { + if (interrupted()) { + return APR_EINTR; + } + apr_sleep(duration < slice? duration : slice); + duration -= slice; + } + return interrupted()? APR_EINTR : APR_SUCCESS; +} + +static apr_status_t try_loop(md_util_try_fn *fn, void *baton, int ignore_errs, + apr_interval_time_t timeout, + apr_interval_time_t start_delay, + apr_interval_time_t max_delay, int backoff, + apr_time_t *retry_at, int interruptible); + apr_status_t md_util_try(md_util_try_fn *fn, void *baton, int ignore_errs, apr_interval_time_t timeout, apr_interval_time_t start_delay, apr_interval_time_t max_delay, int backoff) +{ + return try_loop(fn, baton, ignore_errs, timeout, start_delay, max_delay, + backoff, NULL, 0); +} + +apr_status_t md_util_try_with_retry_after(md_util_try_fn *fn, void *baton, + int ignore_errs, + apr_interval_time_t timeout, + apr_interval_time_t start_delay, + apr_interval_time_t max_delay, + int backoff, apr_time_t *retry_at) +{ + return try_loop(fn, baton, ignore_errs, timeout, start_delay, max_delay, + backoff, retry_at, 1); +} + +static apr_status_t try_loop(md_util_try_fn *fn, void *baton, int ignore_errs, + apr_interval_time_t timeout, + apr_interval_time_t start_delay, + apr_interval_time_t max_delay, int backoff, + apr_time_t *retry_at, int interruptible) { apr_status_t rv; apr_time_t now = apr_time_now(); @@ -1050,21 +1343,40 @@ apr_status_t md_util_try(md_util_try_fn *fn, void *baton, int ignore_errs, apr_interval_time_t nap_max = max_delay? max_delay : apr_time_from_sec(10); apr_interval_time_t left; int i = 0; - + while (1) { + /* A hint beyond the deadline is waited for only until the deadline, so + * that a server cannot make us give up before the timeout is over. */ + if (retry_at && *retry_at > now) { + apr_interval_time_t wait = (*retry_at < giveup? *retry_at : giveup) - now; + + if (interruptible) { + if (APR_SUCCESS != (rv = interruptible_sleep(wait))) break; + } + else { + apr_sleep(wait); + } + } + else if (interruptible && interrupted()) { + rv = APR_EINTR; + break; + } if (APR_SUCCESS == (rv = fn(baton, i++))) { break; } else if (!APR_STATUS_IS_EAGAIN(rv) && !ignore_errs) { break; } - + now = apr_time_now(); if (now > giveup) { rv = APR_TIMEUP; break; } - + if (retry_at && *retry_at > now && now < giveup) { + continue; + } + left = giveup - now; if (nap_duration > left) { nap_duration = left; @@ -1072,11 +1384,17 @@ apr_status_t md_util_try(md_util_try_fn *fn, void *baton, int ignore_errs, if (nap_duration > nap_max) { nap_duration = nap_max; } - - apr_sleep(nap_duration); + + if (interruptible) { + if (APR_SUCCESS != (rv = interruptible_sleep(nap_duration))) break; + } + else { + apr_sleep(nap_duration); + } if (backoff) { nap_duration *= 2; - } + } + now = apr_time_now(); } return rv; } diff --git a/modules/md/md_util.h b/modules/md/md_util.h index cf43964ebb2..8c147345f80 100644 --- a/modules/md/md_util.h +++ b/modules/md/md_util.h @@ -256,6 +256,33 @@ const char *md_link_find_relation(const struct apr_table_t *headers, apr_pool_t *pool, const char *relation); const char *md_util_parse_ct(apr_pool_t *pool, const char *cth); + +/** + * Parse the value of a Retry-After header field (RFC 9110, section 10.2.3), + * which is either a number of seconds or an HTTP-date. Leading and trailing + * whitespace is ignored. Anything else, such as a sign, a fraction, a list + * of values, a time zone other than GMT or text after the date, is invalid. + * An HTTP-date is accepted in the three formats of RFC 9110, section 5.6.7. + * The day name is checked for its syntax only. A delay too large for an + * apr_interval_time_t is saturated to APR_INT64_MAX. + * + * An HTTP-date is relative to the clock of the server that sent it. To + * be independent of any difference to the local clock, the delay is computed + * against the Date header field of the same response, if given. + * + * This has no httpd dependency, so a2md can call it too. + * @param delay The parsed delay, never negative + * @param value The field value to parse + * @param date The value of the Date header field of the response, or NULL. + * It is ignored if it is not a valid HTTP-date. + * @param now The local time to measure an HTTP-date against if date is not + * usable, also to resolve a two digit year + * @return APR_SUCCESS, or APR_EINVAL if value is not valid + */ +apr_status_t md_util_parse_retry_after(apr_interval_time_t *delay, + const char *value, const char *date, + apr_time_t now); + /**************************************************************************************************/ /* retry logic */ @@ -265,4 +292,31 @@ apr_status_t md_util_try(md_util_try_fn *fn, void *baton, int ignore_errs, apr_interval_time_t timeout, apr_interval_time_t start_delay, apr_interval_time_t max_delay, int backoff); +/** + * Callback to tell md_util_try_with_retry_after() to stop waiting, e.g. + * because the process is shutting down. Return non-zero to cancel. + */ +typedef int md_util_interrupt_fn(void *baton); + +/** + * Register the callback polled by md_util_try_with_retry_after() while it + * waits. Not set by default, so a waiting a2md is not interrupted. + */ +void md_util_interrupt_set(md_util_interrupt_fn *fn, void *baton); + +/** + * Like md_util_try(), but honours a server given Retry-After. *retry_at is + * the local time before which fn should not be called again. It is read + * before every call, so fn may set it, and is waited for at most until the + * timeout, then fn is called a last time. May be NULL. The waits end early + * and APR_EINTR is returned, without calling fn again, when the callback + * set by md_util_interrupt_set() asks for it. + */ +apr_status_t md_util_try_with_retry_after(md_util_try_fn *fn, void *baton, + int ignore_errs, + apr_interval_time_t timeout, + apr_interval_time_t start_delay, + apr_interval_time_t max_delay, + int backoff, apr_time_t *retry_at); + #endif /* md_util_h */ diff --git a/modules/md/mod_md_drive.c b/modules/md/mod_md_drive.c index c5d710817c9..281134c200c 100644 --- a/modules/md/mod_md_drive.c +++ b/modules/md/mod_md_drive.c @@ -26,6 +26,7 @@ #include #include +#include "ap_mpm.h" #include "mod_watchdog.h" #include "md.h" @@ -69,6 +70,15 @@ struct md_renew_ctx_t { apr_array_header_t *jobs; }; +static int server_is_stopping(void *baton) +{ + int state; + + (void)baton; + return APR_SUCCESS == ap_mpm_query(AP_MPMQ_MPM_STATE, &state) + && AP_MPMQ_STOPPING == state; +} + static void process_drive_job(md_renew_ctx_t *dctx, md_job_t *job, apr_pool_t *ptemp) { const md_t *md; @@ -203,6 +213,15 @@ static void process_drive_job(md_renew_ctx_t *dctx, md_job_t *job, apr_pool_t *p md_job_notify(job, "renewed", result); } } + else if (APR_STATUS_IS_EINTR(result->status)) { + /* Not an error, so neither logged nor reported as one. Leave the + * last result in the job as it was. */ + ap_log_error(APLOG_MARK, APLOG_TRACE1, 0, dctx->s, + "md(%s): renewal interrupted by shutdown", job->mdomain); + if (job->last_result) md_result_assign(result, job->last_result); + else md_result_set(result, APR_SUCCESS, NULL); + goto leave; + } else { ap_log_error( APLOG_MARK, APLOG_ERR, result->status, dctx->s, APLOGNO(10056) "processing %s: %s", job->mdomain, result->detail); @@ -264,6 +283,7 @@ static apr_status_t run_watchdog(int state, void *baton, apr_pool_t *ptemp) */ switch (state) { case AP_WATCHDOG_STATE_STARTING: + md_util_interrupt_set(server_is_stopping, NULL); ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, dctx->s, APLOGNO(10054) "md watchdog start, auto drive %d mds", dctx->jobs->nelts); break;