File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / sudo / plugins / sudoers / prompt.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Sun Jun 15 16:12:54 2014 UTC (10 years ago) by misho
Branches: sudo, MAIN
CVS tags: v1_8_10p3_0, v1_8_10p3, HEAD
sudo v 1.8.10p3

    1: /*
    2:  * Copyright (c) 1993-1996,1998-2005, 2007-2013
    3:  *	Todd C. Miller <Todd.Miller@courtesan.com>
    4:  *
    5:  * Permission to use, copy, modify, and distribute this software for any
    6:  * purpose with or without fee is hereby granted, provided that the above
    7:  * copyright notice and this permission notice appear in all copies.
    8:  *
    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
   15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   16:  *
   17:  * Sponsored in part by the Defense Advanced Research Projects
   18:  * Agency (DARPA) and Air Force Research Laboratory, Air Force
   19:  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
   20:  */
   21: 
   22: #include <config.h>
   23: 
   24: #include <sys/types.h>
   25: #include <stdio.h>
   26: #ifdef STDC_HEADERS
   27: # include <stdlib.h>
   28: # include <stddef.h>
   29: #else
   30: # ifdef HAVE_STDLIB_H
   31: #  include <stdlib.h>
   32: # endif
   33: #endif /* STDC_HEADERS */
   34: #ifdef HAVE_STRING_H
   35: # include <string.h>
   36: #endif /* HAVE_STRING_H */
   37: #ifdef HAVE_STRINGS_H
   38: # include <strings.h>
   39: #endif /* HAVE_STRINGS_H */
   40: #include <pwd.h>
   41: #include <grp.h>
   42: 
   43: #include "sudoers.h"
   44: 
   45: /*
   46:  * Expand %h and %u escapes in the prompt and pass back the dynamically
   47:  * allocated result.  Returns the same string if there are no escapes.
   48:  */
   49: char *
   50: expand_prompt(const char *old_prompt, const char *auth_user)
   51: {
   52:     size_t len, n;
   53:     int subst;
   54:     const char *p;
   55:     char *np, *new_prompt, *endp;
   56:     debug_decl(expand_prompt, SUDO_DEBUG_AUTH)
   57: 
   58:     /* How much space do we need to malloc for the prompt? */
   59:     subst = 0;
   60:     for (p = old_prompt, len = strlen(old_prompt); *p; p++) {
   61: 	if (p[0] =='%') {
   62: 	    switch (p[1]) {
   63: 		case 'h':
   64: 		    p++;
   65: 		    len += strlen(user_shost) - 2;
   66: 		    subst = 1;
   67: 		    break;
   68: 		case 'H':
   69: 		    p++;
   70: 		    len += strlen(user_host) - 2;
   71: 		    subst = 1;
   72: 		    break;
   73: 		case 'p':
   74: 		    p++;
   75: 		    len += strlen(auth_user) - 2;
   76: 		    subst = 1;
   77: 		    break;
   78: 		case 'u':
   79: 		    p++;
   80: 		    len += strlen(user_name) - 2;
   81: 		    subst = 1;
   82: 		    break;
   83: 		case 'U':
   84: 		    p++;
   85: 		    len += strlen(runas_pw->pw_name) - 2;
   86: 		    subst = 1;
   87: 		    break;
   88: 		case '%':
   89: 		    p++;
   90: 		    len--;
   91: 		    subst = 1;
   92: 		    break;
   93: 		default:
   94: 		    break;
   95: 	    }
   96: 	}
   97:     }
   98: 
   99:     if (subst) {
  100: 	new_prompt = emalloc(++len);
  101: 	endp = new_prompt + len;
  102: 	for (p = old_prompt, np = new_prompt; *p; p++) {
  103: 	    if (p[0] =='%') {
  104: 		switch (p[1]) {
  105: 		    case 'h':
  106: 			p++;
  107: 			n = strlcpy(np, user_shost, np - endp);
  108: 			if (n >= (size_t)(np - endp))
  109: 			    goto oflow;
  110: 			np += n;
  111: 			continue;
  112: 		    case 'H':
  113: 			p++;
  114: 			n = strlcpy(np, user_host, np - endp);
  115: 			if (n >= (size_t)(np - endp))
  116: 			    goto oflow;
  117: 			np += n;
  118: 			continue;
  119: 		    case 'p':
  120: 			p++;
  121: 			n = strlcpy(np, auth_user, np - endp);
  122: 			if (n >= (size_t)(np - endp))
  123: 				goto oflow;
  124: 			np += n;
  125: 			continue;
  126: 		    case 'u':
  127: 			p++;
  128: 			n = strlcpy(np, user_name, np - endp);
  129: 			if (n >= (size_t)(np - endp))
  130: 			    goto oflow;
  131: 			np += n;
  132: 			continue;
  133: 		    case 'U':
  134: 			p++;
  135: 			n = strlcpy(np,  runas_pw->pw_name, np - endp);
  136: 			if (n >= (size_t)(np - endp))
  137: 			    goto oflow;
  138: 			np += n;
  139: 			continue;
  140: 		    case '%':
  141: 			/* convert %% -> % */
  142: 			p++;
  143: 			break;
  144: 		    default:
  145: 			/* no conversion */
  146: 			break;
  147: 		}
  148: 	    }
  149: 	    *np++ = *p;
  150: 	    if (np >= endp)
  151: 		goto oflow;
  152: 	}
  153: 	*np = '\0';
  154:     } else
  155: 	new_prompt = estrdup(old_prompt);
  156: 
  157:     debug_return_str(new_prompt);
  158: 
  159: oflow:
  160:     /* We pre-allocate enough space, so this should never happen. */
  161:     fatalx(U_("internal error, %s overflow"), "expand_prompt()");
  162: }

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