Browse Source

Factor out DUID identification code so that it can easily be reused.

ok krw@
OPENBSD_4_9
jsing 14 years ago
parent
commit
972351062d
4 changed files with 48 additions and 29 deletions
  1. +4
    -4
      src/lib/libutil/Makefile
  2. +40
    -0
      src/lib/libutil/duid.c
  3. +2
    -24
      src/lib/libutil/opendev.c
  4. +2
    -1
      src/lib/libutil/util.h

+ 4
- 4
src/lib/libutil/Makefile View File

@ -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


+ 40
- 0
src/lib/libutil/duid.c View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2010 Joel Sing <jsing@openbsd.org>
*
* 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 <string.h>
#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;
}

+ 2
- 24
src/lib/libutil/opendev.c View File

@ -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));


+ 2
- 1
src/lib/libutil/util.h View File

@ -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_ */

Loading…
Cancel
Save