Entering password into terminal through c

Multi tool use
Multi tool use


Entering password into terminal through c



I searched for this question and could only find python variants. If this is a duplicate apologies upfront.



I'm trying to write a script in c that auto setup systems so that I don't have too manually do it on heaps of computers. For part of the script I need to enter a password.





It is not the terminal that requires a password but some program you are running. Some programs just read stdin but some use low-level terminal IO to ensure that passwords have to be entered manually. You will have to look at each program to find which method they use.
– cdarke
Jul 3 at 5:37



stdin





I think you would be better off with an existing single-sign-on tool. You are not asking for such a tool (which would be off-topic), I know. But it would be worth the effort of searching.
– Yunnosch
Jul 3 at 5:40






I'm actually looking to input the computer admin password and not a program password. I also would like to know how to detect when bash requires a password
– F3nrir
Jul 3 at 5:46





bash does not require a password, but the login process might, or the sudo program (which has nothing to do with bash). Under what circumstances are you requiring a password?
– cdarke
Jul 3 at 5:52



bash


sudo


bash





I meant sudo bash
– F3nrir
Jul 3 at 5:59




2 Answers
2



If you have access to the terminal, why not use bash for these setups? In both instances of C, or bash, if you're accessing high privilege level areas, you'll need privilege access. The thing you'd be looking for would be a sudo command. It'll prompt you for the password prior to running it, and is just ran as follows:


sudo someExecutable





I already knew this and I would still like to know how to insert passwords into terminal through c though
– F3nrir
Jul 3 at 5:47





Would this solve your problem? stackoverflow.com/questions/29456701/…
– Nova Ardent
Jul 3 at 5:51





thanks that might do
– F3nrir
Jul 3 at 6:09





No problem, good luck.
– Nova Ardent
Jul 3 at 6:14



Some programs (that prompts for password) use isatty(STDIN_FILENO) call for ensuring that password is typed via terminal.


isatty(STDIN_FILENO)



In that case it not possible use normal pipes for sending password to the program.



For those programs, you can create a pseudo terminal, that is used instead of a pipe. Example:


void test(void)
{
const int fdm = posix_openpt(O_RDWR | O_NOCTTY);
if (fdm != -1)
{
char name[100];
unlockpt(fdm);
ptsname_r(fdm, name, sizeof(name));

const int fds = open(name, O_RDWR | O_NOCTTY);
if (fds != -1)
{
const pid_t child_pid = fork();

if (child_pid == -1)
{
// TODO: Error handling
}
else if (child_pid == 0)
{
dup2(fds, STDIN_FILENO);
dup2(fds, STDOUT_FILENO);
close(fds);
close(fdm);
execl("/sbin/program_that_requires_password_from_terminal", "blabla", (char*) NULL);
}
else
{
const char password = "mypasswordn";
int status;
close(fds);
// TODO: wait prompt from the program by reading fdm
write(fdm, password, strlen(password));
waitpid(-1, &status, 0);
close(fdm);
}
}
}
}



In that way isatty(STDIN_FILENO) in the target program returns 'true'.


isatty(STDIN_FILENO)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

tiec9 QJzxvS,sTsC3Ow xtcDuankOgKiMIn,xFcWT ss8 NiS,tYrzrUrAndsJV107gzj09eFaT8GUcd0m M9YqP
Zsb7c9gOYxYIzhICOVCJt 4T2z

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications