Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/plugins/sudoers/exptilde.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2020 Todd C. Miller <[email protected]>
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 <stdio.h>
22
#include <stdlib.h>
23
#include <string.h>
24
#include <grp.h>
25
#include <pwd.h>
26
27
#include <sudoers.h>
28
#include <pwutil.h>
29
30
/*
31
* Expand leading tilde in *path, which must be dynamically allocated.
32
* Replaces path with the expanded version as needed, freeing the old one.
33
* Returns true on success, false on failure.
34
*/
35
bool
36
expand_tilde(char **path, const char *user)
37
{
38
char *npath, *opath = *path;
39
char *slash = NULL;
40
struct passwd *pw;
41
int len;
42
debug_decl(expand_tilde, SUDOERS_DEBUG_UTIL);
43
44
switch (*opath++) {
45
case '/':
46
/* A fully-qualified path, nothing to do. */
47
debug_return_bool(true);
48
case '~':
49
/* See below. */
50
break;
51
default:
52
/* Not a fully-qualified path or one that starts with a tilde. */
53
debug_return_bool(false);
54
}
55
56
switch (*opath) {
57
case '\0':
58
/* format: ~ */
59
break;
60
case '/':
61
/* format: ~/foo */
62
opath++;
63
break;
64
default:
65
/* format: ~user/foo */
66
user = opath;
67
slash = strchr(opath, '/');
68
if (slash != NULL) {
69
*slash = '\0';
70
opath = slash + 1;
71
} else {
72
opath = (char *)"";
73
}
74
}
75
pw = sudo_getpwnam(user);
76
if (slash != NULL)
77
*slash = '/';
78
if (pw == NULL) {
79
/* Unknown user. */
80
sudo_warnx(U_("unknown user %s"), user);
81
debug_return_bool(false);
82
}
83
84
len = asprintf(&npath, "%s%s%s", pw->pw_dir, *opath ? "/" : "", opath);
85
sudo_pw_delref(pw);
86
if (len == -1) {
87
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
88
debug_return_bool(false);
89
}
90
91
free(*path);
92
*path = npath;
93
debug_return_bool(true);
94
}
95
96