password .c

can somebody provide me an .c with password inside ?
i mean when i rule ./file to ask me for a password and only if i type the right password continues else it exit
i realy need this please. thanks

very odd request. I tried to understand what your trying to do but failed. Can you provide more details?

People who post homework and repeatedly break the homework posting rules will be banned. Cheaters are not welcome here.
If you post homework, and do not complete the homework template completely and accurately, you will be banned.
You should post your problem on Homework & Coursework Questions - The UNIX and Linux Forums but before that you should read:

  1. Community Spirit and Ethos on Homework & Coursework
  2. Rules for Homework & Coursework Questions Forum

Thank You.

The UNIX and Linux Forums
Reply With Quote

Tip below :wink:

pass=Password;echo -n "Type password:";read pass2;if [ ${pass}. != ${pass2}. ];then echo "Wrong $pass, bye";else echo $pass OK ;fi

this is not my homework, i know looks like one.
Thank you for that code but this is for a bash script and i need it in c++ i mean .c script.
thanks anyway !

The safest way to do what you want is to create one special user account - in this example let's call it trial.

  1. add the user account
  2. logon as trail
  3. put the script(s) and data files all in the trial home directory.
  4. chmod 700 all of the file(s), including the trial home directory itself.

You now have an application that can be run only by the trial user. (or root)

Your users can get to the tiral application one of two ways

  1. login as trial
  2. su trial -> requires the password for trial account.

thanks for the idea it nice, but i need a passworded .c file.When i run it to ask me the password and only who know that pasword can run it.
thanks again

It is not that simple. Do you know about PAM or keberos. for example?
Authentication on your system can be complex. If you are creating a 'secure' app, then you need to follow system rules. If you are trying to keep your little brother off the computer then write some that uses the getpass() call.

man getpass has an example.

oh i see you didn`t understand me.
I will show you like this.
This is a bash script wich perfom uname -a with password protected so if you know the password you get the uname -a else exit.danremo`s script thx.
--
pass=espana
echo -n "Type password:";
read pass2;
if [ ${pass}. != ${pass2}. ];
then
echo "Wrong password, bye";
else
echo OK password acepted !;
uname -a
fi
--
now, what i am searching about is the same thing only writed in c++ wich i have very little knowledge about.
thank you !

/* mypass.c */
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char **argv)
{
    char *pswd="espana";
    char *result=getpass("Enter password");   
    if(strcmp(pswd, result)==0 )
       return 0;
    return 1;
    
}

Are you sure this is not homework? This makes no sense - anybody who is at the commandline prompt can type uname -a and get a response.

  ./mypass
  if [ $? -eq 0 ] ; then
      uname -a
  else
      echo 'bad password'
  fi

yes this is what i looking for !
thanks man ! very very nice !

---------- Post updated at 01:45 PM ---------- Previous update was at 01:43 PM ----------

i am wondering if there is any chance to hide the password ? when i strings the file shows :
/lib/ld-linux.so.2
_Jv_RegisterClasses
__gmon_start__
libc.so.6
getpass
system
strcmp
_IO_stdin_used
__libc_start_main
GLIBC_2.0
PTRh
espana <--
Enter password

dont know , some md5sum maybe ?