@@ -569,42 +569,60 @@ static int Main()
569
569
} else if (command && command->GetImpersonationLevel () == ImpersonateIcinga) {
570
570
String group = Configuration::RunAsGroup;
571
571
String user = Configuration::RunAsUser;
572
+ gid_t gid = 0 ;
572
573
573
574
errno = 0 ;
574
- struct group *gr = getgrnam (group.CStr ());
575
-
576
- if (!gr) {
577
- if (errno == 0 ) {
578
- Log (LogCritical, " cli" )
579
- << " Invalid group specified: " << group;
580
- return EXIT_FAILURE;
581
- } else {
582
- Log (LogCritical, " cli" )
583
- << " getgrnam() failed with error code " << errno << " , \" " << Utility::FormatErrorNumber (errno) << " \" " ;
575
+ try {
576
+ gid = boost::lexical_cast<gid_t >(group);
577
+ } catch (const boost::bad_lexical_cast&) {
578
+ struct group * gr = getgrnam (group.CStr ());
579
+ if (!gr) {
580
+ if (errno == 0 ) {
581
+ Log (LogCritical, " cli" )
582
+ << " Invalid group specified: " << group;
583
+ } else {
584
+ Log (LogCritical, " cli" )
585
+ << " getgrnam() failed with error code " << errno << " , \" " << Utility::FormatErrorNumber (errno) << " \" " ;
586
+ }
584
587
return EXIT_FAILURE;
585
588
}
589
+
590
+ gid = gr->gr_gid ;
586
591
}
587
592
588
- if (getgid () != gr-> gr_gid ) {
593
+ if (getgid () != gid ) {
589
594
if (!vm.count (" reload-internal" ) && setgroups (0 , nullptr ) < 0 ) {
590
595
Log (LogCritical, " cli" )
591
596
<< " setgroups() failed with error code " << errno << " , \" " << Utility::FormatErrorNumber (errno) << " \" " ;
592
597
Log (LogCritical, " cli" )
593
- << " Please re-run this command as a privileged user or using the \" " << user << " \" account." ;
598
+ << " Please rerun this command as a privileged user or using the \" " << user << " \" account." ;
594
599
return EXIT_FAILURE;
595
600
}
596
601
597
- if (setgid (gr-> gr_gid ) < 0 ) {
602
+ if (setgid (gid ) < 0 ) {
598
603
Log (LogCritical, " cli" )
599
604
<< " setgid() failed with error code " << errno << " , \" " << Utility::FormatErrorNumber (errno) << " \" " ;
605
+ Log (LogCritical, " cli" )
606
+ << " Please rerun this command as a privileged user or using the \" " << user << " \" account." ;
600
607
return EXIT_FAILURE;
601
608
}
602
609
}
603
610
611
+ std::optional<uid_t > uid;
612
+ struct passwd *pw = nullptr ;
613
+
604
614
errno = 0 ;
605
- struct passwd *pw = getpwnam (user.CStr ());
615
+ try {
616
+ uid = boost::lexical_cast<uid_t >(user);
617
+ pw = getpwuid (*uid);
618
+ } catch (const boost::bad_lexical_cast&) {
619
+ pw = getpwnam (user.CStr ());
620
+ if (pw) {
621
+ uid = pw->pw_uid ;
622
+ }
623
+ }
606
624
607
- if (!pw ) {
625
+ if (!uid ) {
608
626
if (errno == 0 ) {
609
627
Log (LogCritical, " cli" )
610
628
<< " Invalid user specified: " << user;
@@ -617,20 +635,22 @@ static int Main()
617
635
}
618
636
619
637
// also activate the additional groups the configured user is member of
620
- if (getuid () != pw->pw_uid ) {
621
- if (!vm.count (" reload-internal" ) && initgroups (user.CStr (), pw->pw_gid ) < 0 ) {
638
+ if (getuid () != *uid) {
639
+ // initgroups() is only called when either getpwuid() or getpwnam() returned a valid user entry.
640
+ // Otherwise it makes no sense to set any additional groups.
641
+ if (!vm.count (" reload-internal" ) && pw && initgroups (user.CStr (), pw->pw_gid ) < 0 ) {
622
642
Log (LogCritical, " cli" )
623
643
<< " initgroups() failed with error code " << errno << " , \" " << Utility::FormatErrorNumber (errno) << " \" " ;
624
644
Log (LogCritical, " cli" )
625
- << " Please re-run this command as a privileged user or using the \" " << user << " \" account." ;
645
+ << " Please rerun this command as a privileged user or using the \" " << user << " \" account." ;
626
646
return EXIT_FAILURE;
627
647
}
628
648
629
- if (setuid (pw-> pw_uid ) < 0 ) {
649
+ if (setuid (*uid ) < 0 ) {
630
650
Log (LogCritical, " cli" )
631
651
<< " setuid() failed with error code " << errno << " , \" " << Utility::FormatErrorNumber (errno) << " \" " ;
632
652
Log (LogCritical, " cli" )
633
- << " Please re-run this command as a privileged user or using the \" " << user << " \" account." ;
653
+ << " Please rerun this command as a privileged user or using the \" " << user << " \" account." ;
634
654
return EXIT_FAILURE;
635
655
}
636
656
}
0 commit comments