X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=nssm.cpp;h=4203c6156afc758f1746365a442b1c70b5cc081f;hb=b99cfc38fcf4fbf5c87e23f93a02d1d6a6d931ca;hp=2933dea9c4956d964fe4e5346872d9de0597146a;hpb=13a14267e612fb6a28ddef57f30f9de2685fc2ff;p=nssm.git diff --git a/nssm.cpp b/nssm.cpp index 2933dea..4203c61 100644 --- a/nssm.cpp +++ b/nssm.cpp @@ -27,10 +27,34 @@ int usage(int ret) { return(ret); } +int check_admin(char *action) { + /* Lifted from MSDN examples */ + PSID AdministratorsGroup; + SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY; + BOOL ok = AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdministratorsGroup); + if (ok) { + if (! CheckTokenMembership(0, AdministratorsGroup, &ok)) ok = 0; + FreeSid(AdministratorsGroup); + + if (ok) return 0; + + fprintf(stderr, "Administator access is needed to %s a service.\n", action); + return 1; + } + + /* Can't tell if we are admin or not; later operations may fail */ + return 0; +} + int main(int argc, char **argv) { /* Require an argument since users may try to run nssm directly */ if (argc == 1) exit(usage(1)); + /* Elevate */ + if (str_equiv(argv[1], "install") || str_equiv(argv[1], "remove")) { + if (check_admin(argv[1])) exit(100); + } + /* Valid commands are install or remove */ if (str_equiv(argv[1], "install")) { exit(pre_install_service(argc - 2, argv + 2));