Skip to content

Commit f4555f5

Browse files
Add support for subdomain checking in aliases and socket aliases
1 parent e02b99b commit f4555f5

File tree

6 files changed

+69
-17
lines changed

6 files changed

+69
-17
lines changed

cfg.lex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ CR \n
341341
342342
ANY "any"
343343
ANYCAST ("anycast"|"ANYCAST")
344+
SUBDOMAIN ("subdomain"|"SUBDOMAIN")
344345
FRAG ("frag"|"FRAG")
345346
REUSE_PORT ("reuse_port"|"REUSE_PORT")
346347
@@ -615,6 +616,7 @@ SPACE [ ]
615616
<INITIAL>{CR} { count();/* return CR;*/ }
616617
<INITIAL>{ANY} { count(); return ANY; }
617618
<INITIAL>{ANYCAST} { count(); return ANYCAST; }
619+
<INITIAL>{SUBDOMAIN} { count(); return SUBDOMAIN; }
618620
<INITIAL>{REUSE_PORT} { count(); return REUSE_PORT; }
619621
<INITIAL>{FRAG} { count(); return FRAG; }
620622
<INITIAL>{SLASH} { count(); return SLASH; }

cfg.y

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ struct listen_param {
185185
char *auto_scaling_profile;
186186
} p_tmp;
187187
static void fill_socket_id(struct listen_param *param, struct socket_id *s);
188+
static void fill_alias_socket(struct listen_param *param, struct socket_id *s);
188189

189190
union route_name_var {
190191
int iname;
@@ -466,6 +467,7 @@ extern int cfg_parse_only_routes;
466467
%token COLON
467468
%token ANY
468469
%token ANYCAST
470+
%token SUBDOMAIN
469471
%token FRAG
470472
%token REUSE_PORT
471473
%token SCRIPTVARERR
@@ -491,6 +493,7 @@ extern int cfg_parse_only_routes;
491493
%type <sockid> socket_def
492494
%type <sockid> id_lst
493495
%type <sockid> alias_def
496+
%type <sockid> any_alias
494497
%type <sockid> listen_id_def
495498
%type <sockid> phostport phostportrange
496499
%type <intval> proto port any_proto
@@ -694,7 +697,7 @@ phostportrange: proto COLON MULT { IFOR();
694697
}
695698
;
696699

697-
alias_def: listen_id { IFOR();
700+
any_alias: listen_id { IFOR();
698701
$$=mk_listen_id($1, PROTO_NONE, 0); }
699702
| ANY COLON listen_id { IFOR();
700703
$$=mk_listen_id($3, PROTO_NONE, 0); }
@@ -707,6 +710,23 @@ alias_def: listen_id { IFOR();
707710
| phostport
708711
;
709712

713+
id_lst_param: SUBDOMAIN { IFOR();
714+
p_tmp.flags |= SI_IS_SUBDOMAIN_ALIAS;
715+
}
716+
;
717+
718+
id_lst_params: id_lst_param
719+
| id_lst_param id_lst_params
720+
;
721+
722+
alias_def: any_alias { $$=$1; }
723+
| any_alias { IFOR();
724+
memset(&p_tmp, 0, sizeof(p_tmp));
725+
} id_lst_params { IFOR();
726+
$$=$1; fill_alias_socket(&p_tmp, $$);
727+
}
728+
;
729+
710730
id_lst: alias_def { IFOR(); $$=$1 ; }
711731
| alias_def id_lst { IFOR(); $$=$1; $$->next=$2; }
712732
;
@@ -730,6 +750,9 @@ socket_def_param: ANYCAST { IFOR();
730750
| REUSE_PORT { IFOR();
731751
p_tmp.flags |= SI_REUSEPORT;
732752
}
753+
| SUBDOMAIN { IFOR();
754+
p_tmp.flags |= SI_IS_SUBDOMAIN_ALIAS;
755+
}
733756
| USE_WORKERS NUMBER { IFOR();
734757
p_tmp.workers=$2;
735758
}
@@ -1484,7 +1507,7 @@ assign_stm: LOGLEVEL EQUAL snumber { IFOR();
14841507
| ALIAS EQUAL id_lst { IFOR();
14851508
for(lst_tmp=$3; lst_tmp; lst_tmp=lst_tmp->next)
14861509
add_alias(lst_tmp->name, strlen(lst_tmp->name),
1487-
lst_tmp->port, lst_tmp->proto);
1510+
lst_tmp->port, lst_tmp->proto, lst_tmp->flags);
14881511
}
14891512
| ALIAS EQUAL error { yyerror("hostname expected (use quotes"
14901513
" if the hostname includes config keywords)"); }
@@ -2771,6 +2794,10 @@ static void fill_socket_id(struct listen_param *param, struct socket_id *s)
27712794
}
27722795
}
27732796

2797+
static void fill_alias_socket(struct listen_param *param, struct socket_id *s) {
2798+
s->flags |= param->flags;
2799+
}
2800+
27742801
static struct multi_str *new_string(char *s)
27752802
{
27762803
struct multi_str *ms = pkg_malloc(sizeof(struct multi_str));

ip_addr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ union sockaddr_union{
8989

9090

9191
enum si_flags { SI_NONE=0, SI_IS_IP=1, SI_IS_LO=2, SI_IS_MCAST=4,
92-
SI_IS_ANYCAST=8, SI_FRAG=16, SI_REUSEPORT=32, SI_INTERNAL=64 };
92+
SI_IS_ANYCAST=8, SI_FRAG=16, SI_REUSEPORT=32, SI_INTERNAL=64, SI_IS_SUBDOMAIN_ALIAS=128 };
9393

9494
struct receive_info {
9595
struct ip_addr src_ip;

name_alias.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include <string.h>
2929
#include "name_alias.h"
3030

31-
3231
struct host_alias* aliases=0; /* name aliases list */
3332

3433
struct alias_function* alias_fcts = NULL;
@@ -40,7 +39,7 @@ struct alias_function* alias_fcts = NULL;
4039
* if proto==0, the alias will match all the protocols
4140
* returns 1 if a new alias was added, 0 if a matching alias was already on
4241
* the list and -1 on error */
43-
int add_alias(char* name, int len, unsigned short port, unsigned short proto)
42+
int add_alias(char* name, int len, unsigned short port, unsigned short proto, enum si_flags flags)
4443
{
4544
struct host_alias* a;
4645

@@ -63,6 +62,7 @@ int add_alias(char* name, int len, unsigned short port, unsigned short proto)
6362
a->alias.s[len]=0; /* null terminate for easier printing*/
6463
a->port=port;
6564
a->proto=proto;
65+
a->flags=flags;
6666
a->next=aliases;
6767
aliases=a;
6868
return 1;

name_alias.h

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <strings.h>
3434
#include "str.h"
3535
#include "dprint.h"
36+
#include "ip_addr.h"
3637
#include "mem/mem.h"
3738

3839

@@ -41,6 +42,7 @@ struct host_alias{
4142
str alias;
4243
unsigned short port;
4344
unsigned short proto;
45+
enum si_flags flags;
4446
struct host_alias* next;
4547
};
4648

@@ -65,17 +67,39 @@ static inline int grep_aliases(char* name, int len, unsigned short port,
6567
{
6668
struct host_alias* a;
6769
struct alias_function *af;
70+
char *name_to_compare, *alias_to_compare;
71+
int len_to_compare, index_offset;
6872

6973
if ((len>2)&&((*name)=='[')&&(name[len-1]==']')){
7074
/* ipv6 reference, skip [] */
7175
name++;
7276
len-=2;
7377
}
74-
for(a=aliases;a;a=a->next)
75-
if ((a->alias.len==len) && ((a->port==0) || (port==0) ||
76-
(a->port==port)) && ((a->proto==0) || (proto==0) ||
77-
(a->proto==proto)) && (strncasecmp(a->alias.s, name, len)==0))
78-
return 1;
78+
79+
for(a=aliases;a;a=a->next) {
80+
if (((a->port==0) || (port==0) || (a->port==port)) &&
81+
((a->proto==0) || (proto==0) || (a->proto==proto))) {
82+
/* Check if the alias is a subdomain alias and if so calculate the index offset to start the comparison
83+
* Given an alias my.domain.com or my.great.domain.com and a subdomain of domain.com the comparison should start at domain.com
84+
* a host of domain.com will also match, if the flag is not set then do a strict comparison
85+
*/
86+
if (a->flags & SI_IS_SUBDOMAIN_ALIAS) {
87+
index_offset = len - a->alias.len;
88+
if (index_offset < 0) // the host we're checking is a shorter len than the alias so no need to compare
89+
continue;
90+
91+
name_to_compare = name + index_offset;
92+
alias_to_compare = a->alias.s;
93+
94+
len_to_compare = a->alias.len;
95+
96+
if (strncasecmp(alias_to_compare, name_to_compare, len_to_compare)==0)
97+
return 1;
98+
} else if (len == a->alias.len && strncasecmp(a->alias.s, name, len)==0) {
99+
return 1;
100+
}
101+
}
102+
}
79103

80104
for( af=alias_fcts ; af ; af=af->next ) {
81105
if ( af->alias_f(name,len,port,proto)>0 )
@@ -84,9 +108,8 @@ static inline int grep_aliases(char* name, int len, unsigned short port,
84108
return 0;
85109
}
86110

87-
88111
/* adds an alias to the list (only if it isn't already there) */
89-
int add_alias(char* name, int len, unsigned short port, unsigned short proto);
112+
int add_alias(char* name, int len, unsigned short port, unsigned short proto, enum si_flags flags);
90113

91114
/* register a new function for detecting aliases */
92115
int register_alias_fct( is_alias_fct *fct );

socket_info.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ int fix_socket_list(struct socket_info_full **list)
668668
/* check if we got the official name */
669669
if (strcasecmp(he->h_name, si->name.s)!=0){
670670
if (auto_aliases && add_alias(si->name.s, si->name.len,
671-
si->port_no, si->proto)<0){
671+
si->port_no, si->proto, si->flags)<0){
672672
LM_ERR("add_alias failed\n");
673673
}
674674
/* change the official name */
@@ -684,7 +684,7 @@ int fix_socket_list(struct socket_info_full **list)
684684
/* add the aliases*/
685685
if (auto_aliases) {
686686
for(h=he->h_aliases; h && *h; h++)
687-
if (add_alias(*h, strlen(*h), si->port_no, si->proto)<0){
687+
if (add_alias(*h, strlen(*h), si->port_no, si->proto, si->flags)<0){
688688
LM_ERR("add_alias failed\n");
689689
}
690690
}
@@ -724,11 +724,11 @@ int fix_socket_list(struct socket_info_full **list)
724724
}else{
725725
/* add the aliases*/
726726
if (add_alias(he->h_name, strlen(he->h_name),
727-
si->port_no, si->proto)<0){
727+
si->port_no, si->proto, si->flags)<0){
728728
LM_ERR("add_alias failed\n");
729729
}
730730
for(h=he->h_aliases; h && *h; h++)
731-
if (add_alias(*h,strlen(*h),si->port_no,si->proto)<0){
731+
if (add_alias(*h,strlen(*h),si->port_no,si->proto, si->flags)<0){
732732
LM_ERR(" add_alias failed\n");
733733
}
734734
}
@@ -859,7 +859,7 @@ int fix_socket_list(struct socket_info_full **list)
859859
(sl->name.len!=si->name.len)||
860860
(strncmp(sl->name.s, si->name.s, si->name.len)!=0))
861861
)
862-
if (add_alias(sl->name.s,sl->name.len,sl->port_no,sl->proto)<0)
862+
if (add_alias(sl->name.s,sl->name.len,sl->port_no,sl->proto,sl->flags)<0)
863863
LM_ERR(" add_alias failed\n");
864864

865865
/* remove l*/

0 commit comments

Comments
 (0)