/src/sudo/plugins/sudoers/sethost.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2018-2023 Todd C. Miller <Todd.Miller@sudo.ws> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | /* |
20 | | * This is an open source non-commercial project. Dear PVS-Studio, please check it. |
21 | | * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com |
22 | | */ |
23 | | |
24 | | #include <config.h> |
25 | | |
26 | | #include <errno.h> |
27 | | #include <stdio.h> |
28 | | #include <stdlib.h> |
29 | | #include <string.h> |
30 | | |
31 | | #include <sudoers.h> |
32 | | |
33 | | /* |
34 | | * Set ctx->user.host. ctx->user.shost, ctx->runas.host and ctx->runas.shost |
35 | | * based on the local and remote host names. If host is NULL, the local |
36 | | * host name is used. If remhost is NULL, the same value as host is used. |
37 | | */ |
38 | | bool |
39 | | sudoers_sethost(struct sudoers_context *ctx, const char *host, |
40 | | const char *remhost) |
41 | 22.1k | { |
42 | 22.1k | char *cp; |
43 | 22.1k | debug_decl(sudoers_sethost, SUDOERS_DEBUG_UTIL); |
44 | | |
45 | 22.1k | if (ctx->user.shost != ctx->user.host) |
46 | 0 | free(ctx->user.shost); |
47 | 22.1k | free(ctx->user.host); |
48 | 22.1k | ctx->user.host = NULL; |
49 | 22.1k | ctx->user.shost = NULL; |
50 | | |
51 | 22.1k | if (host == NULL) { |
52 | 0 | ctx->user.host = sudo_gethostname(); |
53 | 0 | if (ctx->user.host == NULL && errno != ENOMEM) |
54 | 0 | ctx->user.host = strdup("localhost"); |
55 | 22.1k | } else { |
56 | 22.1k | ctx->user.host = strdup(host); |
57 | 22.1k | } |
58 | 22.1k | if (ctx->user.host == NULL) |
59 | 0 | goto oom; |
60 | 22.1k | if ((cp = strchr(ctx->user.host, '.')) != NULL) { |
61 | 357 | ctx->user.shost = strndup(ctx->user.host, |
62 | 357 | (size_t)(cp - ctx->user.host)); |
63 | 357 | if (ctx->user.shost == NULL) |
64 | 0 | goto oom; |
65 | 21.7k | } else { |
66 | 21.7k | ctx->user.shost = ctx->user.host; |
67 | 21.7k | } |
68 | | |
69 | 22.1k | if (ctx->runas.shost != ctx->runas.host) |
70 | 0 | free(ctx->runas.shost); |
71 | 22.1k | free(ctx->runas.host); |
72 | 22.1k | ctx->runas.host = NULL; |
73 | 22.1k | ctx->runas.shost = NULL; |
74 | | |
75 | 22.1k | ctx->runas.host = strdup(remhost ? remhost : ctx->user.host); |
76 | 22.1k | if (ctx->runas.host == NULL) |
77 | 0 | goto oom; |
78 | 22.1k | if ((cp = strchr(ctx->runas.host, '.')) != NULL) { |
79 | 367 | ctx->runas.shost = strndup(ctx->runas.host, |
80 | 367 | (size_t)(cp - ctx->runas.host)); |
81 | 367 | if (ctx->runas.shost == NULL) |
82 | 0 | goto oom; |
83 | 21.7k | } else { |
84 | 21.7k | ctx->runas.shost = ctx->runas.host; |
85 | 21.7k | } |
86 | | |
87 | 22.1k | debug_return_bool(true); |
88 | 0 | oom: |
89 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
90 | 0 | debug_return_bool(false); |
91 | 0 | } |