ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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, ...
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.
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?
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.
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.
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.
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.
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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.