bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainenauth_request_var_expand_static_tab[AUTH_REQUEST_VAR_TAB_COUNT+1] = {
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen /* be sure to update AUTH_REQUEST_VAR_TAB_COUNT */
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainenstatic const char *
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen const struct auth_request *request ATTR_UNUSED)
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen const struct auth_request *request ATTR_UNUSED)
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainenauth_request_get_var_expand_table_full(const struct auth_request *auth_request,
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen unsigned int *count)
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen N_ELEMENTS(auth_request_var_expand_static_tab);
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen /* keep the extra fields at the beginning. the last static_tab field
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen contains the ending NULL-fields. */
e7d0bea63a08b08c47c4b5c187d2cb7127859657Timo Sirainen tab = ret_tab = t_new(struct var_expand_table,
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen memcpy(tab, auth_request_var_expand_static_tab,
99abb1302ae693ccdfe0d57351fd42c67a8612fcTimo Sirainen username = auth_request->user != NULL ? auth_request->user : "";
99abb1302ae693ccdfe0d57351fd42c67a8612fcTimo Sirainen tab[0].value = escape_func(username, auth_request);
99abb1302ae693ccdfe0d57351fd42c67a8612fcTimo Sirainen tab[1].value = escape_func(t_strcut(username, '@'),
99abb1302ae693ccdfe0d57351fd42c67a8612fcTimo Sirainen tab[2].value = i_strchr_to_next(username, '@');
4b1781e4c64be52e25b5994e5242dbe696cc7d29Timo Sirainen tab[2].value = escape_func(tab[2].value, auth_request);
de2cba085b9b231135be953d7f34f74fefb82725Timo Sirainen tab[3].value = escape_func(auth_request->service, auth_request);
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen /* tab[4] = we have no home dir */
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[5].value = net_ip2addr(&auth_request->local_ip);
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[6].value = net_ip2addr(&auth_request->remote_ip);
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[7].value = dec2str(auth_request->client_pid);
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[8].value = escape_func(auth_request->mech_password,
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[9].value = auth_request->userdb == NULL ? "" :
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[9].value = auth_request->passdb == NULL ? "" :
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[10].value = auth_request->mech_name == NULL ? "" :
de2cba085b9b231135be953d7f34f74fefb82725Timo Sirainen escape_func(auth_request->mech_name, auth_request);
1eedfce593fd29b5a5dee8d8ae3d82ab63d99cbfAki Tuomi case AUTH_REQUEST_SECURED_NONE: tab[11].value = ""; break;
1eedfce593fd29b5a5dee8d8ae3d82ab63d99cbfAki Tuomi case AUTH_REQUEST_SECURED: tab[11].value = "secured"; break;
1eedfce593fd29b5a5dee8d8ae3d82ab63d99cbfAki Tuomi case AUTH_REQUEST_SECURED_TLS: tab[11].value = "TLS"; break;
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[12].value = dec2str(auth_request->local_port);
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[13].value = dec2str(auth_request->remote_port);
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[14].value = auth_request->valid_client_cert ? "valid" : "";
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen if (auth_request->requested_login_user != NULL) {
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen const char *login_user = auth_request->requested_login_user;
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[15].value = escape_func(login_user, auth_request);
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[16].value = escape_func(t_strcut(login_user, '@'),
4b1781e4c64be52e25b5994e5242dbe696cc7d29Timo Sirainen tab[17].value = i_strchr_to_next(login_user, '@');
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[18].value = auth_request->session_id == NULL ? NULL :
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen escape_func(auth_request->session_id, auth_request);
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[19].value = net_ip2addr(&auth_request->real_local_ip);
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[20].value = net_ip2addr(&auth_request->real_remote_ip);
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[21].value = dec2str(auth_request->real_local_port);
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[22].value = dec2str(auth_request->real_remote_port);
99abb1302ae693ccdfe0d57351fd42c67a8612fcTimo Sirainen tab[23].value = i_strchr_to_next(username, '@');
4b1781e4c64be52e25b5994e5242dbe696cc7d29Timo Sirainen tab[23].value = escape_func(t_strcut(tab[23].value, '@'),
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[24].value = escape_func(tab[24].value+1, auth_request);
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[25].value = auth_request->master_user == NULL ? NULL :
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen escape_func(auth_request->master_user, auth_request);
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[26].value = auth_request->session_pid == (pid_t)-1 ? NULL :
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen orig_user = auth_request->original_username != NULL ?
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[27].value = escape_func(orig_user, auth_request);
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen tab[28].value = escape_func(t_strcut(orig_user, '@'), auth_request);
4b1781e4c64be52e25b5994e5242dbe696cc7d29Timo Sirainen tab[29].value = i_strchr_to_next(orig_user, '@');
4b1781e4c64be52e25b5994e5242dbe696cc7d29Timo Sirainen tab[29].value = escape_func(tab[29].value, auth_request);
cc7699162ad3f1eaa95997b95b299610ca07158dTimo Sirainen tab[30].value = escape_func(auth_user, auth_request);
cc7699162ad3f1eaa95997b95b299610ca07158dTimo Sirainen tab[31].value = escape_func(t_strcut(auth_user, '@'), auth_request);
4b1781e4c64be52e25b5994e5242dbe696cc7d29Timo Sirainen tab[32].value = i_strchr_to_next(auth_user, '@');
4b1781e4c64be52e25b5994e5242dbe696cc7d29Timo Sirainen tab[32].value = escape_func(tab[32].value, auth_request);
fe791e96fdf796f7d8997ee0515b163dc5eddd72Aki Tuomi tab[33].value = escape_func(auth_request->local_name, auth_request);
e88c6613d3bc78fa928b0e74f8e4ebd91151206eAki Tuomi tab[34].value = escape_func(auth_request->client_id, auth_request);
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainenauth_request_get_var_expand_table(const struct auth_request *auth_request,
4b7957c5e995f2c1820891d77a292a4886d52a43Timo Sirainen return auth_request_get_var_expand_table_full(auth_request, escape_func,
38ceb710e2bc957a66a75c68957cb87746682a75Timo Sirainenstatic const char *field_get_default(const char *data)
38ceb710e2bc957a66a75c68957cb87746682a75Timo Sirainen const char *p;
38ceb710e2bc957a66a75c68957cb87746682a75Timo Sirainen /* default value given */
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainenauth_request_var_expand_func_passdb(const char *data, void *context,
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainen const char **value_r,
38ceb710e2bc957a66a75c68957cb87746682a75Timo Sirainen struct auth_request_var_expand_ctx *ctx = context;
38ceb710e2bc957a66a75c68957cb87746682a75Timo Sirainen value = auth_fields_find(ctx->auth_request->extra_fields, field_name);
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainen *value_r = ctx->escape_func(value != NULL ? value : field_get_default(data),
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainenauth_request_var_expand_func_userdb(const char *data, void *context,
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainen const char **value_r,
38ceb710e2bc957a66a75c68957cb87746682a75Timo Sirainen struct auth_request_var_expand_ctx *ctx = context;
38ceb710e2bc957a66a75c68957cb87746682a75Timo Sirainen value = ctx->auth_request->userdb_reply == NULL ? NULL :
38ceb710e2bc957a66a75c68957cb87746682a75Timo Sirainen auth_fields_find(ctx->auth_request->userdb_reply, field_name);
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainen *value_r = ctx->escape_func(value != NULL ? value : field_get_default(data),
38ceb710e2bc957a66a75c68957cb87746682a75Timo Sirainenconst struct var_expand_func_table auth_request_var_funcs_table[] = {
38ceb710e2bc957a66a75c68957cb87746682a75Timo Sirainen { "passdb", auth_request_var_expand_func_passdb },
38ceb710e2bc957a66a75c68957cb87746682a75Timo Sirainen { "userdb", auth_request_var_expand_func_userdb },
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainenint auth_request_var_expand(string_t *dest, const char *str,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen const char **error_r)
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen return auth_request_var_expand_with_table(dest, str, auth_request,
38ceb710e2bc957a66a75c68957cb87746682a75Timo Sirainen auth_request_get_var_expand_table(auth_request, escape_func),
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainenint auth_request_var_expand_with_table(string_t *dest, const char *str,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen const char **error_r)
c30bcc83aca4e0082c0d28abea72ec309984b06fTimo Sirainen ctx.escape_func = escape_func == NULL ? escape_none : escape_func;
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen return var_expand_with_funcs(dest, str, table,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen const struct auth_request *auth_request ATTR_UNUSED,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen auth_request_escape_func_t *escape_func ATTR_UNUSED,