summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbenno <benno@openbsd.org>2019-02-12 19:04:52 +0000
committerbenno <benno@openbsd.org>2019-02-12 19:04:52 +0000
commit997d99f978acb9cb2bc9c99a8e022b60bcdb4cfd (patch)
tree81809742939561775398ebc49ebbbaecf5070758
parentsync (diff)
downloadwireguard-openbsd-997d99f978acb9cb2bc9c99a8e022b60bcdb4cfd.tar.xz
wireguard-openbsd-997d99f978acb9cb2bc9c99a8e022b60bcdb4cfd.zip
sync
commit bc997c5d70dbe402903c7ce0c915c7fe3744a754 Author: kristaps <> Date: Mon Feb 11 23:37:41 2019 +0000 Move identifier code into ids.c.
-rw-r--r--usr.bin/rsync/Makefile4
-rw-r--r--usr.bin/rsync/extern.h17
-rw-r--r--usr.bin/rsync/flist.c212
3 files changed, 29 insertions, 204 deletions
diff --git a/usr.bin/rsync/Makefile b/usr.bin/rsync/Makefile
index 0cd7a1e799b..f38aae5cbba 100644
--- a/usr.bin/rsync/Makefile
+++ b/usr.bin/rsync/Makefile
@@ -1,7 +1,7 @@
-# $OpenBSD: Makefile,v 1.3 2019/02/11 16:36:34 deraadt Exp $
+# $OpenBSD: Makefile,v 1.4 2019/02/12 19:04:52 benno Exp $
PROG= rsync
-SRCS= blocks.c child.c client.c downloader.c fargs.c flist.c hash.c \
+SRCS= blocks.c child.c client.c downloader.c fargs.c flist.c hash.c ids.c \
io.c log.c md4.c mkpath.c receiver.c sender.c server.c session.c \
socket.c symlinks.c uploader.c main.c
LDADD+= -lm
diff --git a/usr.bin/rsync/extern.h b/usr.bin/rsync/extern.h
index 3d4c828fff4..350351754c9 100644
--- a/usr.bin/rsync/extern.h
+++ b/usr.bin/rsync/extern.h
@@ -1,4 +1,4 @@
-/* $Id: extern.h,v 1.5 2019/02/12 18:06:25 benno Exp $ */
+/* $Id: extern.h,v 1.6 2019/02/12 19:04:52 benno Exp $ */
/*
* Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
*
@@ -151,6 +151,15 @@ struct sess {
int mplex_writes; /* multiplexing writes? */
};
+/*
+ * Combination of name and numeric id for groups and users.
+ */
+struct ident {
+ int32_t id; /* the gid_t or uid_t */
+ int32_t mapped; /* if receiving, the mapped gid */
+ char *name; /* resolved name */
+};
+
struct download;
struct upload;
@@ -292,6 +301,12 @@ char *symlinkat_read(struct sess *, int, const char *);
int sess_stats_send(struct sess *, int);
int sess_stats_recv(struct sess *, int);
+void idents_free(struct ident *, size_t);
+void idents_gid_remap(struct sess *, struct ident *, size_t);
+int idents_gid_add(struct sess *, struct ident **, size_t *, gid_t);
+int idents_send(struct sess *, int, const struct ident *, size_t);
+int idents_recv(struct sess *, int, struct ident **, size_t *);
+
__END_DECLS
#endif /*!EXTERN_H*/
diff --git a/usr.bin/rsync/flist.c b/usr.bin/rsync/flist.c
index f58cb36313b..980433fa424 100644
--- a/usr.bin/rsync/flist.c
+++ b/usr.bin/rsync/flist.c
@@ -1,4 +1,4 @@
-/* $Id: flist.c,v 1.7 2019/02/12 19:02:06 benno Exp $ */
+/* $Id: flist.c,v 1.8 2019/02/12 19:04:52 benno Exp $ */
/*
* Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
*
@@ -21,7 +21,6 @@
#include <errno.h>
#include <fcntl.h>
#include <fts.h>
-#include <grp.h>
#include <inttypes.h>
#include <search.h>
#include <stdio.h>
@@ -50,110 +49,6 @@
#define FLIST_TIME_SAME 0x0080 /* time is repeat */
/*
- * Combination of name and numeric id for groups and users.
- */
-struct ident {
- int32_t id; /* the gid_t or uid_t */
- int32_t mapped; /* if receiving, the mapped gid */
- char *name; /* resolved name */
-};
-
-/*
- * Free a list of struct ident previously allocated with flist_gid_add().
- * Does nothing if the pointer is NULL.
- */
-static void
-flist_ident_free(struct ident *p, size_t sz)
-{
- size_t i;
-
- if (NULL == p)
- return;
- for (i = 0; i < sz; i++)
- free(p[i].name);
- free(p);
-}
-
-/*
- * Given a list of groups from the remote host, fill in our local
- * identifiers of the same names.
- * Use the remote numeric identifier if we can't find the group OR the
- * group has identifier zero.
- */
-static void
-flist_gid_remap(struct sess *sess, struct ident *gids, size_t gidsz)
-{
- size_t i;
- struct group *grp;
-
- for (i = 0; i < gidsz; i++) {
- if (NULL == (grp = getgrnam(gids[i].name)))
- gids[i].mapped = gids[i].id;
- else if (0 == grp->gr_gid)
- gids[i].mapped = gids[i].id;
- else
- gids[i].mapped = grp->gr_gid;
- LOG4(sess, "remapped group %s: %" PRId32 " -> %" PRId32,
- gids[i].name, gids[i].id, gids[i].mapped);
- }
-}
-
-/*
- * If "gid" is not part of the list of known groups, add it.
- * This also verifies that the group name isn't too long.
- * Return zero on failure, non-zero on success.
- */
-static int
-flist_gid_add(struct sess *sess, struct ident **gids, size_t *gidsz, gid_t gid)
-{
- struct group *grp;
- size_t i, sz;
- void *pp;
-
- for (i = 0; i < *gidsz; i++)
- if ((*gids)[i].id == (int32_t)gid)
- return 1;
-
- /*
- * Look us up in /etc/group.
- * Make sure that the group name length is sane: we transmit it
- * using a single byte.
- */
-
- assert(i == *gidsz);
- if (NULL == (grp = getgrgid(gid))) {
- ERR(sess, "%u: unknown gid", gid);
- return 0;
- } else if ((sz = strlen(grp->gr_name)) > UINT8_MAX) {
- ERRX(sess, "%u: group name too long: %s", gid, grp->gr_name);
- return 0;
- } else if (0 == sz) {
- ERRX(sess, "%u: group name zero-length", gid);
- return 0;
- }
-
- /* Add the group to the array. */
-
- pp = reallocarray(*gids, *gidsz + 1, sizeof(struct ident));
- if (NULL == pp) {
- ERR(sess, "reallocarray");
- return 0;
- }
- *gids = pp;
- (*gids)[*gidsz].id = gid;
- (*gids)[*gidsz].name = strdup(grp->gr_name);
- if (NULL == (*gids)[*gidsz].name) {
- ERR(sess, "strdup");
- return 0;
- }
-
- LOG4(sess, "adding group to list: %s (%u)",
- (*gids)[*gidsz].name, (*gids)[*gidsz].id);
- (*gidsz)++;
- return 1;
-}
-
-/*
* Requied way to sort a filename list.
*/
static int
@@ -337,42 +232,6 @@ flist_free(struct flist *f, size_t sz)
}
/*
- * Send a list of struct ident.
- * See flist_recv_ident().
- * We should only do this if we're preserving gids/uids.
- * Return zero on failure, non-zero on success.
- */
-static int
-flist_send_ident(struct sess *sess,
- int fd, const struct ident *ids, size_t idsz)
-{
- size_t i, sz;
-
- for (i = 0; i < idsz; i++) {
- assert(NULL != ids[i].name);
- sz = strlen(ids[i].name);
- assert(sz > 0 && sz <= UINT8_MAX);
- if (!io_write_int(sess, fd, ids[i].id)) {
- ERRX1(sess, "io_write_int");
- return 0;
- } else if (!io_write_byte(sess, fd, sz)) {
- ERRX1(sess, "io_write_byte");
- return 0;
- } else if (!io_write_buf(sess, fd, ids[i].name, sz)) {
- ERRX1(sess, "io_write_byte");
- return 0;
- }
- }
-
- if (!io_write_int(sess, fd, 0)) {
- ERRX1(sess, "io_write_int");
- return 0;
- }
-
- return 1;
-}
-
-/*
* Serialise our file list (which may be zero-length) to the wire.
* Makes sure that the receiver isn't going to block on sending us
* return messages on the log channel.
@@ -457,8 +316,8 @@ flist_send(struct sess *sess, int fdin, int fdout, const struct flist *fl,
ERRX1(sess, "io_write_int");
goto out;
}
- if (!flist_gid_add(sess, &gids, &gidsz, f->st.gid)) {
- ERRX1(sess, "flist_gid_add");
+ if (!idents_gid_add(sess, &gids, &gidsz, f->st.gid)) {
+ ERRX1(sess, "idents_gid_add");
goto out;
}
}
@@ -494,15 +353,15 @@ flist_send(struct sess *sess, int fdin, int fdout, const struct flist *fl,
if (sess->opts->preserve_gids) {
LOG2(sess, "sending gid list: %zu", gidsz);
- if (!flist_send_ident(sess, fdout, gids, gidsz)) {
- ERRX1(sess, "flist_send_ident");
+ if (!idents_send(sess, fdout, gids, gidsz)) {
+ ERRX1(sess, "idents_send");
goto out;
}
}
rc = 1;
out:
- flist_ident_free(gids, gidsz);
+ idents_free(gids, gidsz);
return rc;
}
@@ -671,55 +530,6 @@ flist_append(struct sess *sess, struct flist *f, struct stat *st,
}
/*
- * Receive a list of struct ident.
- * See flist_send_ident().
- * We should only do this if we're preserving gids/uids.
- * Return zero on failure, non-zero on success.
- */
-static int
-flist_recv_ident(struct sess *sess,
- int fd, struct ident **ids, size_t *idsz)
-{
- int32_t id;
- uint8_t sz;
- void *pp;
-
- for (;;) {
- if (!io_read_int(sess, fd, &id)) {
- ERRX1(sess, "io_read_int");
- return 0;
- } else if (0 == id)
- break;
-
- pp = reallocarray(*ids,
- *idsz + 1, sizeof(struct ident));
- if (NULL == pp) {
- ERR(sess, "reallocarray");
- return 0;
- }
- *ids = pp;
- memset(&(*ids)[*idsz], 0, sizeof(struct ident));
- if (!io_read_byte(sess, fd, &sz)) {
- ERRX1(sess, "io_read_byte");
- return 0;
- }
- (*ids)[*idsz].id = id;
- (*ids)[*idsz].name = calloc(sz + 1, 1);
- if (NULL == (*ids)[*idsz].name) {
- ERR(sess, "calloc");
- return 0;
- }
- if (!io_read_buf(sess, fd, (*ids)[*idsz].name, sz)) {
- ERRX1(sess, "io_read_buf");
- return 0;
- }
- (*idsz)++;
- }
-
- return 1;
-}
-
-/*
* Receive a file list from the wire, filling in length "sz" (which may
* possibly be zero) and list "flp" on success.
* Return zero on failure, non-zero on success.
@@ -851,12 +661,12 @@ flist_recv(struct sess *sess, int fd, struct flist **flp, size_t *sz)
*/
if (sess->opts->preserve_gids) {
- if (!flist_recv_ident(sess, fd, &gids, &gidsz)) {
- ERRX1(sess, "flist_recv_ident");
+ if (!idents_recv(sess, fd, &gids, &gidsz)) {
+ ERRX1(sess, "idents_recv");
goto out;
}
LOG2(sess, "received gid list: %zu", gidsz);
- flist_gid_remap(sess, gids, gidsz);
+ idents_gid_remap(sess, gids, gidsz);
}
/* Remember to order the received list. */
@@ -879,11 +689,11 @@ flist_recv(struct sess *sess, int fd, struct flist **flp, size_t *sz)
}
}
- flist_ident_free(gids, gidsz);
+ idents_free(gids, gidsz);
return 1;
out:
flist_free(fl, flsz);
- flist_ident_free(gids, gidsz);
+ idents_free(gids, gidsz);
*sz = 0;
*flp = NULL;
return 0;