File: suidconnect.c

package info (click to toggle)
sfs 1%3A0.8-0%2Bpre20060720.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 9,668 kB
  • ctags: 14,317
  • sloc: cpp: 78,358; ansic: 15,494; sh: 9,540; yacc: 786; makefile: 706; perl: 676; lex: 553; python: 146; sed: 70
file content (144 lines) | stat: -rw-r--r-- 3,489 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* $Id: suidconnect.c,v 1.10 2006/01/21 23:55:48 dm Exp $ */

/*
 *
 * Copyright (C) 1998, 1999 David Mazieres (dm@uun.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 */

#include "sfs-internal.h"
#include "sfsagent.h"

int suidprotect = 1;

int opt_quiet;

static int
sendfd (int fd, int wfd)
{
  char c = '\175';
  return writefd (fd, &c, 1, wfd);
}

static void
usage (void)
{
  fprintf (stderr, "usage: suidconnect [-q] program\n");
  exit (1);
}

static bool_t
checkprog (const char *prog)
{
  if (!*prog)
    return FALSE;
  for (; *prog; prog++)
    if (!isalnum (*prog) && *prog != '_')
      return FALSE;
  return TRUE;
}

static bool_t
getaddr (struct sockaddr_un *sun, const char *prog)
{
  static const char *runinplace = "";
  static const char suffix[] = ".sock";
  const char *sockdir;
#ifdef DMALLOC
  if (!suidsafe ()) {
    xputenv ("DMALLOC_LOGFILE=/dev/null");
    xputenv ("DMALLOC_OPTIONS=");
    dmalloc_debug (0);
  }
#endif /* DMALLOC */
  sockdir = getsfssockdir ();

  if (!checkprog (prog)
      || (strlen (sockdir) + 1 + strlen (runinplace)
	  + strlen (prog) + sizeof (suffix) > sizeof (sun->sun_path)))
    return FALSE;
  strcpy (sun->sun_path, sockdir);
  strcat (sun->sun_path, runinplace);
  strcat (sun->sun_path, "/");
  strcat (sun->sun_path, prog);
  strcat (sun->sun_path, suffix);
  sun->sun_family = AF_UNIX;
  return TRUE;
}

int
main (int argc, char **argv)
{
  struct sockaddr_un sun;
  int fd;
  AUTH *auth;
  int res;
  enum clnt_stat err;
  int argn;

  for (argn = 1; argn < argc && argv[argn][0] == '-'; argn++) {
    if (!strcmp (argv[argn], "-q"))
      opt_quiet = 1;
    else
      usage ();
  }

  if (argn + 1 != argc || !getaddr (&sun, argv[argn]))
    usage ();

  if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0
      || connect (fd, (struct sockaddr *) &sun, sizeof (sun)) < 0) {
    if (!opt_quiet)
      perror (sun.sun_path);
    exit (1);
  }
  
  auth = authunix_create_realids ();
  if (!auth) {
    fprintf (stderr, "could not create auth_unix structure\n");
    exit (1);
  }

  err = srpc_callraw (fd, SETUID_PROG, SETUID_VERS, SETUIDPROC_SETUID,
		      (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_int, &res,
		      auth);
  if (err)
    fprintf (stderr, "%s: %s\n", argv[1], clnt_sperrno (err));
  else if (res)
    fprintf (stderr, "%s: %s\n", argv[1], strerror (res));
  else if (sendfd (0, fd) >= 0) {
#ifdef __APPLE__
    /* Work around garbage-collection bug in MacOS where passed socket
     * becomes invalid if we exit too soon. */
    if (fd < FD_SETSIZE) {
      fd_set fds;
      struct timeval tv;

      FD_ZERO (&fds);
      FD_SET (fd, &fds);
      tv.tv_sec = 60;
      tv.tv_usec = 0;
      select (fd + 1, &fds, NULL, NULL, &tv);
    }
#endif /* __APPLE__ */
    exit (0);
  }
  else
    perror ("suidconnect: sendfd");
  exit (1);
}