LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-13-2015, 09:53 AM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Rep: Reputation: 60
Storing a Temporary File Using C


How would someone save a file such as /etc/vpnc/test.conf locally into a temp file, so it can be queried? So for example if I used rsync to copy this file locally, how would I add that to a temp_file variable and discard it after I am done?
Code:
   #include <stdio.h>
    #include "error.h"
    #include "string.h"
    #define BUFLEN 256
    char buffer[BUFLEN];
    int main() {
    FILE *fp;
    char *rsync;
    char *target
    char tmp_file[BUFLEN];
                command=malloc(2*strlen(rsync)+strlen(target)+24+strlen(tmp_file));
                sprintf(command, "%s %s://%s/etc/vpnc/test.conf %s", rsync, rsync, target, tmp_file);1
            }
            if ... 
         }
     fclose(fp);
     unlink(tmp_file);
   }
I truncate the code to get to the point as you can tell I am very new to C and wanted to get a better understanding of it. How would I initialize tmp_file so I can query it locally and then discard the file using unlink. I looked at this but is rather confusing for something so simple.

http://www.thegeekstuff.com/2012/06/c-temporary-files/

Essentially in a nutshell this is what I am trying to achieve:

1 - Temporary download /etc/vpnc/test.conf to tmp_file
2 - Search for a line in tmp_file
3 - print out the results of my findings
4 - Delete tmp_file

As always, many thanks for your help

Last edited by metallica1973; 01-13-2015 at 10:26 AM.
 
Old 01-13-2015, 10:09 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
char *nameptr= NULL;
asprintf (&nameptr, "/some/directory/file.%lld", (long long)pid ());
...
free (nameptr);
nameptr= NULL;

Last edited by NevemTeve; 01-13-2015 at 10:15 AM.
 
1 members found this post helpful.
Old 01-13-2015, 10:28 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Notes:
1. You don't have to copy (or "download") a file, if you only want to search in it.
2. There are some programs that are quite good in file-searching, they are: fgrep, grep, egprep, awk, ...
 
1 members found this post helpful.
Old 01-13-2015, 10:56 AM   #4
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
NevemTeve,

Thank you for your help. Unfortunately, I am forced to access the file in the above manner using rsync because of the nature of project. I dont have that luxury.
 
Old 01-13-2015, 12:19 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Then you forgot to bring up some little detail (eg: the original file exists on a remote computer), or perhaps misunderstood something. Or is this a homework assignment?
 
Old 01-13-2015, 01:03 PM   #6
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
Thanks for the replies. Its actually part of my job working in vulnerability research. We currently have code that is in place written in C where I have to add my logic in obtaining proof of concept that this vulnerability exist by:

1 - Downloading this file (/etc/vpnc/test.conf) from a remote machine to a temp file(locally)
2 - Searching for something juicy to display showing the proof is in the pudding.
ex. /etc/vpnc/test.conf
Code:
IPSec ID TestID
IPSec gateway blah.blah.com
IPSec secret 123456
Xauth username blah_user
Xauth password 123456!
IKE Authmode psk
#IKE DH Group dh2
#NAT Traversal Mode cisco-udp
Local Port 10000
DPD idle timeout (our side) 0
3 - Simply printing out my findings to stdout using already custom functions already in place. Someone else had suggested using tmpnam and or mkstemp functions
Code:
#include <stdio.h>

int main() {
        char buf[4096];
        char *n=tmpnam(NULL); // Use this as the file name, not tmp_file.

// get the file
... 

        FILE *fp=fopen(n, "r");
        unlink(n);
        // Read a line into 'buf' until we run out of lines
        while(fgets(buf, 4096, fp))
        {
                // Check the contents of 'buf' for what you want
        }
}
but my lack of knowing C has slowed me down. So looking at the code above, how do I get the results of:
Code:
rsync rsync://192.168.3.1/vpnc/test.conf
into the "n" temp file? Forgive me for my stupidity.I promise I will grab a C book to stop nagging the forum

??

Last edited by metallica1973; 01-13-2015 at 01:07 PM.
 
Old 01-13-2015, 02:12 PM   #7
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Quote:
Originally Posted by metallica1973 View Post
How would someone save a file such as /etc/vpnc/test.conf locally into a temp file, so it can be queried? So for example if I used rsync to copy this file locally, how would I add that to a temp_file variable and discard it after I am done?

Essentially in a nutshell this is what I am trying to achieve:

1 - Temporary download /etc/vpnc/test.conf to tmp_file
2 - Search for a line in tmp_file
3 - print out the results of my findings
4 - Delete tmp_file

As always, many thanks for your help
I think you need to clarify your process.

What do you mean by download? Are you downloading the file externally from the C program? If not then why create a file at all? Read from the socket into a buffer then search the buffer. Delete the buffer when you're complete.
 
Old 01-13-2015, 03:50 PM   #8
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
SoftSpocket, thank you for your reply. For clarification:

1 - Temporary download /etc/vpnc/test.conf to a tmp_file from the target remote machine. So what I will be doing is locally from my laptop is using rsync to remotely downloading this file via:
Code:
rsync rsync://192.168.3.1/vpnc/test.conf .
So in my head, I want the "." to be the copy of "test.conf" of the remote machine(192.168.3.1) so that I can gather my data from it.

2 - Once I have the test.conf copied remotely to my "n"(temp file), I want to search for something meaningful

3 - Once I have what I want simply print out the results of my findings

4 - Lastly deleting "n" the temp file. I guess this is where unlink comes to play

Last edited by metallica1973; 01-13-2015 at 03:51 PM.
 
Old 01-13-2015, 04:45 PM   #9
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Quote:
Originally Posted by metallica1973 View Post
SoftSpocket, thank you for your reply. For clarification:

1 - Temporary download /etc/vpnc/test.conf to a tmp_file from the target remote machine. So what I will be doing is locally from my laptop is using rsync to remotely downloading this file via:
Code:
rsync rsync://192.168.3.1/vpnc/test.conf .
So in my head, I want the "." to be the copy of "test.conf" of the remote machine(192.168.3.1) so that I can gather my data from it.

2 - Once I have the test.conf copied remotely to my "n"(temp file), I want to search for something meaningful

3 - Once I have what I want simply print out the results of my findings

4 - Lastly deleting "n" the temp file. I guess this is where unlink comes to play
You want to use rsync from in side the program? Either way you aren't really creating a temp file - rsync the file and use the remove (or unlink) function after your done reading it.
 
2 members found this post helpful.
Old 01-14-2015, 12:58 AM   #10
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Quote:
Originally Posted by metallica1973 View Post
SoftSpocket, thank you for your reply. For clarification:

1 - Temporary download /etc/vpnc/test.conf to a tmp_file from the target remote machine. So what I will be doing is locally from my laptop is using rsync to remotely downloading this file via:
Code:
rsync rsync://192.168.3.1/vpnc/test.conf .
So in my head, I want the "." to be the copy of "test.conf" of the remote machine(192.168.3.1) so that I can gather my data from it.

2 - Once I have the test.conf copied remotely to my "n"(temp file), I want to search for something meaningful

3 - Once I have what I want simply print out the results of my findings

4 - Lastly deleting "n" the temp file. I guess this is where unlink comes to play
may I ask, why do you use C for this task ?
rsync the file , grep in the file , rm the file,
I love C/C++, but I for this task it makes no point to not use a shell script
 
Old 01-14-2015, 08:52 AM   #11
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by a4z View Post
may I ask, why do you use C for this task ?
rsync the file , grep in the file , rm the file,
I love C/C++, but I for this task it makes no point to not use a shell script
I was getting there with my thought process.

Seems weird to require an rsync over simple cp, but I get that considering the possibility of remote accessing the file. You can save a file in the /tmp directory structure too, which does take RAM but will not be saved over a reboot. As far as removing a file from within a program, the remove() function will do that for you. There just seem better ways to skin the cat versus writing a program here.

Last edited by rtmistler; 01-14-2015 at 10:49 AM.
 
1 members found this post helpful.
Old 01-14-2015, 10:19 AM   #12
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
Thanks for all the help. Because the existing code is already in place, I am stuck going about it using C. Once I iron this out, I will post my results so the average joe like myself will at least have a direction to go in.
 
Old 01-14-2015, 10:54 AM   #13
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by metallica1973 View Post
Thanks for all the help. Because the existing code is already in place, I am stuck going about it using C. Once I iron this out, I will post my results so the average joe like myself will at least have a direction to go in.
I certainly would similarly go down a rat hole due to technical restraints, or an existing architecture in place; however in hitting several road blocks much like you're seeing here, I'd go out of the box using a few different means to see if I could solve "better, easier, more correctly", and so forth.

And just now noticing that you've (a) been around the forums for quite a long time, and (b) have been active enough to have posted questions and answers, so I'll have to give you the benefit of the doubt that you understand where you're stuck and this isn't your first choice.

Also however (1) you're not an average Joe you probably have better than average experience and (2) you probably do know better than to allow your self to be stuck and limited in solution form especially if it's not working.

So in conclusion, you've likely done this before too; you're stuck, you've been stuck. OK there's an architecture or set of rules you have to stay within, but you do know better, and the investment in a quick alternative solution may help both in logical organization as well as give you some fuel to assert yourself to a team, or boss and explain that the restraints can be lifted, or if you feel that you've invested so much in a C program, perhaps the act of stepping back and using an alternate solution will also re-set your way of thinking where you may or may not allow your self to be blocked by your technical choice of methods/direction.

Last edited by rtmistler; 01-14-2015 at 10:56 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating a temporary file name in C benav Programming 8 08-14-2011 01:00 PM
temporary cifs file kellemes Linux - Networking 2 07-03-2009 07:00 AM
copy last 10000+ lines of large text file to a temporary file emilyg Linux - Newbie 3 06-24-2009 02:43 PM
Installing a temporary(?) file natedogver31 Linux - Newbie 14 04-02-2004 05:05 PM
file for storing routing entry becky_starr Linux - Networking 1 03-15-2004 09:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration