[LCP]Small question about a program...
Stratis Aftousmis
karen4253 at sbcglobal.net
Fri Apr 16 12:59:02 UTC 2004
#define _XOPEN_SOURCE
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "/Source/str.h"
int set_term()
{
if (system("stty -echo") == -1)
return -1;
else
return 0;
}
int reset_term()
{
if (system("stty echo") == -1)
return -1;
else
return 0;
}
int main()
{
char buff[1024], pass[100], string[100], salt[100], user[100], temp[100];
FILE *file;
if ((file = fopen("/etc/shadow", "r")) == NULL) {
fprintf(stderr, "fopen: Failed.\n");
exit(1);
}
if (fread(buff, 1 , sizeof(buff), file) == 0) {
fprintf(stderr, "fread: Failed.\n");
exit(1);
}
if (substr(buff, getlogin(), "\n", temp) == NULL) {
fprintf(stderr, "substr: Failed.\n");
exit(1);
}
if (substr(temp, "$", ":", pass) == NULL) {
fprintf(stderr, "substr: Failed.\n");
exit(1);
}
if (!strncpy(salt, pass, sizeof(salt))) {
fprintf(stderr, "strncpy: Failed.\n");
exit(1);
}
fprintf(stdout, "Please enter your password.\n");
fprintf(stdout, "Password:");
if (set_term() == -1) {
fprintf(stderr, "set_term: Failed.\n");
exit(1);
}
if (nlnuke(user, sizeof(user)) == -1) {
reset_term();
fprintf(stderr, "nlnuke: Failed.\n");
exit(1);
}
if (reset_term() == -1) {
fprintf(stderr, "reset_term: Failed.\n");
exit(1);
}
if (!strncpy(string, crypt(user, salt), sizeof(string))) {
fprintf(stderr, "strncpy: Failed.\n");
exit(1);
}
if (strcmp(pass, string) != 0) {
fprintf(stderr, "\nlogin: Failed.\n");
exit(1);
}
if (fclose(file) == EOF) {
fprintf(stderr, "fclose: Failed.\n");
exit(1);
}
fprintf(stdout, "\n%s logged in.\n", getlogin());
return 0;
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Used with an include of my own making, 'str.h'.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*C string manipulation functions:
Copyright (C) 2004-2010 stratis aftousmis E-mail: karen4253 at sbcglobal.net
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/
#include <strings.h>
#include <stdio.h>
char *substr(char *buf, const char *start, const char *end, char *result)
{
char *buff;
if ((buff = strstr(buf, start)) == NULL)
return NULL;
while (*buff != *end && *buff != '\0')
*result++ = *buff++;
*result = '\0';
return result;
}
int nlnuke(char st[], size_t in)
{
int i = 0;
char s[in];
if (fgets(s, sizeof(s), stdin) == NULL)
return -1;
while (s[i] != '\n' && s[i] != '\0') {
st[i] = s[i];
i++;
}
st[i] = '\0';
return 0;
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
First post, Hello! ;)
I just wanted to show off one of my programs, get any kind of input available. :)
Thank you.
Stratis
More information about the linuxCprogramming
mailing list