Re: [PATCH] ssh-agent: add systemd socket-based activation

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

 



Demi Marie Obenour wrote:
> >> Something tells me upstream would not be interested in this patch,
> >> but as it may be useful on linux, I'm submitting it here.

Noone should be interested in linking libsystemd into sshd. Of course
this doesn't stop distributions from doing so.


> Would a version that reimplemented sd_listen_fds(3) be okay?

I sent attached sd_notify() reimplementation (Type=notify sshd.service)
five years ago, that didn't go anywhere.


//Peter
/*
 * Copyright (c) 2018 Peter Stuge <peter@xxxxxxxx>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <errno.h>

int sd_notify(int unset_environment, const char *state) {
	char *path;
	int pathlen, ret = 0, sock = -1;
	struct sockaddr_un sa = { AF_UNIX };
	struct iovec vec;
	struct msghdr msg;
	struct ucred creds;
	char cmsgbuf[CMSG_SPACE(sizeof creds)];
	struct cmsghdr *cmsg;

	path = getenv("NOTIFY_SOCKET");
	if (NULL == path)
		return 0;

	pathlen = snprintf(sa.sun_path, sizeof sa.sun_path, "%s", path);
	if (pathlen >= sizeof sa.sun_path) {
		ret = -ENOMEM;
		goto done;
	}

	if ('@' == sa.sun_path[0])
		sa.sun_path[0] = '\0';
	else
		pathlen++;

	sock = socket(sa.sun_family, SOCK_DGRAM, 0);
	if (-1 == sock)
		goto done;

	memset(&msg, 0, sizeof msg);

	msg.msg_name = &sa;
	msg.msg_namelen = offsetof(struct sockaddr_un, sun_path) + pathlen;

	vec.iov_base = (char *)state;
	vec.iov_len = strlen(state);

	msg.msg_iov = &vec;
	msg.msg_iovlen = 1;

	memset(&creds, 0, sizeof creds);
	creds.pid = getpid();
	creds.uid = getuid();
	creds.gid = getgid();

	memset(cmsgbuf, 0, sizeof cmsgbuf);
	msg.msg_control = cmsgbuf;
	msg.msg_controllen = sizeof cmsgbuf;

	cmsg = CMSG_FIRSTHDR(&msg);
	cmsg->cmsg_level = SOL_SOCKET;
	cmsg->cmsg_type = SCM_CREDENTIALS;
	cmsg->cmsg_len = CMSG_LEN(sizeof creds);
	memcpy(CMSG_DATA(cmsg), &creds, sizeof creds);

	msg.msg_controllen = cmsg->cmsg_len;

	if (-1 == sendmsg(sock, &msg, 0))
		goto done;

	ret = 1;

done:
	if (0 == ret)
		ret = -errno;

	if (sock != -1 && -1 == close(sock) && 1 == ret)
		ret = -errno;

	if (unset_environment && -1 == unsetenv("NOTIFY_SOCKET") && 1 == ret)
		ret = -errno;

	return ret;
}
_______________________________________________
openssh-unix-dev mailing list
openssh-unix-dev@xxxxxxxxxxx
https://lists.mindrot.org/mailman/listinfo/openssh-unix-dev

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux