C++ edycja pliku /etc/shadow

Potrzebujesz pomocy z C, C++, perl, python, itp.
buker999
Posty: 89
Rejestracja: 18 października 2011, 21:22
Lokalizacja: Warszawa

C++ edycja pliku /etc/shadow

Post autor: buker999 »

Zamieszczam mój kod, z którym mam mały problem. A mianowicie, chodzi o podmianę hasła w pliku /etc/shadow, wszystko ładnie działa ale zawsze dla pierwszej linii, więc troszkę lipa. Jak ktoś byłby wstanie pomoc dopieścić ten program. Nie mogę użyć żadnej interakcji (typu pytanie o hasła itp.).

Kod: Zaznacz cały

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdexcept>
#include <stdlib.h>
using namespace std;


int main(int argc, char *argv[])
{
	fstream plik;
	string dane;
	string login, haslo;
	size_t found;
	short kon;
	char *buf;




	if(argv[1] != NULL && argv[2] != NULL)
	{
		login = argv[1];
		haslo = argv[2];
	}
	try
	{
		plik.open("/etc/shadow", ios::in);
		if(!plik.is_open())
		{
			throw runtime_error("Nie mozna znalezc pliku");
		}


		//plik>>dane;


		plik.seekg(0, ios::end);
		int lenght = plik.tellg();
		plik.seekg(0, ios::beg);
		buf = new char[lenght + 1];
		plik.read(buf, lenght);
		buf[lenght] = '\0';


		dane = string(buf);
		delete []buf;
		found = dane.find(login);
		if(found == string::npos)
			return -1;
			//throw exception("Nie ma takiego loginu");
		login += ":";
		kon = login.length();
		do
		{
			kon++;
		}
		while(dane[kon]!=':' && kon != dane.size());
		dane.replace(login.length(),kon-login.length(),haslo);
		buf = new char[dane.size() + 1];
		strcpy(buf, dane.c_str());
		
		//plik.write(&dane[0], dane.length());
		plik.close();
		plik.clear();


		plik.open("shadow", ios: :o ut);
		plik<<dane;
		//plik.write(dane.c_str(), dane.size());
		plik.close();
	}
	catch(runtime_error &ex)
	{
		cerr<<ex.what()<<"\n";
	}


	
	return 0;
}
Awatar użytkownika
Yampress
Administrator
Posty: 6420
Rejestracja: 09 sierpnia 2007, 21:41
Lokalizacja: PL

Post autor: Yampress »

Changing a users password inside a script
In an ideal world you'd never need to change the password associated with a user account without using passwd, but there are times when it is helpful to script such things.

The naive attempts to automate the use of passwd will fail, so the standard advice has always been to use a tool like expect to interactively call the passwd binary.

But there is an alternative approach which is more sensible which is to use the usermod command to change a password.

Assume you have a user account called guest upon your system and you wish to set the user's password to openaccess you can do this by running:
# hash=$(echo openaccess | openssl passwd -1 -stdin)
# usermod --pass="$hash" guest

If you wish you could combine that into a single line:
# usermod -p $(echo openaccess | openssl passwd -1 -stdin) guest

If a local user can see the commands you're running in the output of "ps", "top", or similar then this is insecure - but if you generate the hash remotely you should probably be safe enough.

Kod: Zaznacz cały

echo username:password | chpasswd 

This will change password for user `username' to `password'
Spod C++ wywołaj funkcje (polecenie) systemowe.
ODPOWIEDZ