Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dnssd.c: Let Avahi decide the hostname for register #902

Closed
wants to merge 1 commit into from

Conversation

zdohnal
Copy link
Member

@zdohnal zdohnal commented Feb 29, 2024

Sometimes it happens Avahi can register local service under a hostname of different machine, if the local daemon enforces the hostname as parameter, but the hostname is already taken by different machine.

If NULL is passed as hostname into Avahi API for adding services, Avahi figures out hostname and IP address to use based on interface index passed into the function, and handles hostname changes as well.

This way enables adding services to localhost only, because Avahi takes localhost as an invalid FQDN (unless the machine hostname is localhost). The current way of how to share the service on localhost only in Avahi is to use interface index for loopback and pass NULL as hostname.

zdohnal added a commit to zdohnal/pappl that referenced this pull request Feb 29, 2024
In case users would like to prevent sharing services from printer
 applications to local network, restrict it to localhost and let CUPS
 do the sharing.

This can be done by setting listen-hostname in PAPPL API - this prevents
accessing the public addresses, but the service is still published
on those public addresses. This can be prevented if the machine hostname
is changed to localhost, but that's not desired on machines IIUC.

The PR does the following:

- introduced new pappl system member reghost, which is used
  for saving listen-hostname,
- new public accessors for that member, papplSystemSetRegHostName()
  and papplSystemGetRegHostName() - user can set the member to localhost
  or to the current hostname
- dnssd functions will check this member, and if it is localhost,
  it will use loopback index
- in case of Avahi it passes NULL as hostname to let Avahi decide
  what hostname to use (in case of hostname conflicts - and Avahi forbids
  using localhost if it is not FQDN)

The result is that if reghost is set to localhost, the service is published
on .local address, but resolved to loopback because CUPS uses DNS-SD names in URIs.

PAPPL 2.x version requires CUPS PR OpenPrinting/cups#902
to have it working.
zdohnal added a commit to zdohnal/pappl that referenced this pull request Feb 29, 2024
In case users would like to prevent sharing services from printer
 applications to local network, restrict it to localhost and let CUPS
 do the sharing.

This can be done by setting listen-hostname in PAPPL API - this prevents
accessing the public addresses, but the service is still published
on those public addresses. This can be prevented if the machine hostname
is changed to localhost, but that's not desired on machines IIUC.

The PR does the following:

- introduced new pappl system member reghost, which is used
  for saving listen-hostname,
- new public accessors for that member, papplSystemSetRegHostName()
  and papplSystemGetRegHostName() - user can set the member to localhost
  or to the current hostname
- dnssd functions will check this member, and if it is localhost,
  it will use loopback index
- in case of Avahi it passes NULL as hostname to let Avahi decide
  what hostname to use (in case of hostname conflicts - and Avahi forbids
  using localhost if it is not FQDN)

The result is that if reghost is set to localhost, the service is published
on .local address, but resolved to loopback because CUPS uses DNS-SD names in URIs.

PAPPL 2.x version requires CUPS PR OpenPrinting/cups#902
to have it working.
zdohnal added a commit to zdohnal/pappl that referenced this pull request Feb 29, 2024
In case users would like to prevent sharing services from printer
 applications to local network, restrict it to localhost and let CUPS
 do the sharing.

This can be done by setting listen-hostname in PAPPL API - this prevents
accessing the public addresses, but the service is still published
on those public addresses. This can be prevented if the machine hostname
is changed to localhost, but that's not desired on machines IIUC.

The PR does the following:

- introduced new pappl system member reghost, which is used
  for saving listen-hostname,
- new public accessors for that member, papplSystemSetRegHostName()
  and papplSystemGetRegHostName() - user can set the member to localhost
  or to the current hostname
- dnssd functions will check this member, and if it is localhost,
  it will use loopback index
- in case of Avahi it passes NULL as hostname to let Avahi decide
  what hostname to use (in case of hostname conflicts - and Avahi forbids
  using localhost if it is not FQDN)

The result is that if reghost is set to localhost, the service is published
on .local address, but resolved to loopback because CUPS uses DNS-SD names in URIs.

PAPPL 2.x version requires CUPS PR OpenPrinting/cups#902
to have it working.
zdohnal added a commit to zdohnal/pappl that referenced this pull request Mar 1, 2024
In case users would like to prevent sharing services from printer
 applications to local network, restrict it to localhost and let CUPS
 do the sharing.

This can be done by setting listen-hostname in PAPPL API - this prevents
accessing the public addresses, but the service is still published
on those public addresses. This can be prevented if the machine hostname
is changed to localhost, but that's not desired on machines IIUC.

The PR does the following:

- introduced new pappl system member reghost, which is used
  for saving listen-hostname,
- new public accessors for that member, papplSystemSetRegHostName()
  and papplSystemGetRegHostName() - user can set the member to localhost
  or to the current hostname
- dnssd functions will check this member, and if it is localhost,
  it will use loopback index
- in case of Avahi it passes NULL as hostname to let Avahi decide
  what hostname to use (in case of hostname conflicts - and Avahi forbids
  using localhost if it is not FQDN)

The result is that if reghost is set to localhost, the service is published
on .local address, but resolved to loopback because CUPS uses DNS-SD names in URIs.

PAPPL 2.x version requires CUPS PR OpenPrinting/cups#902
to have it working.
zdohnal added a commit to zdohnal/pappl that referenced this pull request Mar 1, 2024
In case users would like to prevent sharing services from printer
 applications to local network, restrict it to localhost and let CUPS
 do the sharing.

This can be done by setting listen-hostname in PAPPL API - this prevents
accessing the public addresses, but the service is still published
on those public addresses. This can be prevented if the machine hostname
is changed to localhost, but that's not desired on machines IIUC.

The PR does the following:

- introduced new pappl system member reghost, which is used
  for saving listen-hostname,
- new public accessors for that member, papplSystemSetRegHostName()
  and papplSystemGetRegHostName() - user can set the member to localhost
  or to the current hostname
- dnssd functions will check this member, and if it is localhost,
  it will use loopback index
- in case of Avahi it passes NULL as hostname to let Avahi decide
  what hostname to use (in case of hostname conflicts - and Avahi forbids
  using localhost if it is not FQDN)

The result is that if reghost is set to localhost, the service is published
on .local address, but resolved to loopback because CUPS uses DNS-SD names in URIs.

PAPPL 2.x version requires CUPS PR OpenPrinting/cups#902
to have it working.
Copy link
Member

@michaelrsweet michaelrsweet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes it is necessary to advertise with a specific hostname. Maybe set "host" to NULL when advertising on the loopback interface?

zdohnal added a commit to zdohnal/pappl that referenced this pull request Mar 6, 2024
In case users would like to prevent sharing services from printer
 applications to local network, restrict it to localhost and let CUPS
 do the sharing.

This can be done by setting listen-hostname in PAPPL API - this prevents
accessing the public addresses, but the service is still published
on those public addresses. This can be prevented if the machine hostname
is changed to localhost, but that's not desired on machines IIUC.

The PR does the following:

- introduced new pappl system member listen_hostname, which is used
  for saving listen-hostname,
- the member will be set in `papplSystemAddListeners()` - in case
  the listener is domain socket or the listener name is not defined
  and no hostname is set, listen_hostname is NULL and disables advertising,
- dnssd functions will check this member, and if it is localhost or matching
  IP address, it will use loopback index,
- in case of Avahi it passes NULL as hostname to let Avahi decide
  what hostname to use (in case of hostname conflicts - and Avahi forbids
  using localhost if it is not FQDN).

The result is that if reghost is set to localhost, the service is published
on .local address, but resolved to loopback because CUPS uses DNS-SD names in URIs.

PAPPL 2.x version requires CUPS PR OpenPrinting/cups#902
to have it working.
zdohnal added a commit to zdohnal/pappl that referenced this pull request Mar 6, 2024
In case users would like to prevent sharing services from printer
 applications to local network, restrict it to localhost and let CUPS
 do the sharing.

This can be done by setting listen-hostname in PAPPL API - this prevents
accessing the public addresses, but the service is still published
on those public addresses. This can be prevented if the machine hostname
is changed to localhost, but that's not desired on machines IIUC.

The PR does the following:

- introduced new pappl system member listen_hostname, which is used
  for saving listen-hostname,
- the member will be set in `papplSystemAddListeners()` - in case
  the listener is domain socket or the listener name is not defined
  and no hostname is set, listen_hostname is NULL and disables advertising,
- dnssd functions will check this member, and if it is localhost or matching
  IP address, it will use loopback index,
- in case of Avahi it passes NULL as hostname to let Avahi decide
  what hostname to use (in case of hostname conflicts - and Avahi forbids
  using localhost if it is not FQDN).

The result is that if reghost is set to localhost, the service is published
on .local address, but resolved to loopback because CUPS uses DNS-SD names in URIs.

PAPPL 2.x version requires CUPS PR OpenPrinting/cups#902
to have it working.
zdohnal added a commit to zdohnal/pappl that referenced this pull request Mar 13, 2024
In case users would like to prevent sharing services from printer
 applications to local network, restrict it to localhost and let CUPS
 do the sharing.

This can be done by setting system hostname in PAPPL API - this prevents
accessing the public addresses, but the service is still published
on those public addresses.

The PR does the following:

- set/reset system hostname in `papplSystemAddListeners()` directly, not via API.
  It prevents changing machine' hostname.
- if the listener is domain socket, system hostname is freed if needed.
  This functionality disables advertising.
- dnssd function will check the hostname and if it is localhost or matching
  IP address, it will use loopback index,
- in case of Avahi it passes NULL as hostname to let Avahi decide
  what hostname to use (in case of hostname conflicts - and Avahi forbids
  using localhost if it is not FQDN).

The result is that if system hostname is set to localhost internally, the service is published
on .local address, but resolved to loopback because CUPS uses DNS-SD names in URIs.

PAPPL 2.x version requires CUPS PR OpenPrinting/cups#902
to have it working.
zdohnal added a commit to zdohnal/pappl that referenced this pull request Mar 13, 2024
In case users would like to prevent sharing services from printer
 applications to local network, restrict it to localhost and let CUPS
 do the sharing.

This can be done by setting system hostname in PAPPL API - this prevents
accessing the public addresses, but the service is still published
on those public addresses.

The PR does the following:

- set/reset system hostname in `papplSystemAddListeners()` directly, not via API.
  It prevents changing machine' hostname.
- dnssd function will check the hostname and if it is localhost or matching
  IP address, it will use loopback index,
- in case of Avahi it passes NULL as hostname to let Avahi decide
  what hostname to use (in case of hostname conflicts - and Avahi forbids
  using localhost if it is not FQDN).

The result is that if system hostname is set to localhost internally, the service is published
on .local address, but resolved to loopback because CUPS uses DNS-SD names in URIs.

PAPPL 2.x version requires CUPS PR OpenPrinting/cups#902
to have it working.
zdohnal added a commit to zdohnal/pappl that referenced this pull request Mar 14, 2024
In case users would like to prevent sharing services from printer
 applications to local network, restrict it to localhost and let CUPS
 do the sharing.

This can be done by setting system hostname in PAPPL API - this prevents
accessing the public addresses, but the service is still published
on those public addresses.

The PR does the following:

- set/reset system hostname in `papplSystemAddListeners()` directly, not via API.
  It prevents changing machine' hostname.
- dnssd function will check the hostname and if it is localhost or matching
  IP address, it will use loopback index,
- in case of Avahi it passes NULL as hostname to let Avahi decide
  what hostname to use (in case of hostname conflicts - and Avahi forbids
  using localhost if it is not FQDN).

The result is that if system hostname is set to localhost internally, the service is published
on .local address, but resolved to loopback because CUPS uses DNS-SD names in URIs.

PAPPL 2.x version requires CUPS PR OpenPrinting/cups#902
to have it working.
Sometimes it happens Avahi can register local service under a hostname
of different machine, if the local daemon enforces the hostname as
parameter, but the hostname is already taken by different machine.

If NULL is passed as hostname into Avahi API for adding services, Avahi
figures out hostname and IP address to use based on interface index
passed into the function, and handles hostname changes as well.

This way enables adding services to localhost only, because Avahi takes
localhost as an invalid FQDN (unless the machine hostname is localhost).
The current way of how to share the service on localhost only in Avahi
is to use interface index for loopback and pass NULL as hostname.
zdohnal added a commit to zdohnal/pappl that referenced this pull request Mar 15, 2024
In case users would like to prevent sharing services from printer
 applications to local network, restrict it to localhost and let CUPS
 do the sharing.

This can be done by setting system hostname in PAPPL API - this prevents
accessing the public addresses, but the service is still published
on those public addresses.

The PR does the following:

- set/reset system hostname in `papplSystemAddListeners()` directly, not via API.
  It prevents changing machine' hostname.
- dnssd function will check the hostname and if it is localhost or matching
  IP address, it will use loopback index,
- in case of Avahi it passes NULL as hostname to let Avahi decide
  what hostname to use (in case of hostname conflicts - and Avahi forbids
  using localhost if it is not FQDN).

The result is that if system hostname is set to localhost internally, the service is published
on .local address, but resolved to loopback because CUPS uses DNS-SD names in URIs.

PAPPL 2.x version requires CUPS PR OpenPrinting/cups#902
to have it working.
Copy link
Member

@michaelrsweet michaelrsweet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll work out the necessary changes here and also commit them to the libcups repo.

// Avahi client callback is first run when client is created.
// Ignore the initial call to prevent regenerating hostnames
// and registrations for nothing...
dnssd->config_changes = -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config_changes is size_t so you can't assign it a negative value...

@@ -1096,7 +1101,7 @@ cupsDNSSDServiceAdd(
*subtypes++ = '\0';

// Add the service entry...
if ((error = avahi_entry_group_add_service_strlst(service->group, avahi_if_index(service->if_index), AVAHI_PROTO_UNSPEC, /*flags*/0, service->name, regtype, domain, host, port, txtrec)) < 0)
if ((error = avahi_entry_group_add_service_strlst(service->group, avahi_if_index(service->if_index), AVAHI_PROTO_UNSPEC, /*flags*/0, service->name, regtype, domain, NULL, port, txtrec)) < 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This really needs to be "host" unless it is "localhost"...

@michaelrsweet
Copy link
Member

[master a88910b] Use NULL hostname for services on the loopback interface (Issue #902)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants