3 * Utilities for the client and the server
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
34 #include <sys/types.h>
40 #include <mLib/dstr.h>
41 #include <mLib/report.h>
45 /*----- Main code ---------------------------------------------------------*/
47 /* --- @u_quotify@ --- *
49 * Arguments: @dstr *d@ = where to write the answer
50 * @const char *p@ = string to quotify
54 * Use: Quotes the given string if necessary, according to our
58 void u_quotify(dstr *d, const char *p)
62 if (*p && !p[strcspn(p, "\"' \t\n\v")])
67 if (*p == '\\' || *p == '\"')
75 /* --- @u_getuser@ --- *
77 * Arguments: @const char *name@ = user name or id requested
78 * @gid_t *gg@ = where to store corresponding gid
80 * Returns: Corresponding uid.
82 * Use: Resolves a user name into a uid. Dies on failure; suitable
83 * for use in argument parsing.
86 uid_t u_getuser(const char *name, gid_t *gg)
90 unsigned long i = strtoul(name, &p, 0);
97 die(EXIT_FAILURE, "user `%s' not found", name);
103 /* --- @u_getgroup@ --- *
105 * Arguments: @const char *name@ = user name or id requested
107 * Returns: Corresponding gid.
109 * Use: Resolves a group name into a gid. Dies on failure; suitable
110 * for use in argument parsing.
113 gid_t u_getgroup(const char *name)
117 unsigned long i = strtoul(name, &p, 0);
124 die(EXIT_FAILURE, "group `%s' not found", name);
128 /* --- @u_setugid@ --- *
130 * Arguments: @uid_t u@ = user to set
131 * @gid_t g@ = group to set
135 * Use: Sets user and group to the given values; aborts on failure.
138 void u_setugid(uid_t u, gid_t g)
140 if (g != (gid_t)-1) {
141 if (setgid(g) || (getuid() == 0 && setgroups(1, &g))) {
142 die(EXIT_FAILURE, "couldn't setgid to %u: %s",
143 (unsigned)g, strerror(errno));
146 if (u != (uid_t)-1) {
148 die(EXIT_FAILURE, "couldn't setuid to %u: %s",
149 (unsigned)u, strerror(errno));
154 /*----- That's all, folks -------------------------------------------------*/