C functions, arguments, and ssh oh my!

Started by Joel Dudleyabout 25 years ago4 messagesgeneral
Jump to latest
#1Joel Dudley
Joel.Dudley@DevelopOnline.com

Hello All,
I am writing my first C function for postgres and failing miserably. my C
function needs to get passed a username (char) , uid(int), and gid(int) and
what it does with those is builds a command string for an external app that
is called with system() and exits 0. I know this is strange and ugly but I
need to trigger this external app when an insert is made into a user table.
I am looking into the version 1 calling convention to get these arguments
into my C function but I can't seem to match the types correctly. I am
looking at SPI with triggers now but I don't know if that will work either.
Am I barking up the wrong tree here? any comments are appreciated and I
appreciate the time you have taken to read my post. Below is to cludgy poor
code I have so far (it doesnt work of course) and I am halfway between
converting to version 1 calling but I wanted to give you an idea of what I
am trying to do. Thanks again.

- Joel

#include <stdlib.h>
#include "postgres.h"
#include "fmgr.h"

/*int *ssh_exec(char *uname[FILENAME_MAX], char *uid[FILENAME_MAX], char
*gid[FILENAME_MAX])*/

PG_FUNCTION_INFO_V1(ssh_exec);

Datum
ssh_exec(PG_FUNCTION_ARGS)
{
char sshcmd[255];

strncpy(sshcmd, "/usr/local/bin/plsshexec ", 255);
strncat(sshcmd, *uname, 255);
strncat(sshcmd, " ", 255);
strncat(sshcmd, *uid, 255);
strncat(sshcmd, " ", 255);
strncat(sshcmd, *gid, 255);
system(sshcmd);
return 0;
}

#2Alfred Perlstein
bright@wintelcom.net
In reply to: Joel Dudley (#1)
Re: C functions, arguments, and ssh oh my!

* Joel Dudley <Joel.Dudley@DevelopOnline.com> [010327 11:29] wrote:

Hello All,
I am writing my first C function for postgres and failing miserably. my C
function needs to get passed a username (char) , uid(int), and gid(int) and

right, wrong and wrong.

char *, uid_t, gid_t.

--
-Alfred Perlstein - [bright@wintelcom.net|alfred@freebsd.org]
Instead of asking why a piece of software is using "1970s technology,"
start asking why software is ignoring 30 years of accumulated wisdom.

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Joel Dudley (#1)
Re: C functions, arguments, and ssh oh my!

Joel Dudley <Joel.Dudley@DevelopOnline.com> writes:

what it does with those is builds a command string for an external app that
is called with system() and exits 0. I know this is strange and ugly but I
need to trigger this external app when an insert is made into a user table.

This seems an extremely dubious practice. If the transaction doing the
insert is later rolled back, the insert effectively never happened ---
but the effects of your external app will still be there. I'd suggest
thinking twice about your whole system design, if it requires this.

You can mitigate the problem a little bit by making the trigger an
AFTER trigger, so that it's only fired when we are about to commit the
transaction. But there's still a possibility of trouble if a later
AFTER trigger decides to abort.

regards, tom lane

#4Peter Eisentraut
peter_e@gmx.net
In reply to: Joel Dudley (#1)
Re: C functions, arguments, and ssh oh my!

Joel Dudley writes:

PG_FUNCTION_INFO_V1(ssh_exec);

Datum
ssh_exec(PG_FUNCTION_ARGS)
{
char sshcmd[255];

strncpy(sshcmd, "/usr/local/bin/plsshexec ", 255);
strncat(sshcmd, *uname, 255);
strncat(sshcmd, " ", 255);
strncat(sshcmd, *uid, 255);
strncat(sshcmd, " ", 255);
strncat(sshcmd, *gid, 255);
system(sshcmd);
return 0;
}

You missed the part about fetching the arguments using PG_GETARG_xxx.
Also you should use PG_RETURN_xxx and you need to null-terminate your
string.

--
Peter Eisentraut peter_e@gmx.net http://yi.org/peter-e/