auth-policy.c revision e1d4f6f6ab3cedc30c7477820c394fbd3769964c
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen#define AUTH_POLICY_DNS_SOCKET_PATH "dns-client"
7384b4e78eaab44693c985192276e31322155e32Stephan Boschstatic struct http_client_settings http_client_set = {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch .dns_client_socket_path = AUTH_POLICY_DNS_SOCKET_PATH,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch const char *url;
9145c8b5eda526d05bd4a7ced20f6f6f2ff8df03Stephan Bosch const char *key;
7384b4e78eaab44693c985192276e31322155e32Stephan Boschint auth_policy_attribute_comparator(const struct policy_template_keyvalue *a,
7384b4e78eaab44693c985192276e31322155e32Stephan Boschint auth_policy_strptrcmp(const char *a0, const char *a1,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return memcmp(a0, b0, I_MIN((a1-a0),(b1-b0)));
7384b4e78eaab44693c985192276e31322155e32Stephan Boschvoid auth_policy_open_key(const char *key, string_t *template)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch const char *ptr;
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Bosch json_append_escaped(template, t_strndup(key, (ptr-key)));
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Boschvoid auth_policy_close_key(const char *key, string_t *template)
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Bosch while((key = strchr(key, '/')) != NULL) { str_append_c(template,'}'); key++; }
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Boschvoid auth_policy_open_and_close_to_key(const char *fromkey, const char *tokey, string_t *template)
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Bosch if (fptr == NULL && tptr == NULL) return; /* nothing to do */
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch if (auth_policy_strptrcmp(fromkey, fptr, tokey, tptr) == 0) {
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch /* nothing to do, again */
e47c2f17d8136c4d972d1074a3f84ba2ecef4fdcStephan Bosch if (auth_policy_strptrcmp(fptr, fdash, tptr, tdash) != 0) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch http_client_set.request_absolute_timeout_msecs = global_auth_settings->policy_server_timeout_msecs;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch http_client = http_client_init(&http_client_set);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* prepare template */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch ARRAY(struct policy_template_keyvalue) attribute_pairs;
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch const char **ptr;
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch const char **list = t_strsplit_spaces(global_auth_settings->policy_request_attributes, "= ");
a991cfe2157e58ee43bc580f517ce9ef0dfb7acfStephan Bosch i_fatal("auth_policy_request_attributes contains invalid value");
16eb9a737d42017fc875ef7b68afc25c3c9f8979Stephan Bosch /* then we sort it */
16eb9a737d42017fc875ef7b68afc25c3c9f8979Stephan Bosch array_sort(&attribute_pairs, auth_policy_attribute_comparator);
fb1be3de0159d6a10e916ad992e2bc53be64c6d5Timo Sirainen /* and build a template string */
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen auth_policy_open_and_close_to_key(prevkey, kvptr->key, template);
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen json_append_escaped(template, (kptr != NULL?kptr+1:kvptr->key));
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch auth_policy_open_and_close_to_key(prevkey, "", template);
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Bosch auth_policy_json_template = i_strdup(str_c(template));
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch (void)json_parser_deinit(&(context->parser), &error);
49287618521ff2c69385456de116e5d1581426c0Timo Sirainen http_client_request_abort(&(context->http_request));
30f35cf5d1e1374d7fab4231e86144fc106a8e79Stephan Boschvoid auth_policy_parse_response(struct policy_lookup_ctx *context)
87c121a4c05b9cee46f1f757ec6999d441519abfStephan Bosch while((ret = json_parse_next(context->parser, &type, &value)) == 1) {
87c121a4c05b9cee46f1f757ec6999d441519abfStephan Bosch context->parse_state = POLICY_RESULT_VALUE_STATUS;
87c121a4c05b9cee46f1f757ec6999d441519abfStephan Bosch context->parse_state = POLICY_RESULT_VALUE_MESSAGE;
87c121a4c05b9cee46f1f757ec6999d441519abfStephan Bosch } else if (context->parse_state == POLICY_RESULT_VALUE_STATUS) {
87c121a4c05b9cee46f1f757ec6999d441519abfStephan Bosch if (type != JSON_TYPE_NUMBER || str_to_int(value, &(context->result)) != 0)
87c121a4c05b9cee46f1f757ec6999d441519abfStephan Bosch } else if (context->parse_state == POLICY_RESULT_VALUE_MESSAGE) {
4c4c4a740bbb1b674d4b0dae009d1919f8ad96b7Stephan Bosch context->message = p_strdup(context->pool, value);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch auth_request_log_error(context->request, "policy",
84740b03d3ee9e96a2e446a54729188764c99292Timo Sirainen "Error reading policy server result: %s",
84740b03d3ee9e96a2e446a54729188764c99292Timo Sirainen } else if (ret == 0 && context->payload->eof) {
84740b03d3ee9e96a2e446a54729188764c99292Timo Sirainen auth_request_log_error(context->request, "policy",
84740b03d3ee9e96a2e446a54729188764c99292Timo Sirainen "Policy server result was too short");
84740b03d3ee9e96a2e446a54729188764c99292Timo Sirainen auth_request_log_error(context->request, "policy",
84740b03d3ee9e96a2e446a54729188764c99292Timo Sirainen "Policy server response was malformed");
84740b03d3ee9e96a2e446a54729188764c99292Timo Sirainen if (json_parser_deinit(&(context->parser), &error) != 0)
84740b03d3ee9e96a2e446a54729188764c99292Timo Sirainen auth_request_log_error(context->request, "policy",
84740b03d3ee9e96a2e446a54729188764c99292Timo Sirainen "Policy server response JSON parse error: %s", error);
84740b03d3ee9e96a2e446a54729188764c99292Timo Sirainen else if (context->parse_state == POLICY_RESULT)
84740b03d3ee9e96a2e446a54729188764c99292Timo Sirainen context->result = (context->set->policy_reject_on_fail ? -1 : 0);
84740b03d3ee9e96a2e446a54729188764c99292Timo Sirainen /* set message here */
84740b03d3ee9e96a2e446a54729188764c99292Timo Sirainen auth_request_log_debug(context->request, "policy",
6d573191bea1a64d6046be070487a5705a2d0204Stephan Bosch "Policy response %d with message: %s",
6d573191bea1a64d6046be070487a5705a2d0204Stephan Bosch auth_request_set_field(context->request, "reason", context->message, NULL);
6d573191bea1a64d6046be070487a5705a2d0204Stephan Bosch auth_request_log_debug(context->request, "policy",
4521d35c263add6af3f1ae55b3760291767ce50cTimo Sirainen if (context->request->policy_refusal == TRUE && context->set->verbose == TRUE) {
4521d35c263add6af3f1ae55b3760291767ce50cTimo Sirainen auth_request_log_info(context->request, "policy", "Authentication failure due to policy server refusal%s%s",
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen (context->message!=NULL?context->message:""));
4521d35c263add6af3f1ae55b3760291767ce50cTimo Sirainen context->callback(context->result, context->callback_context);
4521d35c263add6af3f1ae55b3760291767ce50cTimo Sirainenvoid auth_policy_process_response(const struct http_response *response,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch auth_request_log_error(context->request, "policy",
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "Policy server HTTP error: %d %s", response->status, response->reason);
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch context->callback(context->result, context->callback_context);
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch auth_request_log_error(context->request, "policy",
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch "Policy server result was empty");
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch context->callback(context->result, context->callback_context);
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch context->io = io_add_istream(response->payload, auth_policy_parse_response, context);
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch context->parser = json_parser_init(response->payload);
30f35cf5d1e1374d7fab4231e86144fc106a8e79Stephan Bosch auth_request_log_debug(context->request, "policy",
30f35cf5d1e1374d7fab4231e86144fc106a8e79Stephan Bosch context->callback(context->result, context->callback_context);
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Boschvoid auth_policy_send_request(struct policy_lookup_ctx *context)
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Bosch if (http_url_parse(context->url, NULL, HTTP_URL_ALLOW_USERINFO_PART,
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Bosch auth_request_log_error(context->request, "policy",
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Bosch "Could not parse url %s: %s", context->url, error);
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Bosch context->http_request = http_client_request_url(http_client,
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Bosch "POST", url, auth_policy_process_response, (void*)context);
de0181258ab66b527ad8dc7e51a8efa76b4658d0Stephan Bosch http_client_request_set_destroy_callback(context->http_request, auth_policy_finish, context);
de0181258ab66b527ad8dc7e51a8efa76b4658d0Stephan Bosch http_client_request_add_header(context->http_request, "Content-Type", "application/json");
de0181258ab66b527ad8dc7e51a8efa76b4658d0Stephan Bosch if (*context->set->policy_server_api_header != 0) {
de0181258ab66b527ad8dc7e51a8efa76b4658d0Stephan Bosch const char *ptr;
de0181258ab66b527ad8dc7e51a8efa76b4658d0Stephan Bosch if ((ptr = strstr(context->set->policy_server_api_header, ":")) != NULL) {
de0181258ab66b527ad8dc7e51a8efa76b4658d0Stephan Bosch const char *header = t_strcut(context->set->policy_server_api_header, ':');
de0181258ab66b527ad8dc7e51a8efa76b4658d0Stephan Bosch http_client_request_add_header(context->http_request, header, ptr + 1);
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Bosch http_client_request_add_header(context->http_request,
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Bosch "X-API-Key", context->set->policy_server_api_header);
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Bosch /* allow empty password */
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Bosch http_client_request_set_auth_simple(context->http_request, url->user,
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Bosch (url->password != NULL ? url->password : ""));
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Bosch struct istream *is = i_stream_create_from_buffer(context->json);
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Bosch http_client_request_set_payload(context->http_request, is, FALSE);
19db4c57fd7acc9e54e5724ccfa0633a5665dfefTimo Sirainen http_client_request_submit(context->http_request);
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Boschconst char *auth_policy_escape_function(const char *string,
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Bosch const struct auth_request *auth_request ATTR_UNUSED)
b2a3fbfe1b436123bbe1849eeeef9bb0c28b1f90Timo Sirainenconst struct var_expand_table *policy_get_var_expand_table(struct auth_request *auth_request,
1d048c5050f03c24251e5af8087e640de21b2d62Timo Sirainen table = auth_request_get_var_expand_table_full(auth_request, auth_policy_escape_function,
6ee9ce5ed955a1283dc22ad28980bf9cc23d4c4eStephan Bosch table[0].value = auth_policy_escape_function(table[0].value, auth_request);
6ee9ce5ed955a1283dc22ad28980bf9cc23d4c4eStephan Boschvoid auth_policy_create_json(struct policy_lookup_ctx *context,
6ee9ce5ed955a1283dc22ad28980bf9cc23d4c4eStephan Bosch unsigned char *ptr;
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch const struct hash_method *digest = hash_method_lookup(context->set->policy_hash_mech);
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch void *ctx = t_malloc_no0(digest->context_size);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* use +1 to make sure \0 gets included */
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Bosch digest->loop(ctx, context->request->user, strlen(context->request->user) + 1);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch digest->loop(ctx, password, strlen(password));
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch ptr = (unsigned char*)str_c_modifiable(buffer);
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch buffer_truncate_rshift_bits(buffer, context->set->policy_hash_truncate);
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch const char *hashed_password = binary_to_hex(str_data(buffer), str_len(buffer));
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch var_table = policy_get_var_expand_table(context->request, hashed_password);
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch if (auth_request_var_expand_with_table(context->json, auth_policy_json_template,
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Bosch auth_request_log_error(context->request, "policy",
e47c2f17d8136c4d972d1074a3f84ba2ecef4fdcStephan Bosch "Failed to expand auth policy template: %s", error);
e47c2f17d8136c4d972d1074a3f84ba2ecef4fdcStephan Bosch if (!context->request->failed && context->request->successful &&
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Bosch str_append(context->json, ",\"policy_reject\":");
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Bosch str_append(context->json, context->request->policy_refusal ? "true" : "false");
6d573191bea1a64d6046be070487a5705a2d0204Stephan Bosch auth_request_log_debug(context->request, "policy",
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "Policy server request JSON: %s", str_c(context->json));
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Boschvoid auth_policy_url(struct policy_lookup_ctx *context, const char *command)
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Bosch size_t len = strlen(context->set->policy_server_url);
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch if (context->set->policy_server_url[len-1] == '&')
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch context->url = p_strdup_printf(context->pool, "%scommand=%s",
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch context->url = p_strdup_printf(context->pool, "%s?command=%s",
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Boschvoid auth_policy_check(struct auth_request *request, const char *password,
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch if (request->master != NULL || *(request->set->policy_server_url) == '\0') {
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch struct policy_lookup_ctx *ctx = p_new(request->pool, struct policy_lookup_ctx, 1);
eb325a5a90c1d2655e74972bde0de6a699d2c864Stephan Bosch ctx->result = (ctx->set->policy_reject_on_fail ? -1 : 0);
57962a937b214be3a131f78005509afaa26fe4bfTimo Sirainen auth_request_log_debug(request, "policy", "Policy request %s", ctx->url);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch auth_policy_create_json(ctx, password, FALSE);
31fa529029f35f65451fb1d119ed1d5435b62e46Timo Sirainenvoid auth_policy_report(struct auth_request *request)
6bc9fb43cc1ac24693d030a6cbfa43bc7cbc82cbTimo Sirainen if (*(request->set->policy_server_url) == '\0')
a991cfe2157e58ee43bc580f517ce9ef0dfb7acfStephan Bosch struct policy_lookup_ctx *ctx = p_new(request->pool, struct policy_lookup_ctx, 1);
791fb70b3255a11a91ce0c2dc3ae1460d4cf8459Timo Sirainen auth_request_log_debug(request, "policy", "Policy request %s", ctx->url);