1*134e1779SJakub Wojciech Klama /* 2*134e1779SJakub Wojciech Klama * Copyright 2016 Chris Torek <chris.torek@gmail.com> 3*134e1779SJakub Wojciech Klama * All rights reserved 4*134e1779SJakub Wojciech Klama * 5*134e1779SJakub Wojciech Klama * Redistribution and use in source and binary forms, with or without 6*134e1779SJakub Wojciech Klama * modification, are permitted providing that the following conditions 7*134e1779SJakub Wojciech Klama * are met: 8*134e1779SJakub Wojciech Klama * 1. Redistributions of source code must retain the above copyright 9*134e1779SJakub Wojciech Klama * notice, this list of conditions and the following disclaimer. 10*134e1779SJakub Wojciech Klama * 2. Redistributions in binary form must reproduce the above copyright 11*134e1779SJakub Wojciech Klama * notice, this list of conditions and the following disclaimer in the 12*134e1779SJakub Wojciech Klama * documentation and/or other materials provided with the distribution. 13*134e1779SJakub Wojciech Klama * 14*134e1779SJakub Wojciech Klama * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15*134e1779SJakub Wojciech Klama * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16*134e1779SJakub Wojciech Klama * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17*134e1779SJakub Wojciech Klama * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 18*134e1779SJakub Wojciech Klama * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19*134e1779SJakub Wojciech Klama * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20*134e1779SJakub Wojciech Klama * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21*134e1779SJakub Wojciech Klama * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 22*134e1779SJakub Wojciech Klama * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 23*134e1779SJakub Wojciech Klama * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24*134e1779SJakub Wojciech Klama * POSSIBILITY OF SUCH DAMAGE. 25*134e1779SJakub Wojciech Klama * 26*134e1779SJakub Wojciech Klama */ 27*134e1779SJakub Wojciech Klama 28*134e1779SJakub Wojciech Klama #ifndef LIB9P_RFUNCS_H 29*134e1779SJakub Wojciech Klama #define LIB9P_RFUNCS_H 30*134e1779SJakub Wojciech Klama 31*134e1779SJakub Wojciech Klama #include <grp.h> 32*134e1779SJakub Wojciech Klama #include <pwd.h> 33*134e1779SJakub Wojciech Klama #include <string.h> 34*134e1779SJakub Wojciech Klama 35*134e1779SJakub Wojciech Klama #if defined(WITH_CASPER) 36*134e1779SJakub Wojciech Klama #include <libcasper.h> 37*134e1779SJakub Wojciech Klama #endif 38*134e1779SJakub Wojciech Klama 39*134e1779SJakub Wojciech Klama /* 40*134e1779SJakub Wojciech Klama * Reentrant, optionally-malloc-ing versions of 41*134e1779SJakub Wojciech Klama * basename() and dirname(). 42*134e1779SJakub Wojciech Klama */ 43*134e1779SJakub Wojciech Klama char *r_basename(const char *, char *, size_t); 44*134e1779SJakub Wojciech Klama char *r_dirname(const char *, char *, size_t); 45*134e1779SJakub Wojciech Klama 46*134e1779SJakub Wojciech Klama /* 47*134e1779SJakub Wojciech Klama * Yuck: getpwuid, getgrgid are not thread-safe, and the 48*134e1779SJakub Wojciech Klama * POSIX replacements (getpwuid_r, getgrgid_r) are horrible. 49*134e1779SJakub Wojciech Klama * This is to allow us to loop over the get.*_r calls with ever 50*134e1779SJakub Wojciech Klama * increasing buffers until they succeed or get unreasonable 51*134e1779SJakub Wojciech Klama * (same idea as the libc code for the non-reentrant versions, 52*134e1779SJakub Wojciech Klama * although prettier). 53*134e1779SJakub Wojciech Klama * 54*134e1779SJakub Wojciech Klama * The getpwuid/getgrgid functions auto-init one of these, 55*134e1779SJakub Wojciech Klama * but the caller must call r_pgfree() when done with the 56*134e1779SJakub Wojciech Klama * return values. 57*134e1779SJakub Wojciech Klama * 58*134e1779SJakub Wojciech Klama * If we need more later, we may have to expose the init function. 59*134e1779SJakub Wojciech Klama */ 60*134e1779SJakub Wojciech Klama struct r_pgdata { 61*134e1779SJakub Wojciech Klama char *r_pgbuf; 62*134e1779SJakub Wojciech Klama size_t r_pgbufsize; 63*134e1779SJakub Wojciech Klama union { 64*134e1779SJakub Wojciech Klama struct passwd un_pw; 65*134e1779SJakub Wojciech Klama struct group un_gr; 66*134e1779SJakub Wojciech Klama } r_pgun; 67*134e1779SJakub Wojciech Klama }; 68*134e1779SJakub Wojciech Klama 69*134e1779SJakub Wojciech Klama /* void r_pginit(struct r_pgdata *); */ 70*134e1779SJakub Wojciech Klama void r_pgfree(struct r_pgdata *); 71*134e1779SJakub Wojciech Klama struct passwd *r_getpwuid(uid_t, struct r_pgdata *); 72*134e1779SJakub Wojciech Klama struct group *r_getgrgid(gid_t, struct r_pgdata *); 73*134e1779SJakub Wojciech Klama 74*134e1779SJakub Wojciech Klama #if defined(WITH_CASPER) 75*134e1779SJakub Wojciech Klama struct passwd *r_cap_getpwuid(cap_channel_t *, uid_t, struct r_pgdata *); 76*134e1779SJakub Wojciech Klama struct group *r_cap_getgrgid(cap_channel_t *, gid_t, struct r_pgdata *); 77*134e1779SJakub Wojciech Klama #endif 78*134e1779SJakub Wojciech Klama 79*134e1779SJakub Wojciech Klama #endif /* LIB9P_RFUNCS_H */ 80