LIRC libraries
LinuxInfraredRemoteControl
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
util.c
Go to the documentation of this file.
1 /****************************************************************************
2  * util.c ***************************************************************
3  ****************************************************************************
4  */
5 
12 #include <pwd.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/types.h>
16 #include <unistd.h>
17 
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 
22 #include "lirc/lirc_log.h"
23 #include "util.h"
24 
25 
26 const char* drop_sudo_root(int (*set_some_uid)(uid_t))
27 {
28  struct passwd* pw;
29  char* user;
30  int r;
31 
32  if (getuid() != 0)
33  return "";
34  user = getenv("SUDO_USER");
35  if (user == NULL)
36  return "root";
37  pw = getpwnam(user);
38  if (pw == NULL) {
39  logperror(LIRC_ERROR, "Can't run getpwnam() for %s", user);
40  return "";
41  }
42  r = set_some_uid(pw->pw_uid);
43  if (r == -1) {
44  logperror(LOG_WARNING, "Cannot change UID to %d", pw->pw_uid);
45  return "";
46  }
47  setenv("HOME", pw->pw_dir, 1);
48  logprintf(LOG_NOTICE, "Running as user %s", pw->pw_name);
49  return pw->pw_name;
50 }
51 
52 
53 void drop_root_cli(int (*set_some_uid)(uid_t))
54 {
55  const char* new_user;
56 
57  new_user = drop_sudo_root(set_some_uid);
58  if (strcmp("root", new_user) == 0)
59  puts("Warning: Running as root.");
60  else if (strlen(new_user) == 0)
61  puts("Warning: Cannot change uid.");
62  else
63  printf("Running as regular user %s\n", new_user);
64 }
Utilities.
void drop_root_cli(int(*set_some_uid)(uid_t))
Definition: util.c:53
const char * drop_sudo_root(int(*set_some_uid)(uid_t))
Definition: util.c:26
void logperror(loglevel_t prio, const char *fmt,...)
Definition: lirc_log.c:288