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
|
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
*
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
* Microsystems, Inc. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
#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);
}
|