Question

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

  1. Check to see if the output file exists. If it doesn't, create it. Life is simpler if a newly created file is closed at the end of this step.
  2. Check the permissions of the output file. If any permissions exist, print a useful error message and exit.
  3. Change the permissions on the file to allow writing by the user.
  4. Open the file for output. If the "-c" command line option is present, make sure the file is truncated.
  5. Write the message from the command line to the output file. Write an additional newline character so that the output has a nicer format.
  6. Clear the permissions and close the file. (These two operations can be performed in either order, but the implementation is slightly different.)

Useful Hints

  • Don't use the command line arguments directly. Their position in the argument list may change depending on options. Create meaningful char* variables and fill these with the appropriate entries from the argument list.
  • Check for error values after nearly every system function call. It's a pain to set up, but it saves time in the long run.

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

rm log % /2123456 Usage: seclog [-c] out_file message_atring where the message_string is appended to file out_file The -c optail log Hello Hello Wait, theres more % chmod 000 log % ·/z123456 -c 10g Clean 3tart % 13-1 total 72 1 z123456 student 12

rm log % "/2123456 Usage: seclog [-c] out_file message_atring where the message_string is appended to file out_file The -c option clears the file before the message is appended chmod u-w . % “/2123456 log "Hello" Permission denied chmod utw. ·/2123456 log "Hello" total 72 ---1 z123456 student rwxr-xr-x 1 z123456 student 26385 Sep 24 18:38 z123456 -rw-r--r1 z123456 student 2204 Sep 24 18 36 z123456.cxx -rw-r--r-1 z123456 student 30896 Sep 24 18:38 z123456.0 % "/2123456 log "Hello" 6 Sep 24 18:39 log 13-1 total 72 12 Sep 24 18:40 log rwxr-xr-x 1 z123456 student 26385 Sep 24 18:38 z123456 -rw-r--r1 z123456 student 2204 Sep 24 18:36 z123456.cxx -rw-rr-- 1 z123456 student 30896 Sep 24 18:38 z123456.o chmod 400 log tail Hello He 1 1 log ·/2123456 log "wait, there's more" log is not secure. Ignoring. chmod 000 log * ./z123456 log "Wait, there's more" 13-1 total 72 1 z123456 student 31 Sep 24 18:41 1og 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--r1 z123456 student 30896 Sep 24 18 38 z123456.0 chmod 400 log tail log Hello Wait, there's more chmod 000 log
tail log Hello Hello Wait, there's more % chmod 000 log % ·/z123456 -c 10g "Clean 3tart" % 13-1 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 2123456 student 2204 Sep 24 18:36 z123456.cxx -rw-r--r1 z123456 student 30896 Sep 24 18:38 z123456.0 % chmod 400 log % tail log Ciean start % chmod 000 log
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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;
}

//chmod(argv[1], S_IRWXU);
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 p@ubuntu: /Desktop t )1:34 PM P@ubuntu:/Desktops g++ nain.cpp P@ubuntu: /Desktops ./a.out sample.txt Hello file has permissio 0000"<<endl;
}
myfile.close();
cout << "thank you" << endl;
return 0;
}

p@ubuntu: /Desktop t )1:34 PM P@ubuntu:/Desktops g++ nain.cpp P@ubuntu: /Desktops ./a.out sample.txt Hello file has permissio

Add a comment
Know the answer?
Add Answer to:
Purpose This assignment should give you experience in using file descriptors, open(), close(), wr...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello, my name is Shady Slim. I understand you are going to help me figure out...

    Hello, my name is Shady Slim. I understand you are going to help me figure out my gross income for the year... whatever that means. It's been a busy year and I'm a busy man, so let me give you the lowdown on my life and you can do your thing I was unemployed at the beginning of the year and got $2,000 in unemployment compensation. I later got a job as a manager for Roca Cola. I earned $57,500...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT