/src/sudo/plugins/sudoers/sethost.c
Line | Count | Source |
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 | | #include <config.h> |
20 | | |
21 | | #include <errno.h> |
22 | | #include <stdio.h> |
23 | | #include <stdlib.h> |
24 | | #include <string.h> |
25 | | |
26 | | #include <sudoers.h> |
27 | | |
28 | | /* |
29 | | * Set ctx->user.host. ctx->user.shost, ctx->runas.host and ctx->runas.shost |
30 | | * based on the local and remote host names. If host is NULL, the local |
31 | | * host name is used. If remhost is NULL, the same value as host is used. |
32 | | */ |
33 | | bool |
34 | | sudoers_sethost(struct sudoers_context *ctx, const char *host, |
35 | | const char *remhost) |
36 | 18.1k | { |
37 | 18.1k | char *cp; |
38 | 18.1k | debug_decl(sudoers_sethost, SUDOERS_DEBUG_UTIL); |
39 | | |
40 | 18.1k | if (ctx->user.shost != ctx->user.host) |
41 | 0 | free(ctx->user.shost); |
42 | 18.1k | free(ctx->user.host); |
43 | 18.1k | ctx->user.host = NULL; |
44 | 18.1k | ctx->user.shost = NULL; |
45 | | |
46 | 18.1k | if (host == NULL) { |
47 | 0 | ctx->user.host = sudo_gethostname(); |
48 | 0 | if (ctx->user.host == NULL && errno != ENOMEM) |
49 | 0 | ctx->user.host = strdup("localhost"); |
50 | 18.1k | } else { |
51 | 18.1k | ctx->user.host = strdup(host); |
52 | 18.1k | } |
53 | 18.1k | if (ctx->user.host == NULL) |
54 | 0 | goto oom; |
55 | 18.1k | if ((cp = strchr(ctx->user.host, '.')) != NULL) { |
56 | 211 | ctx->user.shost = strndup(ctx->user.host, |
57 | 211 | (size_t)(cp - ctx->user.host)); |
58 | 211 | if (ctx->user.shost == NULL) |
59 | 0 | goto oom; |
60 | 17.9k | } else { |
61 | 17.9k | ctx->user.shost = ctx->user.host; |
62 | 17.9k | } |
63 | | |
64 | 18.1k | if (ctx->runas.shost != ctx->runas.host) |
65 | 0 | free(ctx->runas.shost); |
66 | 18.1k | free(ctx->runas.host); |
67 | 18.1k | ctx->runas.host = NULL; |
68 | 18.1k | ctx->runas.shost = NULL; |
69 | | |
70 | 18.1k | ctx->runas.host = strdup(remhost ? remhost : ctx->user.host); |
71 | 18.1k | if (ctx->runas.host == NULL) |
72 | 0 | goto oom; |
73 | 18.1k | if ((cp = strchr(ctx->runas.host, '.')) != NULL) { |
74 | 211 | ctx->runas.shost = strndup(ctx->runas.host, |
75 | 211 | (size_t)(cp - ctx->runas.host)); |
76 | 211 | if (ctx->runas.shost == NULL) |
77 | 0 | goto oom; |
78 | 17.9k | } else { |
79 | 17.9k | ctx->runas.shost = ctx->runas.host; |
80 | 17.9k | } |
81 | | |
82 | 18.1k | debug_return_bool(true); |
83 | 0 | oom: |
84 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
85 | | debug_return_bool(false); |
86 | 0 | } |