Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
tg
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
tg
Commits
e23f4e33
Commit
e23f4e33
authored
Sep 09, 2014
by
Vysheng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added -U and -G keys to make setuid and setgid at start if started as root
parent
95e9c69b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
1 deletion
+55
-1
main.c
main.c
+55
-1
No files found.
main.c
View file @
e23f4e33
...
@@ -47,6 +47,8 @@
...
@@ -47,6 +47,8 @@
#include <libconfig.h>
#include <libconfig.h>
#endif
#endif
#include <grp.h>
#include "telegram.h"
#include "telegram.h"
#include "loop.h"
#include "loop.h"
#include "interface.h"
#include "interface.h"
...
@@ -405,6 +407,8 @@ void usage (void) {
...
@@ -405,6 +407,8 @@ void usage (void) {
printf
(
" -R disable readline
\n
"
);
printf
(
" -R disable readline
\n
"
);
printf
(
" -d daemon mode
\n
"
);
printf
(
" -d daemon mode
\n
"
);
printf
(
" -L <log-name> log file name
\n
"
);
printf
(
" -L <log-name> log file name
\n
"
);
printf
(
" -U <user-name> change uid after start
\n
"
);
printf
(
" -G <group-name> change gid after start
\n
"
);
exit
(
1
);
exit
(
1
);
}
}
...
@@ -459,10 +463,53 @@ static void sighup_handler (const int sig) {
...
@@ -459,10 +463,53 @@ static void sighup_handler (const int sig) {
signal
(
SIGHUP
,
sighup_handler
);
signal
(
SIGHUP
,
sighup_handler
);
}
}
char
*
set_user_name
;
char
*
set_group_name
;
int
change_user_group
()
{
char
*
username
=
set_user_name
;
char
*
groupname
=
set_group_name
;
struct
passwd
*
pw
;
/* lose root privileges if we have them */
if
(
getuid
()
==
0
||
geteuid
()
==
0
)
{
if
(
username
==
0
||
*
username
==
'\0'
)
{
username
=
"telegramd"
;
}
if
((
pw
=
getpwnam
(
username
))
==
0
)
{
fprintf
(
stderr
,
"change_user_group: can't find the user %s to switch to
\n
"
,
username
);
return
-
1
;
}
gid_t
gid
=
pw
->
pw_gid
;
if
(
setgroups
(
1
,
&
gid
)
<
0
)
{
fprintf
(
stderr
,
"change_user_group: failed to clear supplementary groups list: %m
\n
"
);
return
-
1
;
}
if
(
groupname
)
{
struct
group
*
g
=
getgrnam
(
groupname
);
if
(
g
==
NULL
)
{
fprintf
(
stderr
,
"change_user_group: can't find the group %s to switch to
\n
"
,
groupname
);
return
-
1
;
}
gid
=
g
->
gr_gid
;
}
if
(
setgid
(
gid
)
<
0
)
{
fprintf
(
stderr
,
"change_user_group: setgid (%d) failed. %m
\n
"
,
(
int
)
gid
);
return
-
1
;
}
if
(
setuid
(
pw
->
pw_uid
)
<
0
)
{
fprintf
(
stderr
,
"change_user_group: failed to assume identity of user %s
\n
"
,
username
);
return
-
1
;
}
}
return
0
;
}
void
args_parse
(
int
argc
,
char
**
argv
)
{
void
args_parse
(
int
argc
,
char
**
argv
)
{
int
opt
=
0
;
int
opt
=
0
;
while
((
opt
=
getopt
(
argc
,
argv
,
"u:hk:vNl:fEwWCRdL:"
while
((
opt
=
getopt
(
argc
,
argv
,
"u:hk:vNl:fEwWCRdL:
U:G:
"
#ifdef HAVE_LIBCONFIG
#ifdef HAVE_LIBCONFIG
"c:p:"
"c:p:"
#else
#else
...
@@ -533,6 +580,12 @@ void args_parse (int argc, char **argv) {
...
@@ -533,6 +580,12 @@ void args_parse (int argc, char **argv) {
case
'L'
:
case
'L'
:
logname
=
optarg
;
logname
=
optarg
;
break
;
break
;
case
'U'
:
set_user_name
=
optarg
;
break
;
case
'G'
:
set_group_name
=
optarg
;
break
;
case
'h'
:
case
'h'
:
default:
default:
usage
();
usage
();
...
@@ -574,6 +627,7 @@ void sig_abrt_handler (int signum __attribute__ ((unused))) {
...
@@ -574,6 +627,7 @@ void sig_abrt_handler (int signum __attribute__ ((unused))) {
}
}
int
main
(
int
argc
,
char
**
argv
)
{
int
main
(
int
argc
,
char
**
argv
)
{
change_user_group
();
signal
(
SIGSEGV
,
sig_segv_handler
);
signal
(
SIGSEGV
,
sig_segv_handler
);
signal
(
SIGABRT
,
sig_abrt_handler
);
signal
(
SIGABRT
,
sig_abrt_handler
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment