Radish alpha
H
HardenedBSD Package Manager
Radicle
Git (anonymous pull)
Log in to clone via SSH
Add new pkg ssh command which will serve packages over ssh
Baptiste Daroussin committed 13 years ago
commit 4e1974d3fc0d250286c9408498961fdb5dd31a62
parent e3c0cc927368c7d0676023689ff9c8415653a8a9
7 files changed +198 -4
modified libpkg/Makefile
@@ -35,6 +35,7 @@ SRCS= ${PC} \
		pkgdb_repo.c \
		rcscripts.c \
		rsa.c \
+
		ssh.c \
		scripts.c \
		update.c \
		utils.c \
modified libpkg/pkg.h.in
@@ -1297,4 +1297,5 @@ int pkg_old_emit_content(struct pkg *pkg, char **dest);
int pkg_from_old(struct pkg *pkg);
int pkg_to_old(struct pkg *pkg);
int pkg_register_old(struct pkg *pkg);
+
int pkg_sshserve(const char *path);
#endif
added libpkg/ssh.c
@@ -0,0 +1,133 @@
+
/*-
+
 * Copyright (c) 2011-2013 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * All rights reserved.
+
 * 
+
 * Redistribution and use in source and binary forms, with or without
+
 * modification, are permitted provided that the following conditions
+
 * are met:
+
 * 1. Redistributions of source code must retain the above copyright
+
 *    notice, this list of conditions and the following disclaimer
+
 *    in this position and unchanged.
+
 * 2. Redistributions in binary form must reproduce the above copyright
+
 *    notice, this list of conditions and the following disclaimer in the
+
 *    documentation and/or other materials provided with the distribution.
+
 * 
+
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
+
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
+
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
 */
+

+
#include <sys/param.h>
+
#include <sys/stat.h>
+

+
#include <ctype.h>
+
#define _WITH_GETLINE
+
#include <stdio.h>
+
#include <string.h>
+
#include "pkg.h"
+

+
int
+
pkg_sshserve(const char *path __unused)
+
{
+
	struct stat st;
+
	char *line = NULL;
+
	char *file, *age;
+
	char fpath[MAXPATHLEN];
+
	size_t linecap = 0, r;
+
	ssize_t linelen;
+
	time_t mtime = 0;
+
	const char *errstr;
+
	FILE *f;
+
	char buf[BUFSIZ];
+

+
	printf("ok: pkg "PKGVERSION"\n");
+
	for (;;) {
+
		if ((linelen = getline(&line, &linecap, stdin)) <= 0)
+
			continue;
+

+
		/* trim cr */
+
		if (line[linelen - 1] == '\n')
+
			line[linelen - 1] = '\0';
+

+
		if (strcmp(line, "quit") == 0)
+
			return (EPKG_OK);
+

+
		if (strncmp(line, "get ", 4) != 0) {
+
			printf("ko: unknown command '%s'\n", line);
+
			continue;
+
		}
+

+
		file = line + 4;
+
		age = file;
+
		while (!isspace(*age)) {
+
			if (*age == '\0') {
+
				age = NULL;
+
				break;
+
			}
+
			age++;
+
		}
+

+
		if (age == NULL) {
+
			printf("ko: bad command get, expecting 'get file age'\n");
+
			continue;
+
		}
+

+
		*age = '\0';
+
		age++;
+

+
		while (isspace(*age)) {
+
			if (*age == '\0') {
+
				age = NULL;
+
				break;
+
			}
+
			age++;
+
		}
+

+
		if (age == NULL) {
+
			printf("ko: bad command get, expecting 'get file age'\n");
+
			continue;
+
		}
+

+
		mtime = strtonum(age, 0, LONG_MAX, &errstr);
+
		if (errstr) {
+
			printf("ko: bad number %s: %s\n", age, errstr);
+
			continue;
+
		}
+

+
		snprintf(fpath, sizeof(fpath), "%s/%s", path, file);
+

+
		if (stat(fpath, &st) == -1) {
+
			printf("ko: file not found\n");
+
			continue;
+
		}
+

+
		if (!S_ISREG(st.st_mode)) {
+
			printf("ko: not a file\n");
+
			continue;
+
		}
+

+
		if (st.st_mtime <= mtime) {
+
			printf("ok: 0\n");
+
			continue;
+
		}
+

+
		printf("ok: %ld\n", st.st_size);
+
		f = fopen(fpath, "r");
+

+
		while ((r = fread(buf, 1, sizeof(buf), f)) > 0)
+
			fwrite(buf, 1, r, stdout);
+

+
		fclose(f);
+
	}
+

+
	free(line);
+

+
	return (EPKG_OK);
+
}
modified pkg/Makefile
@@ -30,7 +30,8 @@ SRCS= add.c \
		which.c \
		fetch.c \
		shell.c \
-
		stats.c
+
		stats.c \
+
		ssh.c

PREFIX?=	/usr/local
BINDIR=		${PREFIX}/sbin
modified pkg/main.c
@@ -1,9 +1,9 @@
/*-
-
 * Copyright (c) 2011-2012 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * Copyright (c) 2011-2013 Baptiste Daroussin <bapt@FreeBSD.org>
 * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
 * Copyright (c) 2011 Will Andrews <will@FreeBSD.org>
 * Copyright (c) 2011-2012 Marin Atanasov Nikolov <dnaeon@gmail.com>
-
 * All rights reserved.
+
 * All rights resshd.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
@@ -85,6 +85,7 @@ static struct commands {
	{ "rquery", "Queries information in repository catalogues", exec_rquery, usage_rquery},
	{ "search", "Performs a search of package repository catalogues", exec_search, usage_search},
	{ "set", "Modifies information about packages in the local database", exec_set, usage_set},
+
	{ "ssh", "ssh packages to be used via ssh", exec_ssh, usage_ssh},
	{ "shell", "Opens a debug shell", exec_shell, usage_shell},
	{ "shlib", "Displays which packages link against a specific shared library", exec_shlib, usage_shlib},
	{ "stats", "Displays package database statistics", exec_stats, usage_stats},
modified pkg/pkgcli.h
@@ -1,6 +1,6 @@
/*-
 * Copyright (c) 2011-2012 Baptiste Daroussin <bapt@FreeBSD.org>
-
 * All rights reserved.
+
 * All rights resshd.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
@@ -162,6 +162,10 @@ void usage_which(void);
int exec_convert(int, char **);
void usage_convert(void);

+
/* pkg ssh */
+
int exec_ssh(int, char **);
+
void usage_ssh(void);
+

/* utils */

/* These are the fields of the Full output, in order */
added pkg/ssh.c
@@ -0,0 +1,53 @@
+
/*-
+
 * Copyright (c) 2011-2013 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * All rights resshd.
+
 * 
+
 * Redistribution and use in source and binary forms, with or without
+
 * modification, are permitted provided that the following conditions
+
 * are met:
+
 * 1. Redistributions of source code must retain the above copyright
+
 *    notice, this list of conditions and the following disclaimer
+
 *    in this position and unchanged.
+
 * 2. Redistributions in binary form must reproduce the above copyright
+
 *    notice, this list of conditions and the following disclaimer in the
+
 *    documentation and/or other materials provided with the distribution.
+
 * 
+
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
+
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
+
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
 */
+

+
#include <sysexits.h>
+
#include <stdio.h>
+

+
#include <pkg.h>
+

+
#include "pkgcli.h"
+

+
void
+
usage_ssh(void)
+
{
+
	fprintf(stderr, "usage: pkg ssh <ssh-path>\n\n");
+
	fprintf(stderr, "For more information see 'pkg help ssh'.\n");
+
}
+

+
int
+
exec_ssh(int argc, char **argv)
+
{
+
	if (argc != 2) {
+
		usage_ssh();
+
		return (EX_USAGE);
+
	}
+

+
	if (pkg_sshserve(argv[1]) != EPKG_OK)
+
		return (EX_SOFTWARE);
+

+
	return (EX_OK);
+
}