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