In: Computer Science
Purpose
This assignment should give you experience in using file descriptors, open(), close(), write(), stat() and chmod(), perror(), and command line arguments.
Program
Write a C++ program that will allow you to add messages to a file that has NO permissions for any user.
A Unix system has many files that have sensitive information in them. Permissions help keep these files secure. Some files can be publicly read, but can not be altered by a regular user (ex.: /etc/passwd). Other files can't be read at all by a regular user (ex.: /etc/shadow).
The program you develop should take a message given as a command line argument and append it to a file (also specified on the command line). The file should have no permissions, both before and after the message is appended. Of course, the file should be owned by you.
Your program should also have a -c option that will clear the file before the message is appended.
Algorithm
Useful Hints
Input
None, really. Just command line arguments.
Error Checking
If the log file cannot be opened, an appropriate error message should be printed and the program should exit. If the file has any permissions at all, the file should be rejected as insecure, and the program should exit.
Example Run
Your program executable is called "z123456" here.
% rm log % ./z123456 Usage: seclog [-c] out_file message_string where the message_string is appended to file out_file. The -c option clears the file before the message is appended % chmod u-w . % ./z123456 log "Hello" Permission denied % chmod u+w . % ./z123456 log "Hello" % ls -l total 72 ---------- 1 z123456 student 6 Sep 24 18:39 log -rwxr-xr-x 1 z123456 student 26385 Sep 24 18:38 z123456 -rw-r--r-- 1 z123456 student 2204 Sep 24 18:36 z123456.cxx -rw-r--r-- 1 z123456 student 30896 Sep 24 18:38 z123456.o % ./z123456 log "Hello" % ls -l total 72 ---------- 1 z123456 student 12 Sep 24 18:40 log -rwxr-xr-x 1 z123456 student 26385 Sep 24 18:38 z123456 -rw-r--r-- 1 z123456 student 2204 Sep 24 18:36 z123456.cxx -rw-r--r-- 1 z123456 student 30896 Sep 24 18:38 z123456.o % chmod 400 log % tail log Hello Hello % ./z123456 log "Wait, there's more" log is not secure. Ignoring. % chmod 000 log % ./z123456 log "Wait, there's more" % ls -l total 72 ---------- 1 z123456 student 31 Sep 24 18:41 log -rwxr-xr-x 1 z123456 student 26385 Sep 24 18:38 z123456 -rw-r--r-- 1 z123456 student 2204 Sep 24 18:36 z123456.cxx -rw-r--r-- 1 z123456 student 30896 Sep 24 18:38 z123456.o % chmod 400 log % tail log Hello Hello Wait, there's more % chmod 000 log % ./z123456 -c log "Clean start" % ls -l total 72 ---------- 1 z123456 student 12 Sep 24 18:41 log -rwxr-xr-x 1 z123456 student 26385 Sep 24 18:38 z123456 -rw-r--r-- 1 z123456 student 2204 Sep 24 18:36 z123456.cxx -rw-r--r-- 1 z123456 student 30896 Sep 24 18:38 z123456.o % chmod 400 log % tail log Clean start % chmod 000 log %
The required code is given below in case of any doubts you can ask me in comments.
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(int agrc,char*argv[])
{
struct stat sb;
if(agrc != 2){
cout << "invalind command line arguments "<<
endl;
return 0;
}
if (stat(argv[1], &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
if((sb.st_mode & 777) != 0) {
cout<<"file has permission and its
invalid"<<endl;
}
int status = chmod(argv[1], 0200);
if(status){
cout<<"permission sucessfull change"<<endl;
}
ofstream myfile;
myfile.open (argv[1], ios::out | ios::app);
myfile<<argv[2]<<endl;
status = chmod(argv[1], 0000);
if(status){
cout<<"permission sucessfull change <<endl;
}
myfile.close();
cout << "thank you" << endl;
return 0;
}