{"id":1348,"date":"2019-07-11T14:23:09","date_gmt":"2019-07-11T09:23:09","guid":{"rendered":"http:\/\/alexeyka.zantsev.com\/?p=1348"},"modified":"2021-03-17T16:51:34","modified_gmt":"2021-03-17T11:51:34","slug":"opensips-filter-register-requests-based-on-username-2","status":"publish","type":"post","link":"https:\/\/alexeyka.zantsev.com\/?p=1348","title":{"rendered":"OpenSIPS: filter REGISTER requests based on username"},"content":{"rendered":"\n<p>Formerly we&#8217;ve learned how to <a rel=\"noreferrer noopener\" href=\"http:\/\/alexeyka.zantsev.com\/?p=1109\" target=\"_blank\">restrict access with permission.so module based on source IP addresses<\/a>. Today I&#8217;ll show how to restrict access to your OpenSIPS based on usernames, being registering.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">loadmodule \"permissions.so\" # no deps<br><br>... <br><br>if (is_method(\"REGISTER\")) {<br>  if(!allow_register(\"register\")) {<br>    sl_send_reply(403, \"Forbidden by permissions\");<br>    exit;<br>  } <\/pre>\n\n\n\n<p>&#8220;Deny all, but &#8230;&#8221; policy &#8211; we will allow registrations of explicitly defined usernames and drop anybody else.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">register.deny file:<br><br>ALL : ALL<br><br> <br>register.allow file:<br><br># this allows lexus, lexus2, lexus3 to register <br>\"^sip:lexus[23]?@alexeyka.zantsev.com\" : ALL <br><br># regexp seems to be CORRECT, but for some reason lexus2 and lexus3 can not register <br># \"^sip:lexus[\\d]?@alexeyka.zantsev.com\" : ALL<\/pre>\n\n\n\n<p>Have a look how it&#8217;s working! A good guy is being registered successfully:<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><a href=\"https:\/\/alexeyka.zantsev.com\/wp-content\/uploads\/2008\/02\/permissions_good_guy.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/alexeyka.zantsev.com\/wp-content\/uploads\/2008\/02\/permissions_good_guy.png\" alt=\"\" class=\"wp-image-1334\" width=\"328\" height=\"92\"\/><\/a><figcaption>good guy<\/figcaption><\/figure>\n\n\n\n<p>While a bad guy had been kicked:<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><a href=\"https:\/\/alexeyka.zantsev.com\/wp-content\/uploads\/2008\/02\/permissions_bad_guy.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/alexeyka.zantsev.com\/wp-content\/uploads\/2008\/02\/permissions_bad_guy.png\" alt=\"\" class=\"wp-image-1340\" width=\"437\" height=\"43\"\/><\/a><figcaption>bad guy<\/figcaption><\/figure>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/opensips.org\/pipermail\/users\/2019-July\/041287.html\" target=\"_blank\">Another solution<\/a> using regex.so module from Pavel Eremin. The pros of this method is that it allows editing a txt file with usernames defined and <a rel=\"noreferrer noopener\" href=\"https:\/\/opensips.org\/html\/docs\/modules\/3.0.x\/regex.html#mi_regex_reload\" target=\"_blank\">reload regex.so module<\/a> via MI interface (no restart needed).<\/p>\n\n\n\n<p>And even <a href=\"https:\/\/opensips.org\/pipermail\/users\/2019-July\/041309.html\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"one more (opens in a new tab)\">one more<\/a> from R\u0103zvan Crainea.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Formerly we&#8217;ve learned how to restrict access with permission.so module based on source IP addresses. Today I&#8217;ll show how to restrict access to your OpenSIPS based on usernames, being registering. loadmodule &#8220;permissions.so&#8221; # no deps&#8230; if (is_method(&#8220;REGISTER&#8221;)) { if(!allow_register(&#8220;register&#8221;)) { sl_send_reply(403, &#8220;Forbidden by permissions&#8221;); exit; } &#8220;Deny all, but &#8230;&#8221; policy &#8211; we will allow [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[208,88],"class_list":["post-1348","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-opensips","tag-sip"],"_links":{"self":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts\/1348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1348"}],"version-history":[{"count":10,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts\/1348\/revisions"}],"predecessor-version":[{"id":5061,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts\/1348\/revisions\/5061"}],"wp:attachment":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}