summaryrefslogtreecommitdiffstats
path: root/qarsh/qarsh.c
diff options
context:
space:
mode:
Diffstat (limited to 'qarsh/qarsh.c')
-rw-r--r--qarsh/qarsh.c56
1 files changed, 54 insertions, 2 deletions
diff --git a/qarsh/qarsh.c b/qarsh/qarsh.c
index 20e7023..0b91f5d 100644
--- a/qarsh/qarsh.c
+++ b/qarsh/qarsh.c
@@ -18,6 +18,7 @@
#include <netinet/in.h>
#include <netdb.h>
#include <syslog.h>
+#include <pwd.h>
#include "sockutil.h"
@@ -31,7 +32,7 @@ int qarsh_fd = -1; /* The control connection to qarshd */
void
usage()
{
- printf("Usage: qarsh hostname cmdline ...\n");
+ printf("qarsh: [user[.group]@]hostname cmdline ...\n");
}
char *
@@ -58,6 +59,25 @@ copyargs(char **argv)
return args;
}
+void
+set_remote_user(char *user, char *group)
+{
+ struct qa_packet *qp;
+
+ qp = make_qp_setuser(user, group);
+ qp->qp_seq = 1;
+ send_packet(qarsh_fd, qp);
+ qpfree(qp);
+ qp = recv_packet(qarsh_fd);
+ if (qp && qp->qp_type == QP_RETURNCODE
+ && qp->qp_returncode.qp_rc == -1) {
+
+ fprintf(stderr, "Remote side failed, %s\n",
+ qp->qp_returncode.qp_strerror);
+ close(qarsh_fd);
+ exit(125);
+ }
+}
int
run_remote_cmd(char *cmdline)
@@ -219,13 +239,22 @@ main(int argc, char *argv[])
int c;
int port = 5008;
char *host;
+ char *remuser = NULL;
+ char *remgroup = NULL;
char *args;
+ struct passwd *pw;
int ret;
openlog("qarsh", LOG_PID, LOG_DAEMON);
- while ((c = getopt(argc, argv, "+p:")) != -1) {
+ while ((c = getopt(argc, argv, "+p:l:g:")) != -1) {
switch (c) {
+ case 'l':
+ remuser = strdup(optarg);
+ break;
+ case 'g':
+ remgroup = strdup(optarg);
+ break;
case 'p':
port = atoi(optarg);
break;
@@ -240,6 +269,27 @@ main(int argc, char *argv[])
usage();
exit(1);
}
+ /* check for user and group in form [user[.group]@]hostname */
+ {
+ char *sp;
+
+ if ((sp = strchr(host, '@'))) {
+ remuser = host;
+ host = sp+1;
+ *sp = '\0';
+ }
+ if (remuser && (sp = strchr(remuser, '.'))) {
+ remgroup = sp+1;
+ *sp = '\0';
+ }
+ }
+ if (!(pw = getpwuid(getuid()))) {
+ fprintf(stderr, "qarsh: unknown user id.\n");
+ exit(1);
+ }
+ if (remuser == NULL) {
+ remuser = strdup(pw->pw_name);
+ }
argc -= optind;
argv += optind;
@@ -259,6 +309,8 @@ main(int argc, char *argv[])
return 127;
}
+ set_remote_user(remuser, remgroup);
+
ret = run_remote_cmd(args);
close(qarsh_fd);
free(args);