cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering/***
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering This file is part of systemd.
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering Copyright 2014 Lennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart 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
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
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/>.
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering***/
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering#include <netdb.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <nss.h>
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering#include "sd-bus.h"
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering#include "sd-login.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "alloc-util.h"
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering#include "bus-common-errors.h"
25300b5a1fcf54674a69d0f4ab08925be00b0227Lennart Poettering#include "hostname-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "in-addr-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "macro.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "nss-util.h"
0c5eb0562abec6f845f07c30b2ad2515900ec1e5Lennart Poettering#include "signal-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "string-util.h"
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering#include "user-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "util.h"
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart PoetteringNSS_GETHOSTBYNAME_PROTOTYPES(mymachines);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart PoetteringNSS_GETPW_PROTOTYPES(mymachines);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart PoetteringNSS_GETGR_PROTOTYPES(mymachines);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering#define HOST_UID_LIMIT ((uid_t) UINT32_C(0x10000))
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering#define HOST_GID_LIMIT ((gid_t) UINT32_C(0x10000))
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poetteringstatic int count_addresses(sd_bus_message *m, int af, unsigned *ret) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering unsigned c = 0;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering int r;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering assert(m);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering assert(ret);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
3a6fb33c54bc64398e0af1c9d7c74a6b614a849dLennart Poettering while ((r = sd_bus_message_enter_container(m, 'r', "iay")) > 0) {
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering int family;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering r = sd_bus_message_read(m, "i", &family);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering return r;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_skip(m, "ay");
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering return r;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_exit_container(m);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering return r;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (af != AF_UNSPEC && family != af)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering continue;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering c ++;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering }
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering return r;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_rewind(m, false);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering return r;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering *ret = c;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering return 0;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering}
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poetteringenum nss_status _nss_mymachines_gethostbyname4_r(
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering const char *name,
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering struct gaih_addrtuple **pat,
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering char *buffer, size_t buflen,
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering int *errnop, int *h_errnop,
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering int32_t *ttlp) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
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;
634af5665fda8776d22624d947c8de830e30a874Lennart Poettering _cleanup_free_ int *ifindices = NULL;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering _cleanup_free_ char *class = NULL;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering size_t l, ms, idx;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering unsigned i = 0, c = 0;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering char *r_name;
634af5665fda8776d22624d947c8de830e30a874Lennart Poettering int n_ifindices, r;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
0c5eb0562abec6f845f07c30b2ad2515900ec1e5Lennart Poettering BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
0c5eb0562abec6f845f07c30b2ad2515900ec1e5Lennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering assert(name);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering assert(pat);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering assert(buffer);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering assert(errnop);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering assert(h_errnop);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_machine_get_class(name, &class);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (!streq(class, "container")) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = -ENOTTY;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering }
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
634af5665fda8776d22624d947c8de830e30a874Lennart Poettering n_ifindices = sd_machine_get_ifindices(name, &ifindices);
634af5665fda8776d22624d947c8de830e30a874Lennart Poettering if (n_ifindices < 0) {
634af5665fda8776d22624d947c8de830e30a874Lennart Poettering r = n_ifindices;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering }
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_open_system(&bus);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_call_method(bus,
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "org.freedesktop.machine1",
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "/org/freedesktop/machine1",
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "org.freedesktop.machine1.Manager",
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "GetMachineAddresses",
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering NULL,
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering &reply,
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "s", name);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering r = sd_bus_message_enter_container(reply, 'a', "(iay)");
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = count_addresses(reply, AF_UNSPEC, &c);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (c <= 0) {
555bd6e95bf2b469306d9cd86e126c0122d0895bLennart Poettering *errnop = ESRCH;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering *h_errnop = HOST_NOT_FOUND;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering return NSS_STATUS_NOTFOUND;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering }
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering l = strlen(name);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * c;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (buflen < ms) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering *errnop = ENOMEM;
555bd6e95bf2b469306d9cd86e126c0122d0895bLennart Poettering *h_errnop = TRY_AGAIN;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering return NSS_STATUS_TRYAGAIN;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering }
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering /* First, append name */
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r_name = buffer;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering memcpy(r_name, name, l+1);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering idx = ALIGN(l+1);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
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) {
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering int family;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering const void *a;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering size_t sz;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering r = sd_bus_message_read(reply, "i", &family);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_read_array(reply, 'y', &a, &sz);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_exit_container(reply);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
555bd6e95bf2b469306d9cd86e126c0122d0895bLennart Poettering if (!IN_SET(family, AF_INET, AF_INET6)) {
555bd6e95bf2b469306d9cd86e126c0122d0895bLennart Poettering r = -EAFNOSUPPORT;
555bd6e95bf2b469306d9cd86e126c0122d0895bLennart Poettering goto fail;
555bd6e95bf2b469306d9cd86e126c0122d0895bLennart Poettering }
555bd6e95bf2b469306d9cd86e126c0122d0895bLennart Poettering
9d485985338a46b8cb1acdf1af6c1eb2e88acfeeLennart Poettering if (sz != FAMILY_ADDRESS_SIZE(family)) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = -EINVAL;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering }
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
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)));
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r_tuple->name = r_name;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r_tuple->family = family;
634af5665fda8776d22624d947c8de830e30a874Lennart Poettering r_tuple->scopeid = n_ifindices == 1 ? ifindices[0] : 0;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering memcpy(r_tuple->addr, a, sz);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering idx += ALIGN(sizeof(struct gaih_addrtuple));
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering i++;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering }
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering assert(i == c);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_exit_container(reply);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering assert(idx == ms);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (*pat)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering **pat = *r_tuple_first;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering else
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering *pat = r_tuple_first;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (ttlp)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering *ttlp = 0;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
e70df46b9721a3d025e7a0b4ffb5893cbde5e55dLennart Poettering /* Explicitly reset all error variables */
e70df46b9721a3d025e7a0b4ffb5893cbde5e55dLennart Poettering *errnop = 0;
e70df46b9721a3d025e7a0b4ffb5893cbde5e55dLennart Poettering *h_errnop = NETDB_SUCCESS;
e70df46b9721a3d025e7a0b4ffb5893cbde5e55dLennart Poettering h_errno = 0;
e70df46b9721a3d025e7a0b4ffb5893cbde5e55dLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering return NSS_STATUS_SUCCESS;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poetteringfail:
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering *errnop = -r;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering *h_errnop = NO_DATA;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering return NSS_STATUS_UNAVAIL;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering}
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poetteringenum nss_status _nss_mymachines_gethostbyname3_r(
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering const char *name,
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering int af,
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering struct hostent *result,
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering char *buffer, size_t buflen,
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering int *errnop, int *h_errnop,
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering int32_t *ttlp,
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering char **canonp) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
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 _cleanup_free_ char *class = NULL;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering unsigned c = 0, i = 0;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering char *r_name, *r_aliases, *r_addr, *r_addr_list;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering size_t l, idx, ms, alen;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering int r;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
0c5eb0562abec6f845f07c30b2ad2515900ec1e5Lennart Poettering BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
0c5eb0562abec6f845f07c30b2ad2515900ec1e5Lennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering assert(name);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering assert(result);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering assert(buffer);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering assert(errnop);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering assert(h_errnop);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (af == AF_UNSPEC)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering af = AF_INET;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (af != AF_INET && af != AF_INET6) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = -EAFNOSUPPORT;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering }
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_machine_get_class(name, &class);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (!streq(class, "container")) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = -ENOTTY;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering }
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_open_system(&bus);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_call_method(bus,
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "org.freedesktop.machine1",
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "/org/freedesktop/machine1",
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "org.freedesktop.machine1.Manager",
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "GetMachineAddresses",
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering NULL,
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering &reply,
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "s", name);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering r = sd_bus_message_enter_container(reply, 'a', "(iay)");
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = count_addresses(reply, af, &c);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (c <= 0) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering *errnop = ENOENT;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering *h_errnop = HOST_NOT_FOUND;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering return NSS_STATUS_NOTFOUND;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering }
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
9d485985338a46b8cb1acdf1af6c1eb2e88acfeeLennart Poettering alen = FAMILY_ADDRESS_SIZE(af);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering l = strlen(name);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
66a16e7e9fc501d371b57cbe2ae5d130fe930c6dThomas Hindoe Paaboel Andersen ms = ALIGN(l+1) + c * ALIGN(alen) + (c+2) * sizeof(char*);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (buflen < ms) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering *errnop = ENOMEM;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering *h_errnop = NO_RECOVERY;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering return NSS_STATUS_TRYAGAIN;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering }
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering /* First, append name */
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r_name = buffer;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering memcpy(r_name, name, l+1);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering idx = ALIGN(l+1);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering /* Second, create aliases array */
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r_aliases = buffer + idx;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering ((char**) r_aliases)[0] = NULL;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering idx += sizeof(char*);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering /* Third, append addresses */
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r_addr = buffer + idx;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering while ((r = sd_bus_message_enter_container(reply, 'r', "iay")) > 0) {
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering int family;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering const void *a;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering size_t sz;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering r = sd_bus_message_read(reply, "i", &family);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_read_array(reply, 'y', &a, &sz);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_exit_container(reply);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (family != af)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering continue;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (sz != alen) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = -EINVAL;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering }
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering memcpy(r_addr + i*ALIGN(alen), a, alen);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering i++;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering }
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering assert(i == c);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering idx += c * ALIGN(alen);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_exit_container(reply);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (r < 0)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering goto fail;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering /* Third, append address pointer array */
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r_addr_list = buffer + idx;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering for (i = 0; i < c; i++)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering ((char**) r_addr_list)[i] = r_addr + i*ALIGN(alen);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering ((char**) r_addr_list)[i] = NULL;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering idx += (c+1) * sizeof(char*);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering assert(idx == ms);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering result->h_name = r_name;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering result->h_aliases = (char**) r_aliases;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering result->h_addrtype = af;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering result->h_length = alen;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering result->h_addr_list = (char**) r_addr_list;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (ttlp)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering *ttlp = 0;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering if (canonp)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering *canonp = r_name;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
e70df46b9721a3d025e7a0b4ffb5893cbde5e55dLennart Poettering /* Explicitly reset all error variables */
e70df46b9721a3d025e7a0b4ffb5893cbde5e55dLennart Poettering *errnop = 0;
e70df46b9721a3d025e7a0b4ffb5893cbde5e55dLennart Poettering *h_errnop = NETDB_SUCCESS;
e70df46b9721a3d025e7a0b4ffb5893cbde5e55dLennart Poettering h_errno = 0;
e70df46b9721a3d025e7a0b4ffb5893cbde5e55dLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering return NSS_STATUS_SUCCESS;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poetteringfail:
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering *errnop = -r;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering *h_errnop = NO_DATA;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering return NSS_STATUS_UNAVAIL;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering}
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart PoetteringNSS_GETHOSTBYNAME_FALLBACKS(mymachines);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringenum nss_status _nss_mymachines_getpwnam_r(
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering const char *name,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering struct passwd *pwd,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering char *buffer, size_t buflen,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering int *errnop) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
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 const char *p, *e, *machine;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering uint32_t mapped;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering uid_t uid;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering size_t l;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering int r;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
0c5eb0562abec6f845f07c30b2ad2515900ec1e5Lennart Poettering BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
0c5eb0562abec6f845f07c30b2ad2515900ec1e5Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering assert(name);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering assert(pwd);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering p = startswith(name, "vu-");
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!p)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto not_found;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering e = strrchr(p, '-');
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!e || e == p)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto not_found;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
cb31827d62066a04b02111df3052949fda4b6888Zbigniew Jędrzejewski-Szmek if (e - p > HOST_NAME_MAX - 1) /* -1 for the last dash */
cb31827d62066a04b02111df3052949fda4b6888Zbigniew Jędrzejewski-Szmek goto not_found;
cb31827d62066a04b02111df3052949fda4b6888Zbigniew Jędrzejewski-Szmek
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = parse_uid(e + 1, &uid);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto not_found;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering machine = strndupa(p, e - p);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!machine_name_is_valid(machine))
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto not_found;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = sd_bus_open_system(&bus);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto fail;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = sd_bus_call_method(bus,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "org.freedesktop.machine1",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "/org/freedesktop/machine1",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "org.freedesktop.machine1.Manager",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "MapFromMachineUser",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering &error,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering &reply,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "su",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering machine, (uint32_t) uid);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_USER_MAPPING))
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto not_found;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto fail;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = sd_bus_message_read(reply, "u", &mapped);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto fail;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
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)
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering goto not_found;
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering l = strlen(name);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (buflen < l+1) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *errnop = ENOMEM;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return NSS_STATUS_TRYAGAIN;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering memcpy(buffer, name, l+1);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pwd->pw_name = buffer;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pwd->pw_uid = mapped;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pwd->pw_gid = 65534; /* nobody */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pwd->pw_gecos = buffer;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pwd->pw_passwd = (char*) "*"; /* locked */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pwd->pw_dir = (char*) "/";
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pwd->pw_shell = (char*) "/sbin/nologin";
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *errnop = 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return NSS_STATUS_SUCCESS;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringnot_found:
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *errnop = 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return NSS_STATUS_NOTFOUND;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringfail:
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *errnop = -r;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return NSS_STATUS_UNAVAIL;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering}
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringenum nss_status _nss_mymachines_getpwuid_r(
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering uid_t uid,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering struct passwd *pwd,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering char *buffer, size_t buflen,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering int *errnop) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
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 const char *machine, *object;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering uint32_t mapped;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering int r;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
0c5eb0562abec6f845f07c30b2ad2515900ec1e5Lennart Poettering BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
0c5eb0562abec6f845f07c30b2ad2515900ec1e5Lennart Poettering
c077529ba6852192c464772ce907670850210dfeLennart Poettering if (!uid_is_valid(uid)) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = -EINVAL;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto fail;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering /* We consider all uids < 65536 host uids */
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering if (uid < HOST_UID_LIMIT)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto not_found;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = sd_bus_open_system(&bus);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto fail;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = sd_bus_call_method(bus,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "org.freedesktop.machine1",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "/org/freedesktop/machine1",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "org.freedesktop.machine1.Manager",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "MapToMachineUser",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering &error,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering &reply,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "u",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering (uint32_t) uid);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_USER_MAPPING))
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto not_found;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto fail;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = sd_bus_message_read(reply, "sou", &machine, &object, &mapped);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto fail;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering if (mapped == uid)
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering goto not_found;
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (snprintf(buffer, buflen, "vu-%s-" UID_FMT, machine, (uid_t) mapped) >= (int) buflen) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *errnop = ENOMEM;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return NSS_STATUS_TRYAGAIN;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pwd->pw_name = buffer;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pwd->pw_uid = uid;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pwd->pw_gid = 65534; /* nobody */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pwd->pw_gecos = buffer;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pwd->pw_passwd = (char*) "*"; /* locked */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pwd->pw_dir = (char*) "/";
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pwd->pw_shell = (char*) "/sbin/nologin";
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *errnop = 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return NSS_STATUS_SUCCESS;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringnot_found:
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *errnop = 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return NSS_STATUS_NOTFOUND;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringfail:
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *errnop = -r;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return NSS_STATUS_UNAVAIL;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering}
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringenum nss_status _nss_mymachines_getgrnam_r(
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering const char *name,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering struct group *gr,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering char *buffer, size_t buflen,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering int *errnop) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
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 const char *p, *e, *machine;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering uint32_t mapped;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering uid_t gid;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering size_t l;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering int r;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
0c5eb0562abec6f845f07c30b2ad2515900ec1e5Lennart Poettering BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
0c5eb0562abec6f845f07c30b2ad2515900ec1e5Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering assert(name);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering assert(gr);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering p = startswith(name, "vg-");
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!p)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto not_found;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering e = strrchr(p, '-');
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!e || e == p)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto not_found;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
cb31827d62066a04b02111df3052949fda4b6888Zbigniew Jędrzejewski-Szmek if (e - p > HOST_NAME_MAX - 1) /* -1 for the last dash */
cb31827d62066a04b02111df3052949fda4b6888Zbigniew Jędrzejewski-Szmek goto not_found;
cb31827d62066a04b02111df3052949fda4b6888Zbigniew Jędrzejewski-Szmek
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = parse_gid(e + 1, &gid);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto not_found;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering machine = strndupa(p, e - p);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!machine_name_is_valid(machine))
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto not_found;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = sd_bus_open_system(&bus);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto fail;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = sd_bus_call_method(bus,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "org.freedesktop.machine1",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "/org/freedesktop/machine1",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "org.freedesktop.machine1.Manager",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "MapFromMachineGroup",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering &error,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering &reply,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "su",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering machine, (uint32_t) gid);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_GROUP_MAPPING))
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto not_found;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto fail;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = sd_bus_message_read(reply, "u", &mapped);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto fail;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering if (mapped < HOST_GID_LIMIT || mapped == gid)
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering goto not_found;
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering l = sizeof(char*) + strlen(name) + 1;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (buflen < l) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *errnop = ENOMEM;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return NSS_STATUS_TRYAGAIN;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering memzero(buffer, sizeof(char*));
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering strcpy(buffer + sizeof(char*), name);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering gr->gr_name = buffer + sizeof(char*);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering gr->gr_gid = gid;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering gr->gr_passwd = (char*) "*"; /* locked */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering gr->gr_mem = (char**) buffer;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *errnop = 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return NSS_STATUS_SUCCESS;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringnot_found:
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *errnop = 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return NSS_STATUS_NOTFOUND;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringfail:
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *errnop = -r;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return NSS_STATUS_UNAVAIL;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering}
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringenum nss_status _nss_mymachines_getgrgid_r(
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering gid_t gid,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering struct group *gr,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering char *buffer, size_t buflen,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering int *errnop) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
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 const char *machine, *object;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering uint32_t mapped;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering int r;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
0c5eb0562abec6f845f07c30b2ad2515900ec1e5Lennart Poettering BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
0c5eb0562abec6f845f07c30b2ad2515900ec1e5Lennart Poettering
c077529ba6852192c464772ce907670850210dfeLennart Poettering if (!gid_is_valid(gid)) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = -EINVAL;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto fail;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering /* We consider all gids < 65536 host gids */
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering if (gid < HOST_GID_LIMIT)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto not_found;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = sd_bus_open_system(&bus);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto fail;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = sd_bus_call_method(bus,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "org.freedesktop.machine1",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "/org/freedesktop/machine1",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "org.freedesktop.machine1.Manager",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "MapToMachineGroup",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering &error,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering &reply,
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering "u",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering (uint32_t) gid);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_GROUP_MAPPING))
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto not_found;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto fail;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = sd_bus_message_read(reply, "sou", &machine, &object, &mapped);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering goto fail;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering if (mapped == gid)
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering goto not_found;
cf3bdcfeba48ffef71f1f59e092c4fb9275dcb3aLennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (buflen < sizeof(char*) + 1) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *errnop = ENOMEM;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return NSS_STATUS_TRYAGAIN;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering memzero(buffer, sizeof(char*));
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (snprintf(buffer + sizeof(char*), buflen - sizeof(char*), "vg-%s-" GID_FMT, machine, (gid_t) mapped) >= (int) buflen) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *errnop = ENOMEM;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return NSS_STATUS_TRYAGAIN;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering gr->gr_name = buffer + sizeof(char*);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering gr->gr_gid = gid;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering gr->gr_passwd = (char*) "*"; /* locked */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering gr->gr_mem = (char**) buffer;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *errnop = 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return NSS_STATUS_SUCCESS;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringnot_found:
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *errnop = 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return NSS_STATUS_NOTFOUND;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringfail:
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *errnop = -r;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return NSS_STATUS_UNAVAIL;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering}