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
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#ifdef SOLARIS
#include <stropts.h>
#else
#include <sys/ioctl.h>
#endif
#include <stdio.h>
#include <errno.h>
#define MAX_PASSWORD_LENGTH 64
#define MAX_CMD_LENGTH 256
void usage() {
fprintf(stderr, "Usage: \n");
fprintf(stderr, " ./privp user privs pid\n");
}
/*
* Kind of wrapper for /sbin/su command.
* Usage:
* privp user privs pid
*
* As an argument it takes pid of process that needs to get privileges privs.
* Then it reads password (to pass to /sbin/su) from stdin
*
*/
int main(int argc, char** argv) {
if (argc < 4) {
usage();
exit(1);
}
char readPassword = 1;
/*
* setsid(2) - creates a new session, if the calling process
* is not a process group leader. Upon return the calling process
* will be the session leader of this new session,
* will be the process group leader of a new process group,
* and will have no controlling terminal.
*/
pid_t gid = setsid();
if (gid == -1) {
// The calling process is already a process group leader
// We already have controlling terminal (?)
readPassword = 0;
}
/*
* /dev/ptmx - a character file used to create a pseudo-terminal master
* and slave pair
*/
int master_fd = open("/dev/ptmx", O_RDWR);
if (master_fd == -1) {
fprintf(stderr, "Cannot open /dev/ptmx\n");
perror(NULL);
exit(1);
}
/*
* Before opening the pseudo-terminal slave, you must pass the master's
* file descriptor to grantpt(3) and unlockpt(3).
*/
if (grantpt(master_fd) == -1) {
fprintf(stderr, "grantpt() failed\n");
perror(NULL);
exit(1);
}
if (unlockpt(master_fd) == -1) {
fprintf(stderr, "unlockpt() failed\n");
perror(NULL);
exit(1);
}
char *slave_name = ptsname(master_fd);
if (slave_name == NULL) {
fprintf(stderr, "Cannot get name of slave pseudo-terminal device\n");
perror(NULL);
exit(1);
}
int slave_fd = open(slave_name, O_RDWR);
#if defined (__SVR4) && defined (__sun)
/*
* ptem(7M) - STREAMS Pseudo Terminal Emulation module
* ptem is a STREAMS module that, when used in conjunction with
* a line discipline and pseudo terminal driver, emulates a terminal.
*
* The ptem module must be pushed onto the slave side of a pseudo terminal
* STREAM, before the ldterm(7M) module is pushed.
*
*/
ioctl(slave_fd, I_PUSH, "ptem");
/*
* ldterm(7M) - standard STREAMS terminal line discipline module
*
* The ldterm STREAMS module provides most of the termio(7I)
* terminal interface.
*/
ioctl(slave_fd, I_PUSH, "ldterm");
/*
* Read password from stdin and write it to master_fd ...
*/
#endif
if (readPassword == 1) {
char pwd[MAX_PASSWORD_LENGTH];
int passwdLength = read(STDIN_FILENO, pwd, MAX_PASSWORD_LENGTH);
int result = write(master_fd, pwd, passwdLength);
// Clear password...
memset(pwd, 0, MAX_PASSWORD_LENGTH);
if (result == -1) {
fprintf(stderr, "Cannot redirect pwd\n");
perror(NULL);
exit(1);
}
}
char command[MAX_CMD_LENGTH];
sprintf(command, "/bin/ppriv -s A+%s %s", argv[2], argv[3]);
/*
* exec(2) -
* ...
* The new process also inherits the following attributes from
* the calling process:
* ...
* session membership
* controlling terminal
* ...
*/
execlp("/sbin/su", "-", argv[1], "-c", command, NULL);
return (EXIT_SUCCESS);
}
|