/src/qsnet/qshd.c

https://code.google.com/ · C · 183 lines · 86 code · 25 blank · 72 comment · 19 complexity · 7b42020175cb202f374217e5a3d262fa MD5 · raw file

  1. /*****************************************************************************\
  2. * $Id$
  3. *****************************************************************************
  4. * Copyright (C) 2001-2006 The Regents of the University of California.
  5. * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
  6. * Written by Jim Garlick <garlick@llnl.gov>.
  7. * UCRL-CODE-2003-005.
  8. *
  9. * This file is part of Pdsh, a parallel remote shell program.
  10. * For details, see <http://www.llnl.gov/linux/pdsh/>.
  11. *
  12. * Pdsh is free software; you can redistribute it and/or modify it under
  13. * the terms of the GNU General Public License as published by the Free
  14. * Software Foundation; either version 2 of the License, or (at your option)
  15. * any later version.
  16. *
  17. * Pdsh is distributed in the hope that it will be useful, but WITHOUT ANY
  18. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  19. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with Pdsh; if not, write to the Free Software Foundation, Inc.,
  24. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  25. \*****************************************************************************/
  26. /*
  27. * This code is based on the BSD rcmd.c with MT safety added, and the
  28. * interface changed. Original UC regents header included below.
  29. */
  30. /*-
  31. * Copyright (c) 1988, 1989 The Regents of the University of California.
  32. * All rights reserved.
  33. *
  34. * Redistribution and use in source and binary forms, with or without
  35. * modification, are permitted provided that the following conditions
  36. * are met:
  37. * 1. Redistributions of source code must retain the above copyright
  38. * notice, this list of conditions and the following disclaimer.
  39. * 2. Redistributions in binary form must reproduce the above copyright
  40. * notice, this list of conditions and the following disclaimer in the
  41. * documentation and/or other materials provided with the distribution.
  42. * 3. All advertising materials mentioning features or use of this software
  43. * must display the following acknowledgement:
  44. * This product includes software developed by the University of
  45. * California, Berkeley and its contributors.
  46. * 4. Neither the name of the University nor the names of its contributors
  47. * may be used to endorse or promote products derived from this software
  48. * without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  51. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  52. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  53. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  54. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  55. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  56. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  57. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  58. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  59. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  60. * SUCH DAMAGE.
  61. */
  62. /*
  63. * PAM modifications by Michael K. Johnson <johnsonm@redhat.com>
  64. */
  65. char copyright[] =
  66. "@(#) Copyright (c) 1988, 1989 The Regents of the University of California.\n"
  67. "All rights reserved.\n";
  68. /*
  69. * From: @(#)rshd.c 5.38 (Berkeley) 3/2/91
  70. */
  71. char rcsid[] = "$Id$";
  72. /*#include "../version.h"*/
  73. #if HAVE_CONFIG_H
  74. #include "config.h"
  75. #endif
  76. #if HAVE_UNISTD_H
  77. #include <unistd.h> /* rresvport */
  78. #endif
  79. #include <sys/socket.h> /* connect */
  80. #include <sys/types.h>
  81. #include <netinet/in.h> /* sockaddr_in, htons */
  82. #include <syslog.h> /* syslog */
  83. #include <stdio.h>
  84. #include <stdlib.h>
  85. #include <netdb.h>
  86. #include "src/common/xmalloc.h"
  87. #include "qshell.h"
  88. extern int paranoid;
  89. extern int sent_null;
  90. extern int allow_root_rhosts;
  91. #ifdef USE_PAM
  92. extern char *pam_errmsg;
  93. #endif
  94. static struct passwd *doauth(char *remuser, char *hostname, char *locuser) {
  95. struct passwd *pwd;
  96. if ((pwd= getpwnam_common(locuser)) == NULL)
  97. return NULL;
  98. #ifdef USE_PAM
  99. if (pamauth(pwd, "qshell", remuser, hostname, locuser) < 0) {
  100. syslog(LOG_INFO | LOG_AUTH, "PAM Authentication Failure\n");
  101. error("%s\n", pam_errmsg);
  102. return NULL;
  103. }
  104. #else
  105. if ((pwd->pw_uid == 0 && !allow_root_rhosts) ||
  106. (ruserok(hostname, pwd->pw_uid == 0, remuser, locuser) < 0)) {
  107. syslog(LOG_INFO | LOG_AUTH, "Authentication Failure\n");
  108. error("Permission Denied\n");
  109. return NULL;
  110. }
  111. #endif
  112. return pwd;
  113. }
  114. static void qshd_get_args(struct sockaddr_in *fp, struct qshell_args *args)
  115. {
  116. char remuser[16];
  117. char locuser[16];
  118. char cmdbuf[ARG_MAX + 1];
  119. if (args->port != 0) {
  120. int lport = IPPORT_RESERVED - 1;
  121. if ((args->sock = rresvport(&lport)) < 0) {
  122. syslog(LOG_ERR, "can't get stderr port: %m");
  123. exit(1);
  124. }
  125. if (args->port >= IPPORT_RESERVED) {
  126. syslog(LOG_ERR, "2nd port not reserved\n");
  127. exit(1);
  128. }
  129. fp->sin_port = htons(args->port);
  130. if (connect(args->sock, (struct sockaddr *) fp, sizeof(*fp)) < 0) {
  131. syslog(LOG_INFO, "connect second port: %m");
  132. exit(1);
  133. }
  134. }
  135. /* Get remote user name, local user name, and command */
  136. if (getstr(remuser, sizeof(remuser), "remuser") < 0)
  137. exit(1);
  138. if (getstr(locuser, sizeof(locuser), "locuser") < 0)
  139. exit(1);
  140. if (getstr(cmdbuf, sizeof(cmdbuf), "command") < 0)
  141. exit(1);
  142. if ((args->hostname = findhostname(fp)) == NULL) {
  143. error("Host Address Mismatch");
  144. exit(1);
  145. }
  146. if ((args->pwd = doauth(remuser, args->hostname, locuser)) == NULL)
  147. exit(1);
  148. args->remuser = Strdup(remuser);
  149. args->locuser = Strdup(remuser);
  150. args->cmdbuf = Strdup(cmdbuf);
  151. return;
  152. }
  153. int main(int argc, char *argv[]) {
  154. return qshell(argc, argv, &qshd_get_args, "qshd", 1);
  155. }
  156. /*
  157. * vi:tabstop=4 shiftwidth=4 expandtab
  158. */