void dotitle(Char **vc, struct command * c) { int k; char titlebuf[512]; char errbuf[128],err2[128]; char **v; Char **nvc; UNREFERENCED_PARAMETER(c); vc++; nvc = glob_all_or_error(vc); if (nvc == NULL) return; if (nvc != vc) cleanup_push(nvc, blk_cleanup); if ((k = GetConsoleTitle(titlebuf, sizeof(titlebuf))) != 0) { setcopy(STRoldtitle,str2short(titlebuf), VAR_READWRITE|VAR_NOGLOB); } titlebuf[0] = '\0'; v = short2blk(nvc); if (nvc != vc) cleanup_until(nvc); cleanup_push((Char **)v, blk_cleanup); for (k = 0; v[k] != NULL ; k++){ __try { StringCbCat(titlebuf,sizeof(titlebuf),v[k]); StringCbCat(titlebuf,sizeof(titlebuf)," "); } __except(GetExceptionCode()) { stderror(ERR_TOOMANY); } } if (!SetConsoleTitle(titlebuf) ) { make_err_str(GetLastError(),errbuf,128); (void)StringCbPrintf(err2,sizeof(err2),"%s",v[k]); stderror(ERR_SYSTEM,err2,errbuf); } cleanup_until((Char **)v); return; }
The default action is to shutdown without a reboot.\n\"now\" must be \ specified to actually shutdown or reboot\n"}; void doshutdown(Char **vc, struct command *c) { unsigned int flags = 0; unsigned char reboot,shutdown,logoff,shutdown_ok; char **v; char *ptr; char errbuf[128]; int k; HANDLE hToken; TOKEN_PRIVILEGES tp,tpPrevious; LUID luid; DWORD cbPrevious = sizeof(TOKEN_PRIVILEGES); if (gdwPlatform != VER_PLATFORM_WIN32_NT) { stderror(ERR_SYSTEM,"shutdown","Sorry,not supported on win95"); } shutdown_ok = reboot = shutdown = logoff = 0; gflag = 0; tglob(vc); if (gflag) { vc = globall(vc); if (vc == 0) stderror(ERR_NAME | ERR_NOMATCH); } else vc = gargv = saveblk(vc); trim(vc); v = short2blk(vc); for (k = 0; v[k] != NULL ; k++){ if ( v[k][0] == '-' ) { ptr = v[k]; ptr++; while( ptr && *ptr) { if (*ptr == 'f') flags |= EWX_FORCE; if (*ptr == 'r') reboot =1; else if (*ptr == 'l') logoff =1; else { blkfree((Char **)v); stderror(ERR_SYSTEM,"Usage",shutdown_usage); } ptr++; } } else if (!lstrcmpi(v[k],"now")) { shutdown_ok = 1; } } if (k == 0) { blkfree((Char**)v); stderror(ERR_SYSTEM,"Usage",shutdown_usage); } if (!reboot && !logoff){ flags |= EWX_SHUTDOWN; shutdown = 1; } if (reboot && logoff ) { blkfree((Char **)v); stderror(ERR_SYSTEM,"Usage",shutdown_usage); } if (reboot) flags |= EWX_REBOOT; if (logoff) flags |= EWX_LOGOFF; if ((reboot || shutdown) && (!shutdown_ok) ) { blkfree((Char **)v); stderror(ERR_SYSTEM,"shutdown","Specify \"now\" to really shutdown"); } if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES| TOKEN_QUERY, &hToken) ){ make_err_str(GetLastError(),errbuf,128); blkfree((Char **)v); stderror(ERR_SYSTEM,"shutdown failed",errbuf); } if (!LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid)) { make_err_str(GetLastError(),errbuf,128); blkfree((Char **)v); stderror(ERR_SYSTEM,"shutdown failed",errbuf); } tp.PrivilegeCount = 1; tp.Privileges[0].Luid = luid; tp.Privileges[0].Attributes = 0; if (!AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(tp),&tpPrevious, &cbPrevious)){ make_err_str(GetLastError(),errbuf,128); blkfree((Char **)v); stderror(ERR_SYSTEM,"shutdown failed",errbuf); } tpPrevious.PrivilegeCount = 1; tpPrevious.Privileges[0].Luid = luid; tpPrevious.Privileges[0].Attributes |= SE_PRIVILEGE_ENABLED; if (!AdjustTokenPrivileges(hToken,FALSE,&tpPrevious,cbPrevious,NULL, NULL)){ make_err_str(GetLastError(),errbuf,128); blkfree((Char **)v); stderror(ERR_SYSTEM,"shutdown failed",errbuf); } if ( !ExitWindowsEx(flags,0) ) { make_err_str(GetLastError(),errbuf,128); blkfree((Char **)v); stderror(ERR_SYSTEM,"shutdown failed",errbuf); } }
void try_shell_ex(char **argv,int exitsuccess, BOOL throw_ok) {/*FIXRESET*/ char *prog; char *cmdstr, *p2, *cmdend; char *originalPtr = NULL; unsigned int cmdsize,cmdlen; char err2[256]; char *ptr; SHELLEXECUTEINFO shinfo; unsigned long mask = SEE_MASK_FLAG_NO_UI; BOOL rc; char *extension; prog=*argv; dprintf("trying shellex for prog %s\n",prog); ptr = prog; if (!is_url(prog)) { while(*ptr) { if (*ptr == '/') *ptr = '\\'; ptr++; } extension = ptr; // search back for "." while(extension != prog) { if (*extension == '.') { extension++; break; } else extension--; } /* check if this matches a member in the no_assoc array. */ if (extension != prog) { if (find_no_assoc(extension)) return; } } originalPtr = cmdstr= heap_alloc(MAX_PATH<<2); cmdsize = MAX_PATH<<2; p2 = cmdstr; cmdlen = 0; cmdend = p2; argv++; // the first arg is the command dprintf("try_shell_ex calling c_a_a_q"); if(!concat_args_and_quote(argv,&originalPtr,&cmdstr,&cmdlen,&cmdend,&cmdsize)) { errno = ENOMEM; heap_free(originalPtr); return; } *cmdend = 0; memset(&shinfo,0,sizeof(shinfo)); shinfo.cbSize = sizeof(shinfo); shinfo.fMask = SEE_MASK_FLAG_DDEWAIT | mask; shinfo.hwnd = NULL; shinfo.lpVerb = NULL; shinfo.lpFile = prog; shinfo.lpParameters = &cmdstr[0]; shinfo.lpDirectory = 0; shinfo.nShow = SW_SHOWDEFAULT; ptr = fix_path_for_child(); rc = pShellExecuteEx(&shinfo); if (rc ) { if (exitsuccess) ExitProcess(0); errno = 0; heap_free(originalPtr); return; } if (throw_ok) { // if we got here, ShellExecuteEx failed, so reset() via stderror() // this may cause the caller to leak, but the assumption is that // only a child process sets exitsuccess, so it will be dead soon // anyway restore_path(ptr); make_err_str(GetLastError(),cmdstr,512);//don't need the full size (void)StringCbPrintf(err2,sizeof(err2),"%s",prog); stderror(ERR_SYSTEM,err2,cmdstr);/*FIXRESET*/ } heap_free(originalPtr); restore_path(ptr); errno = ENOEXEC; }