Start Command Prompt (CMD) from java as Administrator - java

I am working on a java project. This project need admin privileges to execute some process in the system. For this is there any way to give admin privileges while starting the project? Is there any way to start command prompt with admin privileges from java?

Disclaimer: I assume you are asking this specifically for the Windows platform
There are two ways I'd recommend:
Use the runas program
This is the easiest way and since you actually want a console window the password prompt doesn't look awkward. On the other hand a user might find it suspicious to enter his password into a console window.
Use the CreateProcessWithLogonW API function
This requires you to use a Win32 Java library. (I'd recommend JNA). It is a little more work but it might lead to a better user experience because of the standard Windows login dialog.

You can't simply request admin-privileges and you can't start an admin-command-line from an application that was not launched with admin-privileges. This would be a serious security problem.
Instead, you can check if your app was started with administrative privileges and if not prompt the user to do so (showing a little How-To on how to archive this).
Unfortunately this seams to be a bigger problem since there is no standardized way to do this covering all platforms. However this thread gives some ideas: Detect if Java application was run as a Windows admin

Related

Is it possible to interact with a Linux Command line interface (CLI) through a Graphical user interface (GUI)?

I am quite a beginner, any advice is much appreciated.
I have a linux application OpenBTS used to simulate and run a GSM network on a software defined radio device e.g USRP.
I want to build an application that interfaces with the OpenBTS command line on Linux. I want to give the user an easier way to configure and to display the current configurations of the application. The user would have an interface in which he could play with the configurations without the need to use the terminal.
I don't know if this is possible ?
Is it possible to interact with a Linux CLI through a GUI ? If yes what is the most efficient programming language, coding technique or approach to do that ?
Thanks a lot
You could generate a config file using your GUI and then use standard in to get the configuration into OpenBts. When launching OpenBts with the configuration file config.txt you could simply run it as follows.
./OpenBts < config.txt
You could also do this from you GUI by launching OpenBts in a process from your GUI application in a similar fashion, however this requires a fork() and exec()
In C++, you can take inputs from user through GUI then run appropriate openBTS commands using system()/popen(). Based on the return values of system()/popen(), can provide status of the operation back to the user.

Executing installers programmatically

I am working on a project for my users to generate HTML pages, JS files, CSS files etc... through dragging and dropping visual elements and by going through a series of wizzards. The goal I am trying to achieve is to create an application in which users selects a runtime, for example tomcat or nodeJS and then be able to create content for that specific runtime in the manner described above. Part of this application would be the preparing of your runtime, and by that I mean installing tomcat or nodeJS for you. so the big question here would be, how do I run installers through program code ? (java 7/8 mainly)
what I have tried:
Using a strategy design pattern to determine the host platform and run a series of command in the terminal to execute an installer, problem here is that some installers start a wizzard and require further user input. and in some cases the host platform would require elevated permission. Is there perhaps a solution where one adds arguments to the terminal commands to skip an installation wizzard ?
Any help on this subject would be greatly appreciated.
Answer of Fildor was most usefull.

Using "sudo" to elevate privilege (temporarily) when writing to /usr/shared/

I am developing an OSX application for school where multiple users of the same mac will able to launch the program based on shared configuration (i.e. database connection info).
By design, the normal user without administrative privilege should not be allowed to modify the configuration.
Based on unix convention, I am storing the configuration file under /usr/shared/, but this caused another problem:
- Even when logged in as administrator, application will not able to write to the location above. Users will have to launch the application with elevated privilege (using sudo) if they need to make changes to the configuration file.
My question is, whether using "sudo" to write to restricted area is a common practice in mac osx ?
I would say that using "sudo" on a Mac is only common practice for certain types of users, such as network administrators and users who like to do more than just use standard applications.
I certainly would not expect students and teachers to be using the terminal and sudo.
When developing OSX apps, Apple provides the Authorization Services, from the security framework, for the app to be able to separate privileged features from standard ones and prompt users for their credentials when required.
You will have seen this in the System Preferences panes, where you see the padlock icon. Any application can call upon these services, though I'm not sure if it's possible with Java. This is Apple's recommended method of handling a problem such as this, in addition to factoring out privileged tasks to a separate application with a function they term 'SMJobBless'.
Interestingly, the domain of your question maps directly onto Apple's described scenario of using this system for "Simple, Self-Restricted Applications".
With that being said, if only yourself or other network administrators are going to be modifying the config file, then I'd recommend leaving out any features in the app for modifying it and doing it directly with sudo and a text editor such as vi, when required.
Definitely, unless you chmod its privilege with sudo(root)

Java execute Linux commands that require su?

What is the best way to run Linux commands that require "su" with Java? I want it to be very reliable, as for security goes I don't care much about it the system is not a production server. No one ever connects to the machine but myself. I thought about logging in as root so I don't have to deal with the su command and its disabled by default. I'm using Debian 6.
The only thing is that one of my requirements is that the program should start after the normal user logs in without any farther user input. I would set auto login for the normal user and when the computer is turned on and after the normal user logs in I would want that Java program to run as sudo, su without any user input. Would that be possible?
Please tell me a simple and reliable solution.
Regards
Use sudo instead.
It can be configed to allow specific command run without typing in password.
For running programs that require root access, they should be run with sudo. The user will be asked to enter a password. As mentioned in the previous answer, sudo can be used to configure commands to be run as root that do not require entering a password. Try looking at the man page for sudo for more information regarding that.
If you want to run programs on system startup, you can write a bash script to run the command gksudo java -jar path when the user logs in. This will prompt the user with a dialog box to enter a password.
I also recommend doing a system call at the beginning of the program to ensure root access.

Windows: Running privileged command from non-privileged using CMD (or Java)

I will have a service that runs as administrator and listens on a port. My GUI program will talk to the administrator service for the items that require administrator privileges.
If the service is not yet running, I will need to start it. How can I get my GUI program to run a command as administrator? I assume the user will be asked if they want to continue.
I am hoping there is something I could type in a CMD window because that should fit very well into my Java program. I am thinking something like run-as-admin javaw my-service.jar where run-as-admin is the command that asks whether to continue or not.
Windows contains the tool "runas" which can be used to start any executable with a different user account.
On the commandline you would then use:
runas /user:Administrator "javaw -jar my-service.jar"
The only drawback is, that you need to type in the password, you cannot supply the password as an argument to the runas command
You have a problem here. Non admin processes cannot start services. The very simplest thing to do is to arrange that the service starts automatically. I'd strongly recommend you take that route.
What you are thinking of doing would involve creating a helper application that includes the UAC manifest with requestedExecutionLevel set to requireAdministrator. This helper app could then start the service. The problem with this is that it requires that the logged on user is in the administrators group which you cannot guarantee will be the case.
All in all, starting the service automatically is to be preferred.
You can't execute a batch file as administrator. You need to create a shortcut to that file and then set the flag in the Shortcut 'Execute as Administrator', if this is really what you want to do.
To do this from the desktop, select the Shortcut, right click and Properties->Shortcut tab->Advanced button. Then check Execute as Administrator checkbox.
To do this programmatically, see How to set "Run as administrator" flag on shortcut created by MSI installer
We use the Wrapper library for this:
http://sourceforge.net/projects/wrapper/
The official website seems to be suffering from dynamic dns issues at the moment, so here is the wayback archive version.
From the command line (and by extension, Java) you can install, uninstall, start, and stop your Java application as a service.

Categories