summaryrefslogtreecommitdiffstats
path: root/src/process.c
blob: 585123c4612da3ec84ac253aad4bdefc4bf922e0 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282

#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/signal.h>
#include <sys/poll.h>
#include <sys/stat.h>
#include <sys/wait.h>

#include <popt.h>

#include <regex.h>

#include "initlog.h"
#include "process.h"

extern regex_t **regList;

int forkCommand(char **args, int *outfd, int *errfd, int *cmdfd, int quiet) {
   /* Fork command 'cmd', returning pid, and optionally pointer
    * to open file descriptor fd */
    int fdout, fderr, fdcmd, pid;
    int outpipe[2], errpipe[2], fdpipe[2];
    int ourpid;
    
    if ( (pipe(outpipe)==-1) || (pipe(errpipe)==-1) || (pipe(fdpipe)==-1) ) {
	perror("pipe");
	return -1;
    }
    
    if (outfd) {
       fdout = outpipe[1];
      *outfd = outpipe[0];
    } else {
       if (!quiet)
	 fdout=dup(1);
    }
    if (errfd) {
       fderr = errpipe[1];
      *errfd = errpipe[0];
    } else {
       if (!quiet)
	 fderr=dup(2);
    }
    
    if (cmdfd) {
	*cmdfd = fdpipe[0];
	fdcmd = fdpipe[1];
    } else {
        fdcmd = open("/dev/null",O_WRONLY);
    }
    ourpid = getpid();
    if ((pid = fork())==-1) {
	perror("fork");
	return -1;
    }
    /* We exec the command normally as the child. However, if we're getting passed
     * back arguments via an fd, we'll exec it as the parent. Therefore, if Bill
     * fucks up and we segfault or something, we don't kill rc.sysinit. */
    if ( (cmdfd&&!pid) || (pid &&!cmdfd)) {
	/* parent */
	close(fdout);
	close(fderr);
	close(fdcmd);
	if (!pid)
	  return ourpid;
	else
	  return pid;
    } else {
	/* kid */
       int sc_open_max;

       if (outfd) { 
	 if ( (dup2(fdout,1)==-1) ) {
	    perror("dup2");
	    exit(-1);
	 }
       } else if (quiet)
	    if ((dup2(open("/dev/null",O_WRONLY),1))==-1) {
	     perror("dup2");
	     exit(-1);
	    }

       if (errfd)  {
	 if ((dup2(fderr,2)==-1)) {
	    perror("dup2");
	    exit(-1);
	 }
       } else if (quiet) 
	    if ((dup2(open("/dev/null",O_WRONLY),2))==-1)  {
	       perror("dup2");
	       exit(-1);
	    }

 
       if ((dup2(fdcmd,CMD_FD)==-1)) {
	    perror("dup2");
	    exit(-1);
	}
	close(fdout);
	close(fderr);
	close(fdcmd);
	if (outfd)
	  close(*outfd);
	if (errfd)
	  close(*errfd);
	if (cmdfd)
	  close(*cmdfd);

        /* close up extra fds, and hope this doesn't break anything */
	sc_open_max = sysconf(_SC_OPEN_MAX);
	if(sc_open_max > 1) {
	    int fd;
	    for(fd = 3; fd < sc_open_max; fd++) {
		    if (!(cmdfd && fd == CMD_FD))
		      close(fd);
	    }
	}

	execvp(args[0],args);
	perror("execvp");
	exit(-1);
    }
}

int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet, int debug) {
    struct pollfd *pfds;
    char *buf=malloc(8192*sizeof(char));
    char *outbuf=NULL;
    char *tmpstr=NULL;
    int x,y,rc=-1;
    int done=0;
    int output=0;
    char **cmdargs=NULL;
    char **tmpargs=NULL;
    int cmdargc;
    char *procpath = NULL;
    
    if (reexec) {
	procpath=malloc(20*sizeof(char));
	snprintf(procpath,20,"/proc/%d",pid);
    }
    
    pfds = malloc(numfds*sizeof(struct pollfd));
    for (x=0;x<numfds;x++) {
	pfds[x].fd = fds[x];
	pfds[x].events = POLLIN | POLLPRI;
    }
	
    while (!done) {
       usleep(500);
       if (((x=poll(pfds,numfds,500))==-1)&&errno!=EINTR) {
	  perror("poll");
	  return -1;
       }
       if (!reexec) {
	  if (waitpid(pid,&rc,WNOHANG))
	    done=1;
       } else {
	   struct stat sbuf;
	   /* if /proc/pid ain't there and /proc is, it's dead... */
	   if (stat(procpath,&sbuf)&&!stat("/proc/cpuinfo",&sbuf))
	     done=1;
       }
       y=0;
       while (y<numfds) {
	  if ( x && ((pfds[y].revents & (POLLIN | POLLPRI)) )) {
	     int bytesread = 0;
	     
	     do {
		buf=calloc(8192,sizeof(char));
		bytesread = read(pfds[y].fd,buf,8192);
		if (bytesread==-1) {
		   perror("read");
		   return -1;
		}
		if (bytesread) {
		  if (!quiet && !reexec)
		    write(1,buf,bytesread);
		  if (quiet) {
			  outbuf=realloc(outbuf,(outbuf ? strlen(outbuf)+bytesread+1 : bytesread+1));
			  if (!output) outbuf[0]='\0';
			  strcat(outbuf,buf);
			  output = 1;
		  }
		  while ((tmpstr=getLine(&buf))) {
		      int ignore=0;
		      
		      if (regList) {
			  int count=0;
			 
			  while (regList[count]) {
			      if (!regexec(regList[count],tmpstr,0,NULL,0)) {
				  ignore=1;
				  break;
			      }
			      count++;
			  }
		      }
		      if (!ignore) {
			  if (!reexec) {
			      if (getenv("IN_INITLOG")) {
				  char *buffer=calloc(8192,sizeof(char));
				  DDEBUG("sending =%s= to initlog parent\n",tmpstr);
				  snprintf(buffer,8192,"-n %s -s \"%s\"\n",
					   cmdname,tmpstr);
				  /* don't blow up if parent isn't there */
				  signal(SIGPIPE,SIG_IGN);
				  write(CMD_FD,buffer,strlen(buffer));
				  signal(SIGPIPE,SIG_DFL);
				  free(buffer);
			      } else {
				  logString(cmdname,tmpstr);
			      }
			  } else {
			      int z; 
			
			      cmdargs=NULL;
			      tmpargs=NULL;
			      cmdargc=0;
			      
			      poptParseArgvString(tmpstr,&cmdargc,&tmpargs);
			      cmdargs=malloc( (cmdargc+2) * sizeof(char *) );
			      cmdargs[0]=strdup("initlog");
			      for (z=0;z<(cmdargc);z++) {
				  cmdargs[z+1]=tmpargs[z];
			      }
			      cmdargs[cmdargc+1]=NULL;
			      processArgs(cmdargc+1,cmdargs,1);
			  }
		      }
		  }
		}
	     } while ( bytesread==8192 );
	  }
	  y++;
       }
    }
    if ((!WIFEXITED(rc)) || (rc=WEXITSTATUS(rc))) {
      /* If there was an error and we're quiet, be loud */
      
      if (quiet && output) {
	    write(1,outbuf,strlen(outbuf));
      }
      return (rc);
   }
   return 0;
}

int runCommand(char *cmd, int reexec, int quiet, int debug) {
    int fds[2];
    int pid,x;
    char **args, **tmpargs;
    char *cmdname;
    
    poptParseArgvString(cmd,&x,&tmpargs);
    args = malloc((x+1)*sizeof(char *));
    for ( pid = 0; pid < x ; pid++) {
	args[pid] = strdup(tmpargs[pid]);
    }
    args[pid] = NULL;
    if (strcmp(args[0],"sh") && strcmp(args[0],"/bin/sh")) 
      cmdname = basename(args[0]);
    else
      cmdname = basename(args[1]);
    if ((cmdname[0] =='K' || cmdname[0] == 'S') && ( '0' <= cmdname[1] <= '9' )
       && ( '0' <= cmdname[2] <= '9' ) )
      cmdname+=3;
    if (!reexec) {
       pid=forkCommand(args,&fds[0],&fds[1],NULL,quiet);
       x=monitor(cmdname,pid,2,fds,reexec,quiet,debug);
    } else {
       setenv("IN_INITLOG","yes",1);
       pid=forkCommand(args,NULL,NULL,&fds[0],quiet);
       unsetenv("IN_INITLOG");
       x=monitor(cmdname,pid,1,&fds[0],reexec,quiet,debug);
    }
    return x;
}
an>"Out of memory?!\n")); talloc_zfree(conn); return ENOMEM; } DEBUG(4, ("Set-up proxy client ID timeout [%p]\n", proxy_cli->timeout)); /* Attach the client context to the connection context, so that it is * always available when we need to manage the connection. */ sbus_conn_set_private_data(conn, proxy_cli); return EOK; } static void init_timeout(struct tevent_context *ev, struct tevent_timer *te, struct timeval t, void *ptr) { struct proxy_client *proxy_cli; DEBUG(2, ("Client timed out before Identification [%p]!\n", te)); proxy_cli = talloc_get_type(ptr, struct proxy_client); sbus_disconnect(proxy_cli->conn); talloc_zfree(proxy_cli); /* If we time out here, we will also time out to * pc_init_timeout(), so we'll finish the request * there. */ } static int client_registration(DBusMessage *message, struct sbus_connection *conn) { dbus_uint16_t version = DATA_PROVIDER_VERSION; struct proxy_client *proxy_cli; DBusMessage *reply; DBusError dbus_error; dbus_uint16_t cli_ver; uint32_t cli_id; dbus_bool_t dbret; void *data; int hret; hash_key_t key; hash_value_t value; struct tevent_req *req; struct proxy_child_ctx *child_ctx; struct pc_init_ctx *init_ctx; data = sbus_conn_get_private_data(conn); proxy_cli = talloc_get_type(data, struct proxy_client); if (!proxy_cli) { DEBUG(0, ("Connection holds no valid init data\n")); return EINVAL; } /* First thing, cancel the timeout */ DEBUG(4, ("Cancel proxy client ID timeout [%p]\n", proxy_cli->timeout)); talloc_zfree(proxy_cli->timeout); dbus_error_init(&dbus_error); dbret = dbus_message_get_args(message, &dbus_error, DBUS_TYPE_UINT16, &cli_ver, DBUS_TYPE_UINT32, &cli_id, DBUS_TYPE_INVALID); if (!dbret) { DEBUG(1, ("Failed to parse message, killing connection\n")); if (dbus_error_is_set(&dbus_error)) dbus_error_free(&dbus_error); sbus_disconnect(conn); /* FIXME: should we just talloc_zfree(conn) ? */ return EIO; } DEBUG(4, ("Proxy client [%ld] connected\n", cli_id)); /* Check the hash table */ key.type = HASH_KEY_ULONG; key.ul = cli_id; if (!hash_has_key(proxy_cli->proxy_auth_ctx->request_table, &key)) { DEBUG(1, ("Unknown child ID. Killing the connection\n")); sbus_disconnect(proxy_cli->conn); return EIO; } /* reply that all is ok */ reply = dbus_message_new_method_return(message); if (!reply) { DEBUG(0, ("Dbus Out of memory!\n")); return ENOMEM; } dbret = dbus_message_append_args(reply, DBUS_TYPE_UINT16, &version, DBUS_TYPE_INVALID); if (!dbret) { DEBUG(0, ("Failed to build dbus reply\n")); dbus_message_unref(reply); sbus_disconnect(conn); return EIO; } /* send reply back */ sbus_conn_send_reply(conn, reply); dbus_message_unref(reply); hret = hash_lookup(proxy_cli->proxy_auth_ctx->request_table, &key, &value); if (hret != HASH_SUCCESS) { DEBUG(1, ("Hash error [%d][%s]\n", hret, hash_error_string(hret))); sbus_disconnect(conn); } /* Signal that the child is up and ready to receive the request */ req = talloc_get_type(value.ptr, struct tevent_req); child_ctx = tevent_req_data(req, struct proxy_child_ctx); if (!child_ctx->running) { /* This should hopefully be impossible, but protect * against it anyway. If we're not marked running, then * the init_req will be NULL below and things will * break. */ DEBUG(1, ("Client connection from a request " "that's not marked as running\n")); return EIO; } init_ctx = tevent_req_data(child_ctx->init_req, struct pc_init_ctx); init_ctx->conn = conn; tevent_req_done(child_ctx->init_req); child_ctx->init_req = NULL; return EOK; } int sssm_proxy_auth_init(struct be_ctx *bectx, struct bet_ops **ops, void **pvt_data) { struct proxy_auth_ctx *ctx; int ret; int hret; char *sbus_address; /* If we're already set up, just return that */ if(bectx->bet_info[BET_AUTH].mod_name && strcmp("proxy", bectx->bet_info[BET_AUTH].mod_name) == 0) { DEBUG(8, ("Re-using proxy_auth_ctx for this provider\n")); *ops = bectx->bet_info[BET_AUTH].bet_ops; *pvt_data = bectx->bet_info[BET_AUTH].pvt_bet_data; return EOK; } ctx = talloc_zero(bectx, struct proxy_auth_ctx); if (!ctx) { return ENOMEM; } ctx->be = bectx; ctx->timeout_ms = SSS_CLI_SOCKET_TIMEOUT/4; ctx->next_id = 1; ret = confdb_get_string(bectx->cdb, ctx, bectx->conf_path, CONFDB_PROXY_PAM_TARGET, NULL, &ctx->pam_target); if (ret != EOK) goto done; if (!ctx->pam_target) { DEBUG(1, ("Missing option proxy_pam_target.\n")); ret = EINVAL; goto done; } sbus_address = talloc_asprintf(ctx, "unix:path=%s/%s_%s", PIPE_PATH, PROXY_CHILD_PIPE, bectx->domain->name); if (sbus_address == NULL) { DEBUG(1, ("talloc_asprintf failed.\n")); ret = ENOMEM; goto done; } ret = sbus_new_server(ctx, bectx->ev, sbus_address, &proxy_interface, &ctx->sbus_srv, proxy_client_init, ctx); if (ret != EOK) { DEBUG(0, ("Could not set up sbus server.\n")); goto done; } /* Set up request hash table */ /* FIXME: get max_children from configuration file */ ctx->max_children = 10; hret = hash_create(ctx->max_children * 2, &ctx->request_table, NULL, NULL); if (hret != HASH_SUCCESS) { DEBUG(0, ("Could not initialize request table\n")); ret = EIO; goto done; } *ops = &proxy_auth_ops; *pvt_data = ctx; done: if (ret != EOK) { talloc_free(ctx); } return ret; } int sssm_proxy_access_init(struct be_ctx *bectx, struct bet_ops **ops, void **pvt_data) { int ret; ret = sssm_proxy_auth_init(bectx, ops, pvt_data); *ops = &proxy_access_ops; return ret; } int sssm_proxy_chpass_init(struct be_ctx *bectx, struct bet_ops **ops, void **pvt_data) { int ret; ret = sssm_proxy_auth_init(bectx, ops, pvt_data); *ops = &proxy_chpass_ops; return ret; }