File: Credentials.cpp

package info (click to toggle)
ola 0.10.9.nojsmin-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 17,440 kB
  • sloc: cpp: 132,294; python: 14,445; javascript: 7,109; sh: 4,713; ansic: 2,189; java: 518; xml: 253; makefile: 175
file content (364 lines) | stat: -rw-r--r-- 6,913 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * Credentials.cpp
 * Handle getting and setting a process's credentials.
 * Copyright (C) 2012 Simon Newton
 */

/**
 * @addtogroup cred
 * @{
 * @file Credentials.cpp
 * @}
 */

#if HAVE_CONFIG_H
#include <config.h>
#endif  // HAVE_CONFIG_H

#include <errno.h>
#ifndef _WIN32
#include <grp.h>
#include <pwd.h>
#endif  // _WIN32
#include <string.h>
#include <unistd.h>

#include <ola/Logging.h>
#include <ola/base/Credentials.h>
#include <string>

namespace ola {

using std::string;

/**
 * @addtogroup cred
 * @{
 */

bool SupportsUIDs() {
#ifdef _WIN32
  return false;
#else
  return true;
#endif  // _WIN32
}

bool GetUID(uid_t* uid) {
#ifdef _WIN32
  (void) uid;
  return false;
#else
  if (uid) {
    *uid = getuid();
    return true;
  } else {
    return false;
  }
#endif  // _WIN32
}


bool GetEUID(uid_t* euid) {
#ifdef _WIN32
  (void) euid;
  return false;
#else
  if (euid) {
    *euid = geteuid();
    return true;
  } else {
    return false;
  }
#endif  // _WIN32
}


bool GetGID(gid_t* gid) {
#ifdef _WIN32
  (void) gid;
  return false;
#else
  if (gid) {
    *gid = getgid();
    return true;
  } else {
    return false;
  }
#endif  // _WIN32
}


bool GetEGID(gid_t* egid) {
#ifdef _WIN32
  (void) egid;
  return false;
#else
  if (egid) {
    *egid = getegid();
    return true;
  } else {
    return false;
  }
#endif  // _WIN32
}


bool SetUID(uid_t new_uid) {
#ifdef _WIN32
  (void) new_uid;
  return false;
#else
  if (setuid(new_uid)) {
    OLA_WARN << "setuid(" << new_uid << "): " << strerror(errno);
    return false;
  }
  return true;
#endif  // _WIN32
}


bool SetGID(gid_t new_gid) {
#ifdef _WIN32
  (void) new_gid;
  return false;
#else
  if (setgid(new_gid)) {
    OLA_WARN << "setgid(" << new_gid << "): " << strerror(errno);
    return false;
  }
  return true;
#endif  // _WIN32
}

int GetGroups(int size, gid_t list[]) {
#ifdef _WIN32
  (void) size;
  (void) list;
  return -1;
#else
  return getgroups(size, list);
#endif  // _WIN32
}

bool SetGroups(size_t size, const gid_t *list) {
#ifdef _WIN32
  (void) size;
  (void) list;
  return false;
#else
  if (setgroups(size, list)) {
    OLA_WARN << "setgroups(): " << strerror(errno);
    return false;
  }
  return true;
#endif  // _WIN32
}

/**
 * @}
 */

#ifndef _WIN32

/** @private */
template <typename F, typename arg>
bool GenericGetPasswdReentrant(F f, arg a, PasswdEntry *passwd) {
  if (!passwd)
    return false;

  struct passwd pwd, *pwd_ptr;
  unsigned int size = 1024;
  bool ok = false;
  char *buffer;

  while (!ok) {
    buffer = new char[size];
    int ret = f(a, &pwd, buffer, size, &pwd_ptr);
    switch (ret) {
      case 0:
        ok = true;
        break;
      case ERANGE:
        delete[] buffer;
        size += 1024;
        break;
      default:
        delete[] buffer;
        return false;
    }
  }

  if (!pwd_ptr)
    return false;

  passwd->pw_name = pwd_ptr->pw_name;
  passwd->pw_uid = pwd_ptr->pw_uid;
  passwd->pw_gid = pwd_ptr->pw_gid;
  passwd->pw_dir = pwd_ptr->pw_dir;
  passwd->pw_shell = pwd_ptr->pw_shell;
  delete[] buffer;
  return true;
}

/*
 * Some platforms (Android) don't have the _r versions. So we fall back to the
 * non-thread safe versions.
 */
/** @private */
template <typename F, typename arg>
bool GenericGetPasswd(F f, arg a, PasswdEntry *passwd) {
  if (!passwd)
    return false;

  struct passwd *pwd = f(a);
  if (!pwd)
    return false;

  passwd->pw_name = pwd->pw_name;
  passwd->pw_uid = pwd->pw_uid;
  passwd->pw_gid = pwd->pw_gid;
  passwd->pw_dir = pwd->pw_dir;
  passwd->pw_shell = pwd->pw_shell;
  return true;
}

#endif  // !_WIN32


bool GetPasswdName(const string &name, PasswdEntry *passwd) {
#ifdef _WIN32
  (void) name;
  (void) passwd;
  return false;
#else
#ifdef HAVE_GETPWNAM_R
  return GenericGetPasswdReentrant(getpwnam_r, name.c_str(), passwd);
#else
  return GenericGetPasswd(getpwnam, name.c_str(), passwd);
#endif  // HAVE_GETPWNAM_R
#endif  // _WIN32
}


bool GetPasswdUID(uid_t uid, PasswdEntry *passwd) {
#ifdef _WIN32
  (void) uid;
  (void) passwd;
  return false;
#else
#ifdef HAVE_GETPWUID_R
  return GenericGetPasswdReentrant(getpwuid_r, uid, passwd);
#else
  return GenericGetPasswd(getpwuid, uid, passwd);
#endif  // HAVE_GETPWUID_R
#endif  // _WIN32
}

#ifndef _WIN32

/** @private */
template <typename F, typename arg>
bool GenericGetGroupReentrant(F f, arg a, GroupEntry *group_entry) {
  if (!group_entry)
    return false;

  struct group grp, *grp_ptr;
  unsigned int size = 1024;
  bool ok = false;
  char *buffer;

  while (!ok) {
    buffer = new char[size];
    int ret = f(a, &grp, buffer, size, &grp_ptr);
    switch (ret) {
      case 0:
        ok = true;
        break;
      case ERANGE:
        delete[] buffer;
        size += 1024;
        break;
      default:
        delete[] buffer;
        return false;
    }
  }

  if (!grp_ptr) {
    // not found
    return false;
  }

  group_entry->gr_name = grp_ptr->gr_name;
  group_entry->gr_gid = grp_ptr->gr_gid;
  delete[] buffer;
  return true;
}


/*
 * Some platforms (Android) don't have the _r versions. So we fall back to the
 * non-thread safe versions.
 */
/** @private */
template <typename F, typename arg>
bool GenericGetGroup(F f, arg a, GroupEntry *group_entry) {
  if (!group_entry)
    return false;

  struct group *grp_ptr = f(a);
  if (!grp_ptr)
    return false;

  group_entry->gr_name = grp_ptr->gr_name;
  group_entry->gr_gid = grp_ptr->gr_gid;
  return true;
}

#endif  // !_WIN32


bool GetGroupName(const string &name, GroupEntry *group_entry) {
#ifdef _WIN32
  (void) name;
  (void) group_entry;
  return false;
#else
#ifdef HAVE_GETGRNAM_R
  return GenericGetGroupReentrant(getgrnam_r, name.c_str(), group_entry);
#else
  return GenericGetGroup(getgrnam, name.c_str(), group_entry);
#endif  // HAVE_GETGRNAM_R
#endif  // _WIN32
}


bool GetGroupGID(gid_t gid, GroupEntry *group_entry) {
#ifdef _WIN32
  (void) gid;
  (void) group_entry;
  return false;
#else
#ifdef HAVE_GETGRGID_R
  return GenericGetGroupReentrant(getgrgid_r, gid, group_entry);
#else
  return GenericGetGroup(getgrgid, gid, group_entry);
#endif  // HAVE_GETGRGID_R
#endif  // _WIN32
}
}  // namespace ola