kbr / fritzconnection

Python-Tool to communicate with the AVM Fritz!Box by the TR-064 protocol and the AHA-HTTP-Interface
MIT License
304 stars 59 forks source link

401 Unauthorized with correct Username and Password #100

Closed mookie- closed 2 years ago

mookie- commented 3 years ago

Hello,

I'm not sure yet if it's a bug but I couldn't find the problem myself.

This is my setup:

fritzconnection v1.5.0
FRITZ!Box 7590 AX at http://169.254.1.1
FRITZ!OS: 7.27

I'm running this code:

>>> from fritzconnection import FritzConnection
>>> fc=fritzconnection.core.fritzconnection.FritzConnection(address='192.168.1.1',port=49000,user='collectd',password='password')
>>> fc.call_action('WANIPConnection1', 'GetStatusInfo')

and I get this error:

Traceback (most recent call last):
  File "/var/lib/collectd/.local/lib/python3.9/site-packages/fritzconnection/core/soaper.py", line 123, in raise_fritzconnection_error
    root = etree.fromstring(response.content)
  File "/usr/lib/python3.9/xml/etree/ElementTree.py", line 1347, in XML
    parser.feed(text)
xml.etree.ElementTree.ParseError: mismatched tag: line 1, column 156

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/var/lib/collectd/.local/lib/python3.9/site-packages/fritzconnection/core/fritzconnection.py", line 274, in call_action
    return self.soaper.execute(service, action_name, arguments)
  File "/var/lib/collectd/.local/lib/python3.9/site-packages/fritzconnection/core/soaper.py", line 238, in execute
    return handle_response(response)
  File "/var/lib/collectd/.local/lib/python3.9/site-packages/fritzconnection/core/soaper.py", line 222, in handle_response
    raise_fritzconnection_error(response)
  File "/var/lib/collectd/.local/lib/python3.9/site-packages/fritzconnection/core/soaper.py", line 133, in raise_fritzconnection_error
    raise FritzConnectionException(msg)
fritzconnection.core.exceptions.FritzConnectionException: Unable to perform operation. 401 Unauthorized (ERR_NONE)401 UnauthorizedERR_NONEWebserver Sun, 27 Jun 2021 21:36:14 GMT

The FritzBox tells me:

2021-06-28-000638_960x49_scrot

But I've tested it multiple times and made sure the user/password is correct. I've also set it via environment variable. I've also looked at the tcpdump and at least the username is sent correctly. I've changed all passwords after that, so that there is (hopefully) no sensible data in it. tcpdump.txt

I've also attached some screenshots from the FritzBox interface to show you how it's configured.

2021-06-28-000728_984x304_scrot 2021-06-28-000655_1008x814_scrot

It would be nice if someone could help me debugging this further as I don't have an idea right now.

kbr commented 3 years ago

Did you activated the proper settings in the FritzBox to support TR-064 access?

Home Network -> Network -> Network Settings -> "Allow access for applications" and "Transmit status information over UPnP"

mookie- commented 3 years ago

Hello @kbr , thank you for the fast response. Yes I've enabled it (switched my language to english for this screenshot):

2021-06-28-100524_2192x309_scrot

The strangest thing is the Fritzbox tells me:

Login by an app with unkown login name from IP address 192.168.1.11 failed

but I can see via tcpdump that fritzconnect is sending the correct username.

kbr commented 3 years ago

That's weird. Try the following: login at the web-interface as a regular user. Then use the fritzwlan cli tool with the password for this user, but no username. In this case fritzconnection will take the last logged in username (since 1.5.0).

Here is the output from my 7590 with OS 7.27:

$fritzwlan -i 192.168.178.1 -p <password> [-u <username>]

fritzconnection v1.5.0
FRITZ!Box 7590 at http://192.168.178.1
FRITZ!OS: 7.27

Hosts registered at WLANConfiguration1:
WLAN name: xxxx
channel  : 6
index  active                 mac                ip  signal   speed

<more output listed here>

Play around with a missing username, a matching one and an unmatching. In the last case you should get the "401 Unauthorized" error again. That's how it should work and is reproducible on my hardware.

mookie- commented 2 years ago

Hello @kbr, thank you very much for your help!

I figured it out. The reason had nothing to do with the username. I got the 401 due to the fact that the Action WANIPConnection1 is not available at my FritzBox. I'm not sure why it failed with "401 Unauthorized" and I got the "with unkown login name" log entries, but when I use WANPPPConnection1 it works like charm.

Thanks again for your help and for creating and maintaining this software! :heart: