Question

In: Computer Science

Purpose This assignment should give you experience in using file descriptors, open(), close(), write(), stat() and...

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

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
%

Solutions

Expert Solution

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


Related Solutions

In c++  write a program. Give the necessary statements to open a file and to confirm that...
In c++  write a program. Give the necessary statements to open a file and to confirm that the file has been successfully opened for writing.
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this...
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this program is JAVA Part One A hotel salesperson enters sales in a text file. Each line contains the following, separated by semicolons: The name of the client, the service sold (such as Dinner, Conference, Lodging, and so on), the amount of the sale, and the date of that event. Prompt the user for data to write the file. Part Two Write a program that...
Purpose The purpose of this assignment is to give you an opportunity to demonstrate your ability...
Purpose The purpose of this assignment is to give you an opportunity to demonstrate your ability to identify emerging ethical issues in business, interpret the multitude of perspectives inherent in your case study, and model appropriate behaviour by recommending specific solutions. How to Proceed Select a case. It can be one of the textbook cases that we have not discussed during the course. It can also come from the outside world, perhaps a case you have been following in the...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
_____________________________________ will close the open purchase order file. A purchasing system that employs _________________________________________ does not...
_____________________________________ will close the open purchase order file. A purchasing system that employs _________________________________________ does not use a purchase order. The open payable file is organized by ____________________________ to ensure prompt payments Automation leads to collapsing of traditional segregation of duties especially when computer programs ________________________ and _________________________ purchase orders. ____________________________can compensate for the lack of segregation of duties.
Project 3 Details The purpose of this project is to give you experience with creating and...
Project 3 Details The purpose of this project is to give you experience with creating and using custom objects. In it, you will make a couple custom classes that work in tandem with each other, and call them in your main function. For this project we'll be making something kind of like the Chatbot java file we made in class. You will create a Chatbot class that will contain a number of variables and functions. As you can imagine, a...
Purpose of Assignment The purpose of the assignment is to develop students' abilities in using data...
Purpose of Assignment The purpose of the assignment is to develop students' abilities in using data sets to apply the concepts of sampling distributions and confidence intervals to make management decisions. Assignment Steps Resources: Microsoft Excel®, The Payment Time Case Study, The Payment Time Case Data Set Review the Payment Time Case Study and Data Set. Develop a 700-word report including the following calculations and using the information to determine whether the new billing system has reduced the mean bill...
In this assignment you will write a program that encrypts a text file.  You will use the...
In this assignment you will write a program that encrypts a text file.  You will use the following encryption scheme. Ask the user for the name of the original file. Ask the user for the name of the output file. Ask the user for the encryption key, n. Read n2 characters from the file into the n rows and n columns of a 2-dimensional array. Transpose the array.  (Exchange the rows and columns.) Write the characters from the array to an output...
CVP Modeling project The purpose of this project is to give you experience creating a profitability...
CVP Modeling project The purpose of this project is to give you experience creating a profitability analysis that can be used to determine the effects of changing business conditions on a client's financial position. Managers often want to know the production level where profits earned from a product cover the cost of resources used to create it. Break-even analysis is how we determine this level. The point at which total sales revenues covers the costs of committed resources is called...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT