00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <unistd.h>
00023 #include <stdlib.h>
00024 #include <syslog.h>
00025 #include <errno.h>
00026 #include <pwd.h>
00027 #include <string>
00028 #include <sys/types.h>
00029 #include <sys/socket.h>
00030 #include <sys/un.h>
00031 #include <sys/stat.h>
00032 #include <netinet/in.h>
00033 #include "Varargs.h"
00034 #include "Rtypes.h"
00035 #include <string.h>
00036 #ifdef R__GLOBALSTL
00037 namespace std { using ::string; }
00038 #endif
00039
00040 int gDebug;
00041
00042 #define kMAXPATHLEN 4096
00043
00044
00045 void Info(const char *va_(fmt), ...)
00046 {
00047
00048
00049 char buf[kMAXPATHLEN];
00050 va_list ap;
00051
00052 va_start(ap,va_(fmt));
00053 vsnprintf(buf, sizeof(buf), fmt, ap);
00054 va_end(ap);
00055
00056 #if 0
00057
00058 #else
00059 fprintf(stderr, "%s\n",buf);
00060 #endif
00061
00062 }
00063
00064
00065 int main(int argc, char **argv)
00066 {
00067
00068
00069
00070 char *pipeId = 0;
00071 char *tmpDir = 0;
00072
00073 if (argc < 3) {
00074 Info("ssh2rpd: argc=%d"
00075 ": at least one additional argument required - exit",
00076 argc);
00077 exit(1);
00078 }
00079
00080
00081 gDebug = atoi(argv[1]);
00082 if (argc > 2)
00083 pipeId = strdup(argv[2]);
00084 if (argc > 3)
00085 tmpDir = strdup(argv[3]);
00086
00087 if (gDebug > 0) {
00088 std::string tmp = std::string("ssh2rpd: forked with args:");
00089 int i = 1;
00090 for( ; i < argc; i++) {
00091 tmp.append(" ");
00092 tmp.append(argv[i]);
00093 }
00094 Info("%s",tmp.c_str());
00095 }
00096
00097
00098 struct passwd *pw = getpwuid(getuid());
00099 if (!pw) {
00100 Info("ssh2rpd: user name not found");
00101 exit(1);
00102 }
00103
00104 char pipeFile[kMAXPATHLEN];
00105 if (!tmpDir)
00106 snprintf(pipeFile,kMAXPATHLEN, "%s/RootSshPipe.%s", pw->pw_dir, pipeId);
00107 else
00108 snprintf(pipeFile,kMAXPATHLEN,"%s/RootSshPipe.%s", tmpDir, pipeId);
00109
00110 FILE *fpipe = fopen(pipeFile, "r");
00111 char pipe[kMAXPATHLEN];
00112 if (fpipe) {
00113 while (fgets(pipe, sizeof(pipe), fpipe)) {
00114 if (pipe[strlen(pipe)-1] == '\n')
00115 pipe[strlen(pipe)-1] = 0;
00116 }
00117 fclose(fpipe);
00118
00119 unlink(pipeFile);
00120 } else {
00121 Info("ssh2rpd: cannot open file with pipe info (%s): exiting"
00122 " (errno= %d)",pipeFile,errno);
00123 exit(1);
00124 }
00125
00126
00127 struct sockaddr_un servAddr;
00128 servAddr.sun_family = AF_UNIX;
00129 strlcpy(servAddr.sun_path, pipe, sizeof(servAddr.sun_path));
00130 int sd;
00131 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
00132 Info("ssh2rpd: cannot open socket: exiting ");
00133 exit(1);
00134 }
00135
00136 int rc;
00137 if ((rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))) < 0) {
00138 Info("ssh2rpd: cannot connect socket: exiting (errno= %d)",errno);
00139 exit(1);
00140 }
00141
00142
00143 char okbuf[256];
00144 snprintf(okbuf, sizeof(okbuf), "OK %s", pw->pw_name);
00145 int lbuf = strlen(okbuf);
00146 lbuf = htonl(lbuf);
00147 rc = send(sd, &lbuf, sizeof(lbuf), 0);
00148 if (rc != (int)sizeof(lbuf)) {
00149 Info("ssh2rpd: sending might have been unsuccessful (bytes send: %d)",rc);
00150 }
00151 rc = send(sd, okbuf, strlen(okbuf), 0);
00152 if (rc != (int)strlen(okbuf)) {
00153 Info("ssh2rpd: sending might have been unsuccessful (bytes send: %d)",rc);
00154 }
00155
00156 if (tmpDir) free(tmpDir);
00157
00158 exit(0);
00159 }