1 /*
2  * Copyright (c) 2002-2012 Balabit
3  * Copyright (c) 1998-2012 Balázs Scheidler
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * As an additional exemption you are allowed to compile & link against the
20  * OpenSSL libraries as published by the OpenSSL project. See the file
21  * COPYING for details.
22  *
23  */
24 #include "userdb.h"
25 
26 #include <sys/types.h>
27 #include <pwd.h>
28 #include <grp.h>
29 #include <string.h>
30 #include <stdlib.h>
31 
32 gboolean
resolve_user(const char * user,gint * uid)33 resolve_user(const char *user, gint *uid)
34 {
35   struct passwd *pw;
36   gchar *endptr;
37 
38   *uid = 0;
39   if (!(*user))
40     return FALSE;
41 
42   *uid = strtol(user, &endptr, 0);
43   if (*endptr)
44     {
45       pw = getpwnam(user);
46       if (!pw)
47         return FALSE;
48 
49       *uid = pw->pw_uid;
50     }
51   return TRUE;
52 }
53 
54 gboolean
resolve_group(const char * group,gint * gid)55 resolve_group(const char *group, gint *gid)
56 {
57   struct group *gr;
58   gchar *endptr;
59 
60   *gid = 0;
61   if (!(*group))
62     return FALSE;
63 
64   *gid = strtol(group, &endptr, 0);
65   if (*endptr)
66     {
67       gr = getgrnam(group);
68       if (!gr)
69         return FALSE;
70 
71       *gid = gr->gr_gid;
72     }
73   return TRUE;
74 }
75 
76 gboolean
resolve_user_group(char * arg,gint * uid,gint * gid)77 resolve_user_group(char *arg, gint *uid, gint *gid)
78 {
79   char *user, *group;
80 
81   *uid = 0;
82   user = strtok(arg, ":.");
83   group = strtok(NULL, "");
84 
85   if (user && !resolve_user(user, uid))
86     return FALSE;
87   if (group && !resolve_group(group, gid))
88     return FALSE;
89   return TRUE;
90 }
91