Commit 08f69cc3 08f69cc36908f376659369a3c5ee2f63a75f1c73 by Sergey Poznyakoff

Improve URL matching code.

* libmailutils/base/url.c (mu_url_matches_ticket): Use different
weight values, depending on the missing part.
* libmailutils/base/wicket.c (mu_wicket_stream_match_url): Refuse
URLs that lack host and password parts.
1 parent eb3a07ec
...@@ -966,20 +966,25 @@ mu_url_decode (const char *s) ...@@ -966,20 +966,25 @@ mu_url_decode (const char *s)
966 966
967 #define is_wildcard(s) ((s)[0] == '*' && s[1] == 0) 967 #define is_wildcard(s) ((s)[0] == '*' && s[1] == 0)
968 968
969 #define WEIGHT_SCHEME 3
970 #define WEIGHT_USER 4
971 #define WEIGHT_HOST 2
972 #define WEIGHT_PORT 1
973
969 int 974 int
970 mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *pwc) 975 mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *pwc)
971 { 976 {
972 int wcnt = 0; 977 int wcnt = 0;
973 978
974 if (is_wildcard (ticket->scheme)) 979 if (is_wildcard (ticket->scheme))
975 wcnt++; 980 wcnt += WEIGHT_SCHEME;
976 else if (mu_c_strcasecmp (ticket->scheme, url->scheme)) 981 else if (mu_c_strcasecmp (ticket->scheme, url->scheme))
977 return 0; 982 return 0;
978 983
979 if (ticket->flags & MU_URL_HOST) 984 if (ticket->flags & MU_URL_HOST)
980 { 985 {
981 if (is_wildcard (ticket->host)) 986 if (is_wildcard (ticket->host))
982 wcnt++; 987 wcnt += WEIGHT_HOST;
983 else if (url->flags & MU_URL_HOST) 988 else if (url->flags & MU_URL_HOST)
984 { 989 {
985 if (mu_c_strcasecmp (ticket->host, url->host)) 990 if (mu_c_strcasecmp (ticket->host, url->host))
...@@ -990,7 +995,7 @@ mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *pwc) ...@@ -990,7 +995,7 @@ mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *pwc)
990 return 0; 995 return 0;
991 } 996 }
992 else 997 else
993 wcnt++; 998 wcnt += WEIGHT_HOST;
994 999
995 if (ticket->flags & MU_URL_PORT) 1000 if (ticket->flags & MU_URL_PORT)
996 { 1001 {
...@@ -1000,16 +1005,16 @@ mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *pwc) ...@@ -1000,16 +1005,16 @@ mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *pwc)
1000 if (ticket->port != url->port) 1005 if (ticket->port != url->port)
1001 return 0; 1006 return 0;
1002 else 1007 else
1003 wcnt++; 1008 wcnt += WEIGHT_PORT;
1004 } 1009 }
1005 } 1010 }
1006 else 1011 else
1007 wcnt++; 1012 wcnt += WEIGHT_PORT;
1008 1013
1009 if (ticket->flags & MU_URL_USER) 1014 if (ticket->flags & MU_URL_USER)
1010 { 1015 {
1011 if (is_wildcard (ticket->user)) 1016 if (is_wildcard (ticket->user))
1012 wcnt += 2; 1017 wcnt += WEIGHT_USER;
1013 1018
1014 /* If ticket has a user or pass, but url doesn't, that's OK, we were 1019 /* If ticket has a user or pass, but url doesn't, that's OK, we were
1015 looking for this info. But if url does have a user/pass, it 1020 looking for this info. But if url does have a user/pass, it
...@@ -1021,7 +1026,7 @@ mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *pwc) ...@@ -1021,7 +1026,7 @@ mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *pwc)
1021 } 1026 }
1022 } 1027 }
1023 else 1028 else
1024 wcnt++; 1029 wcnt += WEIGHT_USER;
1025 1030
1026 /* Guess it matches. */ 1031 /* Guess it matches. */
1027 if (pwc) 1032 if (pwc)
......
...@@ -284,6 +284,14 @@ mu_wicket_stream_match_url (mu_stream_t stream, struct mu_debug_locus *loc, ...@@ -284,6 +284,14 @@ mu_wicket_stream_match_url (mu_stream_t stream, struct mu_debug_locus *loc,
284 mu_url_destroy (&u); 284 mu_url_destroy (&u);
285 continue; 285 continue;
286 } 286 }
287
288 if (!mu_url_has_flag (u, MU_URL_USER|MU_URL_SECRET))
289 {
290 mu_error (_("%s:%u: URL is missing required parts"),
291 loc->file, loc->line);
292 mu_url_destroy (&u);
293 continue;
294 }
287 295
288 if (!mu_url_matches_ticket (u, url, &n)) 296 if (!mu_url_matches_ticket (u, url, &n))
289 { 297 {
......