@@ -10,29 +10,55 @@ const char * OsLogHandle::s_category = "main";
1010
1111#endif
1212
13- #if HAVE_USERADD || HAVE_PW
13+ #if !HAVE_APPLE_USER_CREATION
1414
15- auto Identity::createDaemonUser (const sys_string & name) -> Identity {
15+ static auto runCreateDaemonUserCommands (const sys_string & name) -> bool {
16+
17+ #if defined(__linux__) && defined(USERADD_PATH)
1618
17- #if HAVE_USERADD
18- #ifdef __linux__
1919 sys_string command = S (USERADD_PATH " -r -d " WSDDN_DEFAULT_CHROOT_DIR " -s /bin/false '" ) + name + S (" '" );
20- #elif defined(__OpenBSD__) || defined(__NetBSD__)
21- sys_string command = S (USERADD_PATH " -L daemon -g =uid -d " WSDDN_DEFAULT_CHROOT_DIR " -s /sbin/nologin -c \" WS-Discovery Daemon\" '" ) + name + S (" '" );
20+ (void )!system (command.c_str ());
21+ return true ;
22+
23+ #elif (defined(__OpenBSD__) || defined(__NetBSD__)) && defined(USERADD_PATH)
24+
2225 createMissingDirs (WSDDN_DEFAULT_CHROOT_DIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP, Identity::admin ());
23- #elif defined(__HAIKU__)
24- sys_string command = S (USERADD_PATH " -d " WSDDN_DEFAULT_CHROOT_DIR " -s /bin/false -n \" WS-Discovery Daemon\" '" ) + name + S (" '" );
26+ sys_string command = S (USERADD_PATH " -L daemon -g =uid -d " WSDDN_DEFAULT_CHROOT_DIR " -s /sbin/nologin -c \" WS-Discovery Daemon\" '" ) + name + S (" '" );
27+ (void )!system (command.c_str ());
28+ return true ;
29+
30+ #elif defined(__HAIKU__) && defined(USERADD_PATH) && defined(GROUPADD_PATH)
31+
2532 createMissingDirs (WSDDN_DEFAULT_CHROOT_DIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP, Identity::admin ());
33+
34+ sys_string command = S (GROUPADD_PATH " '" ) + name + S (" '" );
35+ (void )!system (command.c_str ());
36+ command = S (USERADD_PATH " -g " ) + name + S (" -d " WSDDN_DEFAULT_CHROOT_DIR " -s /bin/false -n \" WS-Discovery Daemon\" '" ) + name + S (" '" );
37+ (void )!system (command.c_str ());
38+ return true ;
39+
40+ #elif defined(__FreeBSD__) && defined(PW_PATH)
41+
42+ sys_string command = S (PW_PATH " adduser '" ) + name + S (" ' -d " WSDDN_DEFAULT_CHROOT_DIR " -s /bin/false -c \" WS-Discovery Daemon User\" " );
43+ (void )!system (command.c_str ());
44+ return true ;
45+
46+ #else
47+
48+ return false ;
49+
2650 #endif
27- #elif HAVE_PW
28- sys_string command = S (PW_PATH " adduser '" ) + name + S (" ' -d " WSDDN_DEFAULT_CHROOT_DIR " -s /bin/false -c \" WS-Discovery Daemon User\" " );
29- #endif
30- (void )!system (command.c_str ());
31- auto pwd = ptl::Passwd::getByName (name);
32- if (!pwd)
33- throw std::runtime_error (fmt::format (" unable to create user {}" , name));
34- return Identity (pwd->pw_uid , pwd->pw_gid );
35- }
51+ }
52+
53+ auto Identity::createDaemonUser (const sys_string & name) -> std::optional<Identity> {
54+
55+ if (!runCreateDaemonUserCommands (name))
56+ return {};
57+ auto pwd = ptl::Passwd::getByName (name);
58+ if (!pwd)
59+ throw std::runtime_error (fmt::format (" unable to create user {}" , name));
60+ return Identity (pwd->pw_uid , pwd->pw_gid );
61+ }
3662
3763#endif
3864
0 commit comments