/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  | 19.9k  | { | 
37  | 19.9k  |     char *cp;  | 
38  | 19.9k  |     debug_decl(sudoers_sethost, SUDOERS_DEBUG_UTIL);  | 
39  |  |  | 
40  | 19.9k  |     if (ctx->user.shost != ctx->user.host)  | 
41  | 0  |   free(ctx->user.shost);  | 
42  | 19.9k  |     free(ctx->user.host);  | 
43  | 19.9k  |     ctx->user.host = NULL;  | 
44  | 19.9k  |     ctx->user.shost = NULL;  | 
45  |  |  | 
46  | 19.9k  |     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  | 19.9k  |     } else { | 
51  | 19.9k  |   ctx->user.host = strdup(host);  | 
52  | 19.9k  |     }  | 
53  | 19.9k  |     if (ctx->user.host == NULL)  | 
54  | 0  |       goto oom;  | 
55  | 19.9k  |     if ((cp = strchr(ctx->user.host, '.')) != NULL) { | 
56  | 184  |   ctx->user.shost = strndup(ctx->user.host,  | 
57  | 184  |       (size_t)(cp - ctx->user.host));  | 
58  | 184  |   if (ctx->user.shost == NULL)  | 
59  | 0  |       goto oom;  | 
60  | 19.7k  |     } else { | 
61  | 19.7k  |   ctx->user.shost = ctx->user.host;  | 
62  | 19.7k  |     }  | 
63  |  |  | 
64  | 19.9k  |     if (ctx->runas.shost != ctx->runas.host)  | 
65  | 0  |   free(ctx->runas.shost);  | 
66  | 19.9k  |     free(ctx->runas.host);  | 
67  | 19.9k  |     ctx->runas.host = NULL;  | 
68  | 19.9k  |     ctx->runas.shost = NULL;  | 
69  |  |  | 
70  | 19.9k  |     ctx->runas.host = strdup(remhost ? remhost : ctx->user.host);  | 
71  | 19.9k  |     if (ctx->runas.host == NULL)  | 
72  | 0  |   goto oom;  | 
73  | 19.9k  |     if ((cp = strchr(ctx->runas.host, '.')) != NULL) { | 
74  | 184  |   ctx->runas.shost = strndup(ctx->runas.host,  | 
75  | 184  |       (size_t)(cp - ctx->runas.host));  | 
76  | 184  |   if (ctx->runas.shost == NULL)  | 
77  | 0  |       goto oom;  | 
78  | 19.7k  |     } else { | 
79  | 19.7k  |   ctx->runas.shost = ctx->runas.host;  | 
80  | 19.7k  |     }  | 
81  |  |  | 
82  | 19.9k  |     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  | }  |