1
\$\begingroup\$

I have written C++ code which will say whether write permission is enable on the server or not using the smbclient command.

Since the smbclient command is not returning a proper error code if the remote server is read-only, my logic is that I try to write in the remote server if successful, then I display write permission is enabled and if failure, it says disabled.

After writing, I deleted it back. Please help me do it better if possible.

    #include<iostream>
    #include<stdlib.h>
    #include<fstream>
    #define SMBCLIENT "smbclient "
    #define TEMPFILE "tempfile"
    #define search  "putting file"
    enum errcode
    {
    EMPTY,
    internalError

    }errCode;

    void getUserCreditentail(std::string &username,std::string &password,std::string &server,std::string &folder,std::string &subfolder)
    {
            std::cin>>server;
            std::cin>>folder;
            std::cin>>subfolder;
            std::cin>>username;
            std::cin>>password;

    }

    void MakeCommand(std::string &command,const std::string server,const std::string folder,const std::string subfolder,const std::string username,const std::string password)
    {
            if(server.empty() ||folder.empty() || username.empty() ||  password.empty())
            {
                    errCode=EMPTY;
                    return;
            }

            command= SMBCLIENT +server +"/"+ folder + " " + "-U"+ " " + username + "%" + password + " " + "--directory=" + subfolder + " " + "-c" + " " + "'put  testfile.txt'" + " " + ">" + TEMPFILE;
    }

    bool executeCommand(const std::string command)
    {

    if(!command.empty())
            {
                    if(system(command.c_str())<0)
                    {
                            std::cout<<"system call fails"<<std::endl;
                            errCode=internalError;
                            return false;
                    }

            }

    return true;


    }

    bool checkWritePermission()
    {

            std::ifstream fin(TEMPFILE);
            std::string line;

            if (fin.is_open())
            {
                    std::string line;

                    while(getline(fin,line))
                    {
                            if (line.find(search, 0) != std::string::npos)
                            {
                                    std::cout << "found: " << search << std::endl;
                                    return true;
                            }

                    }

            }

            return false;
    }


    int main()
    {
            std::string command;
            std::string username,password;
            std::string server,folder,subfolder;
            getUserCreditentail(username,password,server,folder,subfolder);
            std::cout<<server<<" "<<folder<<" "<<username<<" "<<password<<""<<subfolder<<std::endl;
            MakeCommand(command,server,folder,subfolder,username,password);
            std::cout<<command<<std::endl;
            if(executeCommand(command))
            {
                    if(checkWritePermission())
                      {
                            std::cout<<"Write permission is enable"
<<std::endl;
                            // removefile(); /*this will remove written file*/
}                    else
                            std::cout<<"write permission is disabled"<<std::endl;

            }

            return 0;
    }
\$\endgroup\$
  • \$\begingroup\$ What's the intended behaviour if one of the user inputs is invalid? \$\endgroup\$ – Mast Sep 23 '17 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.