LIRC libraries
LinuxInfraredRemoteControl
lirc-utils.c
Go to the documentation of this file.
1 /****************************************************************************
2  * lirc-utils.c *************************************************************
3  ****************************************************************************
4  */
5 
12 #include <grp.h>
13 #include <pwd.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/types.h>
17 #include <unistd.h>
18 
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 
23 #include "lirc/lirc_log.h"
24 #include "lirc-utils.h"
25 
26 static const logchannel_t logchannel = LOG_LIB;
27 
28 const char* drop_sudo_root(int (*set_some_uid)(uid_t))
29 {
30  struct passwd* pw;
31  char* user;
32  GETGROUPS_T groups[32];
33  int group_cnt = sizeof(groups)/sizeof(gid_t);
34  char groupnames[256] = {0};
35  char buff[12];
36  int r;
37  int i;
38 
39  if (getuid() != 0)
40  return "";
41  user = getenv("SUDO_USER");
42  if (user == NULL)
43  return "root";
44  pw = getpwnam(user);
45  if (pw == NULL) {
46  log_perror_err("Can't run getpwnam() for %s", user);
47  return "";
48  }
49  r = getgrouplist(user, pw->pw_gid, groups, &group_cnt);
50  if (r == -1) {
51  log_perror_warn("Cannot get supplementary groups");
52  return "";
53  }
54  r = setgroups(group_cnt, groups);
55  if (r == -1) {
56  log_perror_warn("Cannot set supplementary groups");
57  return "";
58  }
59  r = setgid(pw->pw_gid);
60  if (r == -1) {
61  log_perror_warn("Cannot set GID");
62  return "";
63  }
64  r = set_some_uid(pw->pw_uid);
65  if (r == -1) {
66  log_perror_warn("Cannot change UID to %d", pw->pw_uid);
67  return "";
68  }
69  setenv("HOME", pw->pw_dir, 1);
70  log_notice("Running as user %s", user);
71  for (i = 0; i < group_cnt; i += 1) {
72  snprintf(buff, 5, " %d", groups[i]);
73  strcat(groupnames, buff);
74  }
75  log_debug("Groups: [%d]:%s", pw->pw_gid, groupnames);
76 
77  return pw->pw_name;
78 }
79 
80 
81 void drop_root_cli(int (*set_some_uid)(uid_t))
82 {
83  const char* new_user;
84 
85  new_user = drop_sudo_root(set_some_uid);
86  if (strcmp("root", new_user) == 0)
87  puts("Warning: Running as root.");
88  else if (strlen(new_user) == 0)
89  puts("Warning: Cannot change uid.");
90  else
91  printf("Running as regular user %s\n", new_user);
92 }
#define log_debug(fmt,...)
Log a debug message.
Definition: lirc_log.h:124
logchannel_t
Log channels used to filter messages.
Definition: lirc_log.h:53
#define log_perror_err(fmt,...)
perror wrapper logging with level LIRC_ERROR.
Definition: lirc_log.h:89
Utilities.
void drop_root_cli(int(*set_some_uid)(uid_t))
Default view part of drop_sudo_root.
Definition: lirc-utils.c:81
const char * drop_sudo_root(int(*set_some_uid)(uid_t))
Try to drop possible root privileges, returning new user or "" on error.
Definition: lirc-utils.c:28
#define log_perror_warn(fmt,...)
perror wrapper logging with level LIRC_WARNING.
Definition: lirc_log.h:94
#define log_notice(fmt,...)
Log a notice message.
Definition: lirc_log.h:119