cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering This file is part of systemd.
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering Copyright 2014 Lennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering systemd is free software; you can redistribute it and/or modify it
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering under the terms of the GNU Lesser General Public License as published by
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering (at your option) any later version.
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering systemd is distributed in the hope that it will be useful, but
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering Lesser General Public License for more details.
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering You should have received a copy of the GNU Lesser General Public License
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering#define HOST_UID_LIMIT ((uid_t) UINT32_C(0x10000))
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering#define HOST_GID_LIMIT ((gid_t) UINT32_C(0x10000))
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poetteringstatic int count_addresses(sd_bus_message *m, int af, unsigned *ret) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering unsigned c = 0;
3a6fb33c54bc64398e0af1c9d7c74a6b614a849dLennart Poettering while ((r = sd_bus_message_enter_container(m, 'r', "iay")) > 0) {
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering r = sd_bus_message_read(m, "i", &family);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poetteringenum nss_status _nss_mymachines_gethostbyname4_r(
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering struct gaih_addrtuple *r_tuple, *r_tuple_first = NULL;
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_bus_message_unrefp) sd_bus_message* reply = NULL;
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering unsigned i = 0, c = 0;
634af5665fda8776d22624d947c8de830e30a874Lennart Poettering n_ifindices = sd_machine_get_ifindices(name, &ifindices);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "org.freedesktop.machine1",
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "org.freedesktop.machine1.Manager",
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "GetMachineAddresses",
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering r = sd_bus_message_enter_container(reply, 'a', "(iay)");
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = count_addresses(reply, AF_UNSPEC, &c);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * c;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering /* First, append name */
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering /* Second, append addresses */
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r_tuple_first = (struct gaih_addrtuple*) (buffer + idx);
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering while ((r = sd_bus_message_enter_container(reply, 'r', "iay")) > 0) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering const void *a;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering r = sd_bus_message_read(reply, "i", &family);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_read_array(reply, 'y', &a, &sz);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_exit_container(reply);
555bd6e95bf2b469306d9cd86e126c0122d0895bLennart Poettering if (!IN_SET(family, AF_INET, AF_INET6)) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r_tuple = (struct gaih_addrtuple*) (buffer + idx);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r_tuple->next = i == c-1 ? NULL : (struct gaih_addrtuple*) ((char*) r_tuple + ALIGN(sizeof(struct gaih_addrtuple)));
634af5665fda8776d22624d947c8de830e30a874Lennart Poettering r_tuple->scopeid = n_ifindices == 1 ? ifindices[0] : 0;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering idx += ALIGN(sizeof(struct gaih_addrtuple));
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_exit_container(reply);
e70df46b9721a3d025e7a0b4ffb5893cbde5e55dLennart Poettering /* Explicitly reset all error variables */
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poetteringenum nss_status _nss_mymachines_gethostbyname3_r(
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_bus_message_unrefp) sd_bus_message* reply = NULL;
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering unsigned c = 0, i = 0;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering char *r_name, *r_aliases, *r_addr, *r_addr_list;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "org.freedesktop.machine1",
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "org.freedesktop.machine1.Manager",
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "GetMachineAddresses",
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering r = sd_bus_message_enter_container(reply, 'a', "(iay)");
66a16e7e9fc501d371b57cbe2ae5d130fe930c6dThomas Hindoe Paaboel Andersen ms = ALIGN(l+1) + c * ALIGN(alen) + (c+2) * sizeof(char*);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering /* First, append name */
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering /* Second, create aliases array */
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering idx += sizeof(char*);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering /* Third, append addresses */
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering while ((r = sd_bus_message_enter_container(reply, 'r', "iay")) > 0) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering const void *a;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering r = sd_bus_message_read(reply, "i", &family);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_read_array(reply, 'y', &a, &sz);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_exit_container(reply);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_exit_container(reply);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering /* Third, append address pointer array */
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering for (i = 0; i < c; i++)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering ((char**) r_addr_list)[i] = r_addr + i*ALIGN(alen);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering result->h_addr_list = (char**) r_addr_list;
e70df46b9721a3d025e7a0b4ffb5893cbde5e55dLennart Poettering /* Explicitly reset all error variables */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringenum nss_status _nss_mymachines_getpwnam_r(
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_bus_message_unrefp) sd_bus_message* reply = NULL;
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!e || e == p)
cb31827d62066a04b02111df3052949fda4b6888Zbigniew Jędrzejewski-Szmek if (e - p > HOST_NAME_MAX - 1) /* -1 for the last dash */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "org.freedesktop.machine1",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "org.freedesktop.machine1.Manager",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "MapFromMachineUser",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_USER_MAPPING))
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = sd_bus_message_read(reply, "u", &mapped);
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering /* Refuse to work if the mapped address is in the host UID range, or if there was no mapping at all. */
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering if (mapped < HOST_UID_LIMIT || mapped == uid)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pwd->pw_passwd = (char*) "*"; /* locked */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringenum nss_status _nss_mymachines_getpwuid_r(
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_bus_message_unrefp) sd_bus_message* reply = NULL;
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering /* We consider all uids < 65536 host uids */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "org.freedesktop.machine1",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "org.freedesktop.machine1.Manager",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "MapToMachineUser",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_USER_MAPPING))
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = sd_bus_message_read(reply, "sou", &machine, &object, &mapped);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (snprintf(buffer, buflen, "vu-%s-" UID_FMT, machine, (uid_t) mapped) >= (int) buflen) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pwd->pw_passwd = (char*) "*"; /* locked */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringenum nss_status _nss_mymachines_getgrnam_r(
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_bus_message_unrefp) sd_bus_message* reply = NULL;
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!e || e == p)
cb31827d62066a04b02111df3052949fda4b6888Zbigniew Jędrzejewski-Szmek if (e - p > HOST_NAME_MAX - 1) /* -1 for the last dash */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "org.freedesktop.machine1",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "org.freedesktop.machine1.Manager",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "MapFromMachineGroup",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_GROUP_MAPPING))
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = sd_bus_message_read(reply, "u", &mapped);
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering if (mapped < HOST_GID_LIMIT || mapped == gid)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering gr->gr_passwd = (char*) "*"; /* locked */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringenum nss_status _nss_mymachines_getgrgid_r(
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_bus_message_unrefp) sd_bus_message* reply = NULL;
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering /* We consider all gids < 65536 host gids */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "org.freedesktop.machine1",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "org.freedesktop.machine1.Manager",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "MapToMachineGroup",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_GROUP_MAPPING))
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = sd_bus_message_read(reply, "sou", &machine, &object, &mapped);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (snprintf(buffer + sizeof(char*), buflen - sizeof(char*), "vg-%s-" GID_FMT, machine, (gid_t) mapped) >= (int) buflen) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering gr->gr_passwd = (char*) "*"; /* locked */