From 972351062d87ab9803affa0dc4cf856d7411c1ef Mon Sep 17 00:00:00 2001 From: jsing <> Date: Mon, 15 Nov 2010 15:07:40 +0000 Subject: [PATCH] Factor out DUID identification code so that it can easily be reused. ok krw@ --- src/lib/libutil/Makefile | 8 ++++---- src/lib/libutil/duid.c | 40 +++++++++++++++++++++++++++++++++++++++ src/lib/libutil/opendev.c | 26 ++----------------------- src/lib/libutil/util.h | 3 ++- 4 files changed, 48 insertions(+), 29 deletions(-) create mode 100644 src/lib/libutil/duid.c diff --git a/src/lib/libutil/Makefile b/src/lib/libutil/Makefile index ed52e633..b2599743 100644 --- a/src/lib/libutil/Makefile +++ b/src/lib/libutil/Makefile @@ -1,11 +1,11 @@ -# $OpenBSD: Makefile,v 1.31 2010/05/26 16:44:32 nicm Exp $ +# $OpenBSD: Makefile,v 1.32 2010/11/15 15:07:40 jsing Exp $ # $NetBSD: Makefile,v 1.8 1996/05/16 07:03:28 thorpej Exp $ LIB= util WANTLINT= HDRS= util.h imsg.h -SRCS= check_expire.c getmaxpartitions.c getrawpartition.c login.c \ +SRCS= check_expire.c duid.c getmaxpartitions.c getrawpartition.c login.c \ login_tty.c logout.c logwtmp.c opendev.c passwd.c pty.c readlabel.c \ login_fbtab.c uucplock.c fparseln.c opendisk.c pidfile.c \ fmt_scaled.c imsg.c imsg-buffer.c @@ -61,8 +61,8 @@ MLINKS+=fmt_scaled.3 scan_scaled.3 includes: @cd ${.CURDIR}; for i in $(HDRS); do \ j="cmp -s $$i ${DESTDIR}/usr/include/$$i || \ - ${INSTALL} ${INSTALL_COPY} -o ${BINOWN} -g ${BINGRP} -m 444 $$i \ - ${DESTDIR}/usr/include"; \ + ${INSTALL} ${INSTALL_COPY} -o ${BINOWN} -g ${BINGRP} \ + -m 444 $$i ${DESTDIR}/usr/include"; \ echo $$j; \ eval "$$j"; \ done diff --git a/src/lib/libutil/duid.c b/src/lib/libutil/duid.c new file mode 100644 index 00000000..3e57ab31 --- /dev/null +++ b/src/lib/libutil/duid.c @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2010 Joel Sing + * + * 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. + */ + +#include + +#include "util.h" + +int +isduid(const char *duid, int dflags) +{ + char c; + int i; + + /* Basic format check. */ + if (!((strlen(duid) == 16 && (dflags & OPENDEV_PART)) || + (strlen(duid) == 18 && duid[16] == '.'))) + return 0; + + /* Check UID. */ + for (i = 0; i < 16; i++) { + c = duid[i]; + if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) + return 0; + } + + return 1; +} diff --git a/src/lib/libutil/opendev.c b/src/lib/libutil/opendev.c index 53c3c537..5856cbd2 100644 --- a/src/lib/libutil/opendev.c +++ b/src/lib/libutil/opendev.c @@ -1,4 +1,4 @@ -/* $OpenBSD: opendev.c,v 1.10 2010/06/28 19:12:29 chl Exp $ */ +/* $OpenBSD: opendev.c,v 1.11 2010/11/15 15:07:40 jsing Exp $ */ /* * Copyright (c) 2000, Todd C. Miller. All rights reserved. @@ -41,28 +41,6 @@ #include "util.h" -/* Returns 1 if a valid disklabel UID. */ -static int -valid_diskuid(const char *duid, int dflags) -{ - char c; - int i; - - /* Basic format check. */ - if (!((strlen(duid) == 16 && (dflags & OPENDEV_PART)) || - (strlen(duid) == 18 && duid[16] == '.'))) - return 0; - - /* Check UID. */ - for (i = 0; i < 16; i++) { - c = duid[i]; - if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) - return 0; - } - - return 1; -} - /* * This routine is a generic rewrite of the original code found in * disklabel(8). @@ -88,7 +66,7 @@ opendev(char *path, int oflags, int dflags, char **realpath) if ((slash = strchr(path, '/'))) fd = open(path, oflags); - else if (valid_diskuid(path, dflags)) { + else if (isduid(path, dflags)) { if ((fd = open("/dev/diskmap", oflags)) != -1) { bzero(&dm, sizeof(struct dk_diskmap)); strlcpy(namebuf, path, sizeof(namebuf)); diff --git a/src/lib/libutil/util.h b/src/lib/libutil/util.h index aac35cf7..c0b5bb68 100644 --- a/src/lib/libutil/util.h +++ b/src/lib/libutil/util.h @@ -1,4 +1,4 @@ -/* $OpenBSD: util.h,v 1.27 2006/06/14 02:14:25 krw Exp $ */ +/* $OpenBSD: util.h,v 1.28 2010/11/15 15:07:40 jsing Exp $ */ /* $NetBSD: util.h,v 1.2 1996/05/16 07:00:22 thorpej Exp $ */ /*- @@ -114,6 +114,7 @@ int uu_lock_txfr(const char *_ttyname, pid_t _pid); int uu_unlock(const char *_ttyname); int fmt_scaled(long long number, char *result); int scan_scaled(char *scaled, long long *result); +int isduid(const char *, int); __END_DECLS #endif /* !_UTIL_H_ */