File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / sudo / common / atoid.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Mon Oct 14 07:56:33 2013 UTC (10 years, 11 months ago) by misho
Branches: sudo, MAIN
CVS tags: v1_8_8p0, v1_8_8, HEAD
v 1.8.8

    1: /*
    2:  * Copyright (c) 2013 Todd C. Miller <Todd.Miller@courtesan.com>
    3:  *
    4:  * Permission to use, copy, modify, and distribute this software for any
    5:  * purpose with or without fee is hereby granted, provided that the above
    6:  * copyright notice and this permission notice appear in all copies.
    7:  *
    8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
   14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   15:  */
   16: 
   17: #include <config.h>
   18: 
   19: #include <sys/types.h>
   20: 
   21: #include <stdio.h>
   22: #ifdef STDC_HEADERS
   23: # include <stdlib.h>
   24: # include <stddef.h>
   25: #else
   26: # ifdef HAVE_STDLIB_H
   27: #  include <stdlib.h>
   28: # endif
   29: #endif /* STDC_HEADERS */
   30: #if defined(HAVE_STDINT_H)
   31: # include <stdint.h>
   32: #elif defined(HAVE_INTTYPES_H)
   33: # include <inttypes.h>
   34: #endif
   35: #ifdef HAVE_STDBOOL_H
   36: # include <stdbool.h>
   37: #else
   38: # include "compat/stdbool.h"
   39: #endif
   40: #include <errno.h>
   41: #include <limits.h>
   42: 
   43: #define DEFAULT_TEXT_DOMAIN	"sudo"
   44: #include "gettext.h"
   45: 
   46: #include "missing.h"
   47: #include "sudo_debug.h"
   48: 
   49: /*
   50:  * Parse a uid/gid in string form.
   51:  * If sep is non-NULL, it contains valid separator characters (e.g. comma, space)
   52:  * If endp is non-NULL it is set to the next char after the ID.
   53:  * On success, returns the parsed ID and clears errstr.
   54:  * On error, returns 0 and sets errstr.
   55:  */
   56: id_t
   57: atoid(const char *p, const char *sep, char **endp, const char **errstr)
   58: {
   59:     char *ep;
   60:     id_t rval = 0;
   61:     bool valid = false;
   62:     debug_decl(atoid, SUDO_DEBUG_UTIL)
   63: 
   64:     if (sep == NULL)
   65: 	sep = "";
   66:     errno = 0;
   67:     if (*p == '-') {
   68: 	long lval = strtol(p, &ep, 10);
   69: 	if (ep != p) {
   70: 	    /* check for valid separator (including '\0') */
   71: 	    do {
   72: 		if (*ep == *sep)
   73: 		    valid = true;
   74: 	    } while (*sep++ != '\0');
   75: 	}
   76: 	if (!valid) {
   77: 	    *errstr = N_("invalid value");
   78: 	    errno = EINVAL;
   79: 	    goto done;
   80: 	}
   81: 	if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
   82: 	    (lval > INT_MAX || lval < INT_MIN)) {
   83: 	    errno = ERANGE;
   84: 	    *errstr = N_("value out of range");
   85: 	    goto done;
   86: 	}
   87: 	rval = (id_t)lval;
   88: 	*errstr = NULL;
   89:     } else {
   90: 	unsigned long ulval = strtoul(p, &ep, 10);
   91: 	if (ep != p) {
   92: 	    /* check for valid separator (including '\0') */
   93: 	    do {
   94: 		if (*ep == *sep)
   95: 		    valid = true;
   96: 	    } while (*sep++ != '\0');
   97: 	}
   98: 	if (!valid) {
   99: 	    *errstr = N_("invalid value");
  100: 	    errno = EINVAL;
  101: 	    goto done;
  102: 	}
  103: 	if ((errno == ERANGE && ulval == ULONG_MAX) || ulval > UINT_MAX) {
  104: 	    errno = ERANGE;
  105: 	    *errstr = N_("value too large");
  106: 	    goto done;
  107: 	}
  108: 	rval = (id_t)ulval;
  109: 	*errstr = NULL;
  110:     }
  111:     if (endp != NULL)
  112: 	*endp = ep;
  113: done:
  114:     debug_return_int(rval);
  115: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>