[OpenSIPS-Users] uac_auth DB support

S.Rosenberg thesipguy at gmail.com
Sun Dec 17 17:23:54 UTC 2023


I ended up doing it the manually, I'm posting it so others who need it can
try my method,  I'm using cache_store in the startup route to avoid db
lookups for every call, I use an attribute in the the dr_gateways to
identify the row ID of the carrier in the registrar table I want to use.

In the startup_route I added the following, I will make a route to reload
this so I don't need to restart opensips whenever I add a carrier.

avp_db_query("select id, registrar, proxy, aor, third_party_registrant,
username, password, binding_URI, binding_params, expiry, forced_socket,
state from registrant","$avp(reg_id); $avp(reg_registrar); $avp(reg_proxy);
$avp(reg_aor); $avp(reg_third_party_registrant); $avp(reg_username);
$avp(reg_password); $avp(reg_binding_URI); $avp(reg_binding_params);
$avp(reg_expiry); $avp(reg_forced_socket); $avp(reg_state)");

 $var(i) = 0;

set_count($avp(reg_id),$var(reg_counter));

  while ($var(i) < $var(reg_counter)) {
         cache_store("local", "registrar_username_$(avp(reg_id)[$var(i)])",
"$(avp(reg_username)[$var(i)])");
         cache_store("local", "registrar_password_$(avp(reg_id)[$var(i)])",
"$(avp(reg_password)[$var(i)])");
  }

in the main route I run the following on the INVITE:

        if($(var(gw_attributes){s.substr,0,4})=="auth")    #if auth is set
in attributes it should be something like auth=10 where 10 is the id of the
row in the registrar table
        {
                $var(regAuthId)=$(var(gw_attributes){s.select,1,=});
 #pull the id from the attribute

cache_fetch("local","registrar_username_$var(regAuthId)",$var(registrar_username));

uac_replace_from("","sip:$var(registrar_username)@$socket_in(ip)");
 #replace the from field
                t_on_failure("dr");
                t_on_reply("dr");
        }

In my "dr" failure route I added the following
             if(t_check_status("407")){
                if($(avp(gw_attributes){s.substr,0,4})=="auth")
                {
                        $var(regAuthId)=$(avp(gw_attributes){s.select,1,=});

 cache_fetch("local","registrar_username_$var(regAuthId)",$var(registrar_username));

cache_fetch("local","registrar_password_$var(regAuthId)",$var(registrar_password));

                        if ($(<reply>hdr(Proxy-Authenticate)))
                        {
                                $var(raw_auth) =
$(<reply>hdr(Proxy-Authenticate));
                        }
                        if ($(<reply>hdr(WWW-Authenticate)))
                        {
                                $var(raw_auth) =
$(<reply>hdr(WWW-Authenticate));
                        }
                        $var(reg_start) = "/(.*?)realm=\"//g";
                        $var(reg_end) = "/\"(.*)//g";
                        $var(raw_auth) =
$(var(raw_auth){re.subst,$var(reg_start)});
                        $var(raw_auth) =
$(var(raw_auth){re.subst,$var(reg_end)});
                        $avp(uac_realm)=$var(raw_auth);
                        $avp(uac_username)=$var(registrar_username);
                        $avp(uac_password)=$var(registrar_password);
                        uac_auth();
                        if(!t_relay()){
                                t_reply (503, "Service not available");
                        }
                }
        }




On Wed, Dec 6, 2023 at 9:18 AM S.Rosenberg <thesipguy at gmail.com> wrote:

> Hi, I'm using the uac_auth to send calls to carriers that require
> username/password credentials, until now I only had a handful of carriers
> that don't support IP authentication so I hard coded the credentials, I was
> thinking of pulling it from the  registrant table as most of the time I
> would want to register to them anyway.
>
> I would like to know if OpenSIPS has a way to pull the registrant info via
> code without manually doing a DB query?
>
> S. Rosenberg
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.opensips.org/pipermail/users/attachments/20231217/a714317d/attachment-0001.html>


More information about the Users mailing list