diff options
author | Matthieu Herrb <matthieu@cvs.openbsd.org> | 2011-09-17 11:51:58 +0000 |
---|---|---|
committer | Matthieu Herrb <matthieu@cvs.openbsd.org> | 2011-09-17 11:51:58 +0000 |
commit | f0bd47092943cb33163b44510dab1aca30a52280 (patch) | |
tree | c11cff2530e5088484fbcdd85e88f37fc5d6a40b /app/xterm/xstrings.c | |
parent | bbace3c9e604f0dcf85460e95f8e5dbd985c4f6e (diff) |
Update to xterm-275. Tested by jasper@.
Diffstat (limited to 'app/xterm/xstrings.c')
-rw-r--r-- | app/xterm/xstrings.c | 179 |
1 files changed, 144 insertions, 35 deletions
diff --git a/app/xterm/xstrings.c b/app/xterm/xstrings.c index a2be441b1..5fe083536 100644 --- a/app/xterm/xstrings.c +++ b/app/xterm/xstrings.c @@ -1,36 +1,34 @@ -/* $XTermId: xstrings.c,v 1.40 2011/08/25 08:55:55 tom Exp $ */ +/* $XTermId: xstrings.c,v 1.47 2011/09/11 20:20:12 tom Exp $ */ -/************************************************************ - -Copyright 2000-2010,2011 by Thomas E. Dickey - - All Rights Reserved - -Permission is hereby granted, free of charge, to any person obtaining a -copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be included -in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -Except as contained in this notice, the name(s) of the above copyright -holders shall not be used in advertising or otherwise to promote the -sale, use or other dealings in this Software without prior written -authorization. - -********************************************************/ +/* + * Copyright 2000-2010,2011 by Thomas E. Dickey + * + * All Rights Reserved + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name(s) of the above copyright + * holders shall not be used in advertising or otherwise to promote the + * sale, use or other dealings in this Software without prior written + * authorization. + */ #include <xterm.h> @@ -41,6 +39,16 @@ authorization. #include <xstrings.h> +static void +alloc_pw(struct passwd *target, struct passwd *source) +{ + *target = *source; + /* we care only about these strings */ + target->pw_dir = x_strdup(source->pw_dir); + target->pw_name = x_strdup(source->pw_name); + target->pw_shell = x_strdup(source->pw_shell); +} + void x_appendargv(char **target, char **source) { @@ -137,8 +145,109 @@ x_getenv(const char *name) } /* + * Call this with in_out pointing to data filled in by x_getpwnam() or by + * x_getpwnam(). It finds the user's logon name, if possible. As a side + * effect, it updates in_out to fill in possibly more-relevant data, i.e., + * in case there is more than one alias for the same uid. + */ +char * +x_getlogin(uid_t uid, struct passwd *in_out) +{ + char *login_name = NULL; + +#ifdef HAVE_GETLOGIN + login_name = getlogin(); +#endif + + /* + * Of course getlogin() will fail if we're started from a window-manager, + * since there's no controlling terminal to fuss with. In that case, try + * to get something useful from the user's $LOGNAME or $USER environment + * variables. + */ + if (login_name == NULL) { + login_name = x_getenv("LOGNAME"); + if (login_name == NULL) + login_name = x_getenv("USER"); + } + + /* + * If the logon-name differs from the value we get by looking in the + * password file, check if it does correspond to the same uid. If so, + * allow that as an alias for the uid. + */ + if (login_name != NULL + && strcmp(login_name, in_out->pw_name)) { + struct passwd pw2; + + if (x_getpwnam(login_name, &pw2)) { + uid_t uid2 = pw2.pw_uid; + struct passwd pw3; + + if (!x_getpwuid(uid, &pw3) + || (uid_t) pw3.pw_uid != uid2) { + login_name = NULL; + } else { + /* use the other passwd-data including shell */ + alloc_pw(in_out, &pw2); + } + } else { + (void) x_getpwuid(uid, in_out); + } + } + + if (login_name == NULL) + login_name = in_out->pw_name; + if (login_name != NULL) + login_name = x_strdup(login_name); + + return login_name; +} + +/* + * Simpler than getpwnam_r, retrieves the passwd result by name and stores the + * result via the given pointer. On failure, wipes the data to prevent use. + */ +Boolean +x_getpwnam(const char *name, struct passwd * result) +{ + struct passwd *ptr = getpwnam(name); + Boolean code; + + if (OkPasswd(ptr)) { + code = True; + alloc_pw(result, ptr); + } else { + code = False; + memset(result, 0, sizeof(*result)); + } + return code; +} + +/* + * Simpler than getpwuid_r, retrieves the passwd result by uid and stores the + * result via the given pointer. On failure, wipes the data to prevent use. + */ +Boolean +x_getpwuid(uid_t uid, struct passwd * result) +{ + struct passwd *ptr = getpwuid((uid_t) uid); + Boolean code; + + if (OkPasswd(ptr)) { + code = True; + alloc_pw(result, ptr); + } else { + code = False; + memset(result, 0, sizeof(*result)); + } + return code; +} + +/* * Decode a single hex "nibble", returning the nibble as 0-15, or -1 on error. - */ int + */ +int x_hex2int(int c) { if (c >= '0' && c <= '9') @@ -204,7 +313,7 @@ x_splitargs(const char *command) for (n = count = 0, state = 0; command[n] != '\0'; ++n) { switch (state) { case 0: - if (!isspace(command[n])) { + if (!isspace(CharOf(command[n]))) { state = 1; if (pass) result[count] = blob + n; @@ -214,7 +323,7 @@ x_splitargs(const char *command) } break; case 1: - if (isspace(command[n])) { + if (isspace(CharOf(command[n]))) { blob[n] = '\0'; state = 0; } |