From: martijn Date: Sun, 29 Aug 2021 13:27:11 +0000 (+0000) Subject: Add ober_dup. Needed for upcoming SNMPv3 support for trap receiver in X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=63beb5678c5274b2252aa3afb789b1d8a703ec1f;p=openbsd Add ober_dup. Needed for upcoming SNMPv3 support for trap receiver in snmpd(8). OK jmatthew@ OK deraadt@ for bumping libutil now. --- diff --git a/lib/libutil/Symbols.map b/lib/libutil/Symbols.map index a9e2059a3d6..4aa7d1462d6 100644 --- a/lib/libutil/Symbols.map +++ b/lib/libutil/Symbols.map @@ -65,6 +65,7 @@ ober_add_set; ober_add_string; ober_calc_len; + ober_dup; ober_free; ober_free_element; ober_free_elements; diff --git a/lib/libutil/ber.c b/lib/libutil/ber.c index 175e2bb1960..500c1469d27 100644 --- a/lib/libutil/ber.c +++ b/lib/libutil/ber.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ber.c,v 1.21 2021/02/22 17:15:02 martijn Exp $ */ +/* $OpenBSD: ber.c,v 1.22 2021/08/29 13:27:11 martijn Exp $ */ /* * Copyright (c) 2007, 2012 Reyk Floeter @@ -926,6 +926,43 @@ ober_getpos(struct ber_element *elm) return elm->be_offs; } +struct ber_element * +ober_dup(struct ber_element *orig) +{ + struct ber_element *new; + + if ((new = malloc(sizeof(*new))) == NULL) + return NULL; + memcpy(new, orig, sizeof(*new)); + new->be_next = NULL; + new->be_sub = NULL; + + if (orig->be_next != NULL) { + if ((new->be_next = ober_dup(orig->be_next)) == NULL) + goto fail; + } + if (orig->be_encoding == BER_TYPE_SEQUENCE || + orig->be_encoding == BER_TYPE_SET) { + if (orig->be_sub != NULL) { + if ((new->be_sub = ober_dup(orig->be_sub)) == NULL) + goto fail; + } + } else if (orig->be_encoding == BER_TYPE_OCTETSTRING || + orig->be_encoding == BER_TYPE_BITSTRING || + orig->be_encoding == BER_TYPE_OBJECT) { + if (orig->be_val != NULL) { + if ((new->be_val = malloc(orig->be_len)) == NULL) + goto fail; + memcpy(new->be_val, orig->be_val, orig->be_len); + } + } else + new->be_numeric = orig->be_numeric; + return new; + fail: + ober_free_elements(new); + return NULL; +} + void ober_free_element(struct ber_element *root) { diff --git a/lib/libutil/ber.h b/lib/libutil/ber.h index e89deb46c2b..6d12f2f52f5 100644 --- a/lib/libutil/ber.h +++ b/lib/libutil/ber.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ber.h,v 1.3 2019/12/31 10:34:14 martijn Exp $ */ +/* $OpenBSD: ber.h,v 1.4 2021/08/29 13:27:11 martijn Exp $ */ /* * Copyright (c) 2007, 2012 Reyk Floeter @@ -137,6 +137,7 @@ ssize_t ober_write_elements(struct ber *, struct ber_element *); void ober_set_readbuf(struct ber *, void *, size_t); struct ber_element *ober_read_elements(struct ber *, struct ber_element *); off_t ober_getpos(struct ber_element *); +struct ber_element *ober_dup(struct ber_element *); void ober_free_element(struct ber_element *); void ober_free_elements(struct ber_element *); size_t ober_calc_len(struct ber_element *); diff --git a/lib/libutil/ober_set_header.3 b/lib/libutil/ober_set_header.3 index b1f4fab5d34..c067eda214e 100644 --- a/lib/libutil/ober_set_header.3 +++ b/lib/libutil/ober_set_header.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: ober_set_header.3,v 1.2 2021/03/12 05:18:01 jsg Exp $ +.\" $OpenBSD: ober_set_header.3,v 1.3 2021/08/29 13:27:11 martijn Exp $ .\" .\" Copyright (c) 2007, 2012 Reyk Floeter .\" @@ -14,7 +14,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: March 12 2021 $ +.Dd $Mdocdate: August 29 2021 $ .Dt OBER_SET_HEADER 3 .Os .Sh NAME @@ -23,6 +23,7 @@ .Nm ober_set_writecallback , .Nm ober_link_elements , .Nm ober_replace_elements , +.Nm ober_dup , .Nm ober_unlink_elements , .Nm ober_free_element , .Nm ober_free_elements @@ -45,6 +46,8 @@ .Ft "void" .Fn "ober_replace_elements" "struct ber_element *prev" "struct ber_element *elm" .Ft "struct ber_element *" +.Ft "struct ber_element *" +.Fn "ober_dup" "struct ber_element *orig" .Fn "ober_unlink_elements" "struct ber_element *prev" .Ft "void" .Fn "ober_free_element" "struct ber_element *root" @@ -101,6 +104,9 @@ with and frees any dynamically allocated storage associated with .Fa prev . .Pp +.Fn ober_dup +duplicates an element and all linked elements. +.Pp .Fn ober_unlink_elements unlinks .Fa prev . @@ -117,6 +123,11 @@ returns the total length of a fully populated containing one or more .Vt ber_element . .Pp +.Fn ober_dup +returns a pointer to the duplicated element or +.Dv NULL +on error. +.Pp .Fn ober_unlink_elements returns a pointer to .Vt ber_element . diff --git a/lib/libutil/shlib_version b/lib/libutil/shlib_version index 77913220429..b9229d35170 100644 --- a/lib/libutil/shlib_version +++ b/lib/libutil/shlib_version @@ -1,2 +1,2 @@ major=15 -minor=0 +minor=1