1 /*****************************************************************************
2 * Written by Chris Dunlap <cdunlap@llnl.gov>.
3 * Copyright (C) 2007-2020 Lawrence Livermore National Security, LLC.
4 * Copyright (C) 2002-2007 The Regents of the University of California.
5 * UCRL-CODE-155910.
6 *
7 * This file is part of the MUNGE Uid 'N' Gid Emporium (MUNGE).
8 * For details, see <https://dun.github.io/munge/>.
9 *
10 * MUNGE is free software: you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License, or (at your option)
13 * any later version. Additionally for the MUNGE library (libmunge), you
14 * can redistribute it and/or modify it under the terms of the GNU Lesser
15 * General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * MUNGE is distributed in the hope that it will be useful, but WITHOUT
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * and GNU Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * and GNU Lesser General Public License along with MUNGE. If not, see
25 * <http://www.gnu.org/licenses/>.
26 *****************************************************************************/
27
28
29 #if HAVE_CONFIG_H
30 # include "config.h"
31 #endif /* HAVE_CONFIG_H */
32
33 #include <errno.h>
34 #include <grp.h>
35 #include <limits.h>
36 #include <pwd.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <sys/types.h>
40 #include "common.h"
41 #include "query.h"
42 #include "xgetgr.h"
43 #include "xgetpw.h"
44
45
46 int
query_uid(const char * user,uid_t * uid_ptr)47 query_uid (const char *user, uid_t *uid_ptr)
48 {
49 xpwbuf_p pwbufp;
50 struct passwd pw;
51 uid_t uid;
52 long int l;
53 char *end_ptr;
54 int rv;
55
56 if (user == NULL) {
57 errno = EINVAL;
58 return (-1);
59 }
60 pwbufp = xgetpwbuf_create (0);
61 if (pwbufp == NULL) {
62 return (-1);
63 }
64 if (xgetpwnam (user, &pw, pwbufp) == 0) {
65 uid = pw.pw_uid;
66 rv = 0;
67 }
68 else {
69 errno = 0;
70 l = strtol (user, &end_ptr, 10);
71 if ((errno == ERANGE) && ((l == LONG_MIN) || (l == LONG_MAX))) {
72 rv = -1;
73 }
74 else if ((user == end_ptr) || (*end_ptr != '\0')) {
75 rv = -1;
76 }
77 else if ((l < 0) || ((unsigned int) l > UID_MAXIMUM)) {
78 rv = -1;
79 }
80 else {
81 uid = (uid_t) l;
82 rv = 0;
83 }
84 }
85 if ((uid_ptr != NULL) && (rv == 0)) {
86 *uid_ptr = uid;
87 }
88 xgetpwbuf_destroy (pwbufp);
89 return (rv);
90 }
91
92
93 int
query_gid(const char * group,gid_t * gid_ptr)94 query_gid (const char *group, gid_t *gid_ptr)
95 {
96 xgrbuf_p grbufp;
97 struct group gr;
98 gid_t gid;
99 long int l;
100 char *end_ptr;
101 int rv;
102
103 if (group == NULL) {
104 errno = EINVAL;
105 return (-1);
106 }
107 grbufp = xgetgrbuf_create (0);
108 if (grbufp == NULL) {
109 return (-1);
110 }
111 if (xgetgrnam (group, &gr, grbufp) == 0) {
112 gid = gr.gr_gid;
113 rv = 0;
114 }
115 else {
116 errno = 0;
117 l = strtol (group, &end_ptr, 10);
118 if ((errno == ERANGE) && ((l == LONG_MIN) || (l == LONG_MAX))) {
119 rv = -1;
120 }
121 else if ((group == end_ptr) || (*end_ptr != '\0')) {
122 rv = -1;
123 }
124 else if ((l < 0) || ((unsigned int) l > GID_MAXIMUM)) {
125 rv = -1;
126 }
127 else {
128 gid = (gid_t) l;
129 rv = 0;
130 }
131 }
132 if ((gid_ptr != NULL) && (rv == 0)) {
133 *gid_ptr = gid;
134 }
135 xgetgrbuf_destroy (grbufp);
136 return (rv);
137 }
138