LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Login program in C ?? (https://www.linuxquestions.org/questions/programming-9/login-program-in-c-4175696436/)

lamonaloo2000 06-14-2021 06:57 PM

Login program in C ??
 
HELLO sory if this Noob question. I want to create C program that is able to login into user and switch user on Linux using username and password input. Basically its a login GUI which ask for user input a string for username and password and then the program logins into the respective user. But i cant find any system calls for login, switching users or authentication. I tryed to study login.c, the source for the standard login command for GNU/Linux, but i still have no clue how it works. It seem to be even more complicated since the password is encrypted in shadow file for obvious security rasons.

One thing I think I can do is simply call the login program(which is called by getty on tty) which ask for username and pass word, using the system() function. But login command is an interactive prompt and you have to be in the shell to be able to input values for username and password. But in my case I want to be able to pass strings indirectly into the login program such as thru an input file. For example i have username and password strings and redirect that into the login program instead of answering the prompt interactively. Dunno if piping will works for login bcs it doesnt seem to be able to read from stdin. What is best approach to acomplish this in a C program ?? Is it even Posible? THANK YOU

DISTRIBUTION: Debian 10(console)
Language: C, Bash

NevemTeve 06-14-2021 11:10 PM

Maybe you could examine the source of programs 'login', 'passwd', 'su', 'xdm'.

pan64 06-15-2021 01:30 AM

this is how login process works: https://www.linuxnix.com/how-login-p...work-in-linux/
using GUI it is a bit different, but as you can see it is much more than a single app (like login).

If you want to remote login to another computer - using ssh - you can configure it to read credentials from file, do not need to reinvent it.

GazL 06-15-2021 06:25 AM

It's a non-trivial task. If you really want to investigate this rather than just using something like su, sudo, or gksu, then start reading here:
https://man7.org/linux/man-pages/man...entials.7.html
http://www.linux-pam.org/Linux-PAM-h...x-PAM_ADG.html

boughtonp 06-15-2021 06:46 AM


 
Not clear if you're asking for CLI or GUI solution, but in the latter case the login screen is sometimes referred to as a "greeter".

The LightDM Wikipedia page lists a bunch and includes links to their repositories, so you can examine how they work, maybe even customize an existing one to your own preferences.


lamonaloo2000 06-15-2021 09:42 AM

Tahnks you guys these are some graet resources. But i realise I am probly am too N00b for attempting this kind of thing, I tryed studying the source for LightDM, GDM greeters and login,su, but I am stumped . So From what i gather so far, you can login or switch user by changing USER environment variables, which is done thru PAM ?

On a more general note, dose anyone have tips on how to approach reading and understanding source code , for ejample where to start and how to break it down ? Especially tackling large source files with lot of included Libs ? THANk you

TB0ne 06-15-2021 10:54 AM

Quote:

Originally Posted by lamonaloo2000 (Post 6259310)
Tahnks you guys these are some graet resources. But i realise I am probly am too N00b for attempting this kind of thing, I tryed studying the source for LightDM, GDM greeters and login,su, but I am stumped . So From what i gather so far, you can login or switch user by changing USER environment variables, which is done thru PAM ?

Honestly, I think you're over complicating this, based on your initial post. It seems like you're wanting the person logging in to 'switch' to another user and ostensibly run a program, correct? If so...why not just set their login shell to be whatever program you'd like, instead of bash? Or have a script auto-execute after they log in? You can use su or sudo to 'switch' users pretty easily.

May have misunderstood your goal here...can you explain more clearly?
Quote:

On a more general note, dose anyone have tips on how to approach reading and understanding source code , for ejample where to start and how to break it down ? Especially tackling large source files with lot of included Libs ? THANk you
There are no tips; if you understand the language, you just have to slog through it.

pan64 06-15-2021 11:08 AM

Quote:

Originally Posted by TB0ne (Post 6259334)
Honestly, I think you're over complicating this,

Yes, I think the wanted tool is already created, probably sudo (or something else)?
To OP: what do you want to achieve at all? What is it all about?

dugan 06-15-2021 11:51 AM

If you want to log in as a certain user and run certain commands, just use ssh.

If you want it a bit more automated, then use Ansible. Which just logs in and runs commands using ssh.

If you want to do it a more old-fashioned way, use expect to automate ssh. Nobody does this.

If you want to reinvent these wheels as an exercise: well, I’m currently reading Advanced Programming in the UNIX Environment, which goes into detail about how these things work. Pick up a copy.

lamonaloo2000 06-15-2021 01:22 PM

Quote:

Yes, I think the wanted tool is already created, probably sudo (or something else)?
To OP: what do you want to achieve at all? What is it all about?

heheh sorry guys i know this is abit vague. Basically in a broader sense wat I am creating is a basic custom login screen (GUI) using NCurses library. Kind of liek how on Microsoft Windows or any UNIX Login Managers they prompt user for login on startup. My progrem will run on system startup insted of the getty login process for the Debian console. So the UI will take key input, it will have a input for both Username and password that user can type in, and so these parameters will be stored to string or written to temp file. Problem is, what to do after with these Information ?

I was thinking of ,instead of writing login system frum scratch, I use the existing user programs like login, su, sudo and call them thru Bash script. So basicly in the program, User inputs the username and password strings on a fancy GUI, and THEN i call the standard GNU/LINUX login program behind the scenes. But i dunno how to pass these credentials username and password (from the C program) to another login program. For example say i have user enter data on the login screen (GUI)
--------------------------------
| Username: root
| Password: abc123
|
| [ Login ]
--------------------------------
So then i store these two value into respective strings, such as char *uname, char *pwd. But thenI need somehow to pass these to login program. The login command is an interactive prompt, goes something liek this (on Debian):


debian login: [ENTER USERNAME HERE]
Password: [ENTER PASSWORD HERE]

** if password is incorrect **

Login incorrect

But porblem is, this login program above is interactive, requires someone to be present at the shell and input username and value. But my program the user will not be directly using this login program/command. Is it posible to do something like this?::


debian login: [ char* uname ]
Password: [ char* pwd ]

** if password is incorrect **

Login incorrect

rtmistler 06-15-2021 02:14 PM

Quote:

Originally Posted by lamonaloo2000 (Post 6259372)
i know this is abit vague. Basically in a broader sense wat I am creating is a basic custom login screen (GUI) using NCurses library. Kind of liek how on Microsoft Windows or any UNIX Login Managers they prompt user for login on startup.

NCurses is one option, as is GTK, and also Qt programming. Investigate how to write 'a' login screen using one of those development methods and libraries. You can design a full screen interface that prints out the prompt and has entry fields for the username and password, and also an OK or LOGIN button. Debug that application so that you have it designed enough where odd things like CTRL-C or ESC don't have impacts and keep the user on that screen. Also test it for entry modes, such as TAB and only keyboard entry versus mouse use, but also mouse use. Just allow one username and password for starters to test it fully and verify it works as a login screen should.
Quote:

Originally Posted by lamonaloo2000 (Post 6259372)
My progrem will run on system startup insted of the getty login process for the Debian console. So the UI will take key input, it will have a input for both Username and password that user can type in, and so these parameters will be stored to string or written to temp file. Problem is, what to do after with these Information ?

This is why I suggested refining the "interface" first. Every system type will have libraries or a mechanism for a program of this type to then say, "I have credentials entered by a user, are they valid?" This would be a next task, to figure out how to connect the output of an authentication attempt with the system. And you then should be able to (a) deal with a bad login attempt, and (b) really log them in.
Quote:

Originally Posted by lamonaloo2000 (Post 6259372)
I was thinking of ,instead of writing login system frum scratch, I use the existing user programs like login, su, sudo and call them thru Bash script. So basicly in the program, User inputs the username and password strings on a fancy GUI, and THEN i call the standard GNU/LINUX login program behind the scenes. But i dunno how to pass these credentials username and password (from the C program) to another login program. For example say i have user enter data on the login screen (GUI)

Not impossible, but the scope of your project just grew immensely with these statements. Why? The display manager is a pretty large thing and you're proposing to replace it with your own code. That is the renditions on the screen of a background, input box styling, and button styling, along with text entry. All quite possible, libraries like fltk, Qt, and GTK have all that for you. Their source is available, so you can cut/copy/paste from their source to help you along. First, I recommend you start with the earlier suggested renditions.

lamonaloo2000 06-15-2021 02:52 PM

TAHNK YOU man yes you right i should probs focus on laying the GUI down first . Later on I will look at some source for displey managers and try to see how they do the login user authentication stuff, copy some stuff here an there

dugan 06-15-2021 03:38 PM

Quote:

Originally Posted by lamonaloo2000 (Post 6259372)
So then i store these two value into respective strings, such as char *uname, char *pwd. But thenI need somehow to pass these to login program. The login command is an interactive prompt, goes something liek this (on Debian):


debian login: [ENTER USERNAME HERE]
Password: [ENTER PASSWORD HERE]

** if password is incorrect **

Login incorrect

But porblem is, this login program above is interactive, requires someone to be present at the shell and input username and value. But my program the user will not be directly using this login program/command. Is it posible to do something like this?::


debian login: [ char* uname ]
Password: [ char* pwd ]

** if password is incorrect **

Login incorrect

Yes. This is exactly what pseudoterminals are for.

Here's one link:

Using pseudo-terminals (pty) to control interactive programs


All times are GMT -5. The time now is 10:16 PM.