Commit | Line | Data |
---|---|---|
410c8acf | 1 | /* -*-c-*- |
410c8acf | 2 | * |
3 | * Utilities for the client and the server | |
4 | * | |
5 | * (c) 2001 Straylight/Edgeware | |
6 | */ | |
7 | ||
e04c2d50 | 8 | /*----- Licensing notice --------------------------------------------------* |
410c8acf | 9 | * |
10 | * This file is part of Trivial IP Encryption (TrIPE). | |
11 | * | |
11ad66c2 MW |
12 | * TrIPE is free software: you can redistribute it and/or modify it under |
13 | * the terms of the GNU General Public License as published by the Free | |
14 | * Software Foundation; either version 3 of the License, or (at your | |
15 | * option) any later version. | |
e04c2d50 | 16 | * |
11ad66c2 MW |
17 | * TrIPE is distributed in the hope that it will be useful, but WITHOUT |
18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
20 | * for more details. | |
e04c2d50 | 21 | * |
410c8acf | 22 | * You should have received a copy of the GNU General Public License |
11ad66c2 | 23 | * along with TrIPE. If not, see <https://www.gnu.org/licenses/>. |
410c8acf | 24 | */ |
25 | ||
410c8acf | 26 | /*----- Header files ------------------------------------------------------*/ |
27 | ||
28 | #include <errno.h> | |
29 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
31 | #include <string.h> | |
32 | ||
52b86648 MW |
33 | #include <sys/types.h> |
34 | #include <unistd.h> | |
35 | ||
36 | #include <pwd.h> | |
37 | #include <grp.h> | |
38 | ||
0ed0735f | 39 | #include <mLib/dstr.h> |
52b86648 | 40 | #include <mLib/report.h> |
0ed0735f | 41 | |
410c8acf | 42 | #include "util.h" |
43 | ||
410c8acf | 44 | /*----- Main code ---------------------------------------------------------*/ |
45 | ||
0ed0735f MW |
46 | /* --- @u_quotify@ --- * |
47 | * | |
48 | * Arguments: @dstr *d@ = where to write the answer | |
49 | * @const char *p@ = string to quotify | |
50 | * | |
51 | * Returns: --- | |
52 | * | |
53 | * Use: Quotes the given string if necessary, according to our | |
54 | * quoting rules. | |
55 | */ | |
56 | ||
57 | void u_quotify(dstr *d, const char *p) | |
58 | { | |
59 | if (d->len) | |
60 | dstr_putc(d, ' '); | |
61 | if (*p && !p[strcspn(p, "\"' \t\n\v")]) | |
62 | dstr_puts(d, p); | |
63 | else { | |
64 | dstr_putc(d, '\"'); | |
65 | while (*p) { | |
66 | if (*p == '\\' || *p == '\"') | |
67 | dstr_putc(d, '\\'); | |
68 | dstr_putc(d, *p++); | |
69 | } | |
70 | dstr_putc(d, '\"'); | |
71 | } | |
918a33b7 | 72 | dstr_putz(d); |
0ed0735f MW |
73 | } |
74 | ||
52b86648 MW |
75 | /* --- @u_getuser@ --- * |
76 | * | |
77 | * Arguments: @const char *name@ = user name or id requested | |
78 | * @gid_t *gg@ = where to store corresponding gid | |
79 | * | |
80 | * Returns: Corresponding uid. | |
81 | * | |
82 | * Use: Resolves a user name into a uid. Dies on failure; suitable | |
83 | * for use in argument parsing. | |
84 | */ | |
85 | ||
86 | uid_t u_getuser(const char *name, gid_t *gg) | |
87 | { | |
88 | struct passwd *pw; | |
89 | char *p; | |
90 | unsigned long i = strtoul(name, &p, 0); | |
91 | ||
92 | if (!*p) | |
93 | pw = getpwuid(i); | |
94 | else | |
95 | pw = getpwnam(name); | |
96 | if (!pw) | |
97 | die(EXIT_FAILURE, "user `%s' not found", name); | |
98 | if (gg && *gg == -1) | |
99 | *gg = pw->pw_gid; | |
100 | return (pw->pw_uid); | |
101 | } | |
102 | ||
103 | /* --- @u_getgroup@ --- * | |
104 | * | |
105 | * Arguments: @const char *name@ = user name or id requested | |
106 | * | |
107 | * Returns: Corresponding gid. | |
108 | * | |
109 | * Use: Resolves a group name into a gid. Dies on failure; suitable | |
110 | * for use in argument parsing. | |
111 | */ | |
112 | ||
113 | gid_t u_getgroup(const char *name) | |
114 | { | |
115 | struct group *gr; | |
116 | char *p; | |
117 | unsigned long i = strtoul(name, &p, 0); | |
118 | ||
119 | if (!*p) | |
120 | gr = getgrgid(i); | |
121 | else | |
122 | gr = getgrnam(name); | |
123 | if (!gr) | |
124 | die(EXIT_FAILURE, "group `%s' not found", name); | |
125 | return (gr->gr_gid); | |
126 | } | |
127 | ||
128 | /* --- @u_setugid@ --- * | |
129 | * | |
130 | * Arguments: @uid_t u@ = user to set | |
131 | * @gid_t g@ = group to set | |
132 | * | |
133 | * Returns: --- | |
134 | * | |
135 | * Use: Sets user and group to the given values; aborts on failure. | |
136 | */ | |
137 | ||
138 | void u_setugid(uid_t u, gid_t g) | |
139 | { | |
ec9b8aed MW |
140 | uid_t cu = geteuid(); |
141 | ||
142 | if (cu == 0 && g != (gid_t)-1) { | |
52b86648 MW |
143 | if (setgid(g) || (getuid() == 0 && setgroups(1, &g))) { |
144 | die(EXIT_FAILURE, "couldn't setgid to %u: %s", | |
145 | (unsigned)g, strerror(errno)); | |
146 | } | |
147 | } | |
148 | if (u != (uid_t)-1) { | |
149 | if (setuid(u)) { | |
150 | die(EXIT_FAILURE, "couldn't setuid to %u: %s", | |
151 | (unsigned)u, strerror(errno)); | |
152 | } | |
153 | } | |
154 | } | |
155 | ||
410c8acf | 156 | /*----- That's all, folks -------------------------------------------------*/ |