17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate 237c478bd9Sstevel@tonic-gate /* 24*68300791Scf46844 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate * Use is subject to license terms. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate 28*68300791Scf46844 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 29*68300791Scf46844 /* All Rights Reserved */ 30*68300791Scf46844 317c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate /* 347c478bd9Sstevel@tonic-gate * newgrp [-l | -] [group] 357c478bd9Sstevel@tonic-gate * 367c478bd9Sstevel@tonic-gate * rules 377c478bd9Sstevel@tonic-gate * if no arg, group id in password file is used 387c478bd9Sstevel@tonic-gate * else if group id == id in password file 397c478bd9Sstevel@tonic-gate * else if login name is in member list 407c478bd9Sstevel@tonic-gate * else if password is present and user knows it 417c478bd9Sstevel@tonic-gate * else too bad 427c478bd9Sstevel@tonic-gate */ 437c478bd9Sstevel@tonic-gate #include <stdio.h> 447c478bd9Sstevel@tonic-gate #include <sys/types.h> 457c478bd9Sstevel@tonic-gate #include <pwd.h> 467c478bd9Sstevel@tonic-gate #include <grp.h> 477c478bd9Sstevel@tonic-gate #include <crypt.h> 487c478bd9Sstevel@tonic-gate #include <string.h> 497c478bd9Sstevel@tonic-gate #include <stdlib.h> 507c478bd9Sstevel@tonic-gate #include <locale.h> 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate #define SHELL "/usr/bin/sh" 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate #define PATH "PATH=:/usr/bin:" 557c478bd9Sstevel@tonic-gate #define SUPATH "PATH=:/usr/sbin:/usr/bin" 567c478bd9Sstevel@tonic-gate #define ELIM 128 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate char PW[] = "newgrp: Password: "; 597c478bd9Sstevel@tonic-gate char NG[] = "newgrp: Sorry"; 607c478bd9Sstevel@tonic-gate char PD[] = "newgrp: Permission denied"; 617c478bd9Sstevel@tonic-gate char UG[] = "newgrp: Unknown group"; 627c478bd9Sstevel@tonic-gate char NS[] = "newgrp: You have no shell"; 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate char *homedir; 657c478bd9Sstevel@tonic-gate char *logname; 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate char *envinit[ELIM]; 687c478bd9Sstevel@tonic-gate extern char **environ; 697c478bd9Sstevel@tonic-gate char *path = PATH; 707c478bd9Sstevel@tonic-gate char *supath = SUPATH; 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate extern void audit_newgrp_login(char *, int); 737c478bd9Sstevel@tonic-gate 74*68300791Scf46844 void error(char *s) __NORETURN; 75*68300791Scf46844 void warn(char *s); 76*68300791Scf46844 void usage(void); 77*68300791Scf46844 char *rname(char *); 78*68300791Scf46844 79*68300791Scf46844 int 80*68300791Scf46844 main(int argc, char *argv[]) 817c478bd9Sstevel@tonic-gate { 82*68300791Scf46844 char *s; 83*68300791Scf46844 struct passwd *p; 847c478bd9Sstevel@tonic-gate gid_t chkgrp(); 857c478bd9Sstevel@tonic-gate int eflag = 0; 867c478bd9Sstevel@tonic-gate int flag; 877c478bd9Sstevel@tonic-gate uid_t uid; 887c478bd9Sstevel@tonic-gate char *shell, *dir, *name; 897c478bd9Sstevel@tonic-gate size_t len; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate #ifdef DEBUG 927c478bd9Sstevel@tonic-gate chroot("."); 937c478bd9Sstevel@tonic-gate #endif 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 967c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 977c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 987c478bd9Sstevel@tonic-gate #endif 997c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate if ((p = getpwuid(getuid())) == NULL) 1027c478bd9Sstevel@tonic-gate error(NG); 1037c478bd9Sstevel@tonic-gate endpwent(); 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate while ((flag = getopt(argc, argv, "l")) != EOF) { 1067c478bd9Sstevel@tonic-gate switch (flag) { 1077c478bd9Sstevel@tonic-gate case 'l': 1087c478bd9Sstevel@tonic-gate eflag++; 1097c478bd9Sstevel@tonic-gate break; 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate default: 1127c478bd9Sstevel@tonic-gate usage(); 1137c478bd9Sstevel@tonic-gate break; 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate argc -= optind; 1187c478bd9Sstevel@tonic-gate argv = &argv[optind]; 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate if (argc > 0 && *argv[0] == '-') { 1217c478bd9Sstevel@tonic-gate if (eflag) 1227c478bd9Sstevel@tonic-gate usage(); 1237c478bd9Sstevel@tonic-gate eflag++; 1247c478bd9Sstevel@tonic-gate argv++; 1257c478bd9Sstevel@tonic-gate --argc; 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate if (argc > 0) 1297c478bd9Sstevel@tonic-gate p->pw_gid = chkgrp(argv[0], p); 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate uid = p->pw_uid; 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate len = strlen(p->pw_dir) + 1; 1347c478bd9Sstevel@tonic-gate if ((dir = (char *)malloc(len)) == NULL) 1357c478bd9Sstevel@tonic-gate error("newgrp: Memory request failed"); 1367c478bd9Sstevel@tonic-gate (void) strncpy(dir, p->pw_dir, len); 1377c478bd9Sstevel@tonic-gate len = strlen(p->pw_name) + 1; 1387c478bd9Sstevel@tonic-gate if ((name = (char *)malloc(len)) == NULL) 1397c478bd9Sstevel@tonic-gate error("newgrp: Memory request failed"); 1407c478bd9Sstevel@tonic-gate (void) strncpy(name, p->pw_name, len); 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate if (setgid(p->pw_gid) < 0 || setuid(getuid()) < 0) 1437c478bd9Sstevel@tonic-gate error(NG); 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate if (!*p->pw_shell) { 1467c478bd9Sstevel@tonic-gate if ((shell = getenv("SHELL")) != NULL) { 1477c478bd9Sstevel@tonic-gate p->pw_shell = shell; 1487c478bd9Sstevel@tonic-gate } else { 1497c478bd9Sstevel@tonic-gate p->pw_shell = SHELL; 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate if (eflag) { 1547c478bd9Sstevel@tonic-gate char *simple; 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate len = strlen(dir) + 6; 1577c478bd9Sstevel@tonic-gate if ((homedir = (char *)malloc(len)) == NULL) 1587c478bd9Sstevel@tonic-gate error("newgrp: Memory request failed"); 1597c478bd9Sstevel@tonic-gate (void) snprintf(homedir, len, "HOME=%s", dir); 1607c478bd9Sstevel@tonic-gate len = strlen(name) + 9; 1617c478bd9Sstevel@tonic-gate if ((logname = (char *)malloc(len)) == NULL) 1627c478bd9Sstevel@tonic-gate error("newgrp: Memory request failed"); 1637c478bd9Sstevel@tonic-gate (void) snprintf(logname, len, "LOGNAME=%s", name); 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate envinit[2] = logname; 1677c478bd9Sstevel@tonic-gate chdir(dir); 1687c478bd9Sstevel@tonic-gate envinit[0] = homedir; 1697c478bd9Sstevel@tonic-gate if (uid == 0) 1707c478bd9Sstevel@tonic-gate envinit[1] = supath; 1717c478bd9Sstevel@tonic-gate else 1727c478bd9Sstevel@tonic-gate envinit[1] = path; 1737c478bd9Sstevel@tonic-gate envinit[3] = NULL; 1747c478bd9Sstevel@tonic-gate environ = envinit; 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate len = strlen(p->pw_shell) + 2; 1777c478bd9Sstevel@tonic-gate if ((shell = (char *)malloc(len)) == NULL) 1787c478bd9Sstevel@tonic-gate error("newgrp: Memory request failed"); 1797c478bd9Sstevel@tonic-gate (void) snprintf(shell, len, "-%s", p->pw_shell); 1807c478bd9Sstevel@tonic-gate simple = strrchr(shell, '/'); 1817c478bd9Sstevel@tonic-gate if (simple) { 1827c478bd9Sstevel@tonic-gate *(shell+1) = '\0'; 1837c478bd9Sstevel@tonic-gate shell = strcat(shell, ++simple); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate else 1877c478bd9Sstevel@tonic-gate shell = p->pw_shell; 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate execl(p->pw_shell, shell, NULL); 1907c478bd9Sstevel@tonic-gate error(NS); 191*68300791Scf46844 /* NOTREACHED */ 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate 194*68300791Scf46844 void 195*68300791Scf46844 warn(char *s) 1967c478bd9Sstevel@tonic-gate { 1977c478bd9Sstevel@tonic-gate fprintf(stderr, "%s\n", gettext(s)); 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate 200*68300791Scf46844 void 201*68300791Scf46844 error(char *s) 2027c478bd9Sstevel@tonic-gate { 2037c478bd9Sstevel@tonic-gate warn(s); 2047c478bd9Sstevel@tonic-gate exit(1); 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate gid_t 2087c478bd9Sstevel@tonic-gate chkgrp(gname, p) 2097c478bd9Sstevel@tonic-gate char *gname; 2107c478bd9Sstevel@tonic-gate struct passwd *p; 2117c478bd9Sstevel@tonic-gate { 212*68300791Scf46844 char **t; 213*68300791Scf46844 struct group *g; 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate g = getgrnam(gname); 2167c478bd9Sstevel@tonic-gate endgrent(); 2177c478bd9Sstevel@tonic-gate if (g == NULL) { 2187c478bd9Sstevel@tonic-gate warn(UG); 2197c478bd9Sstevel@tonic-gate return (getgid()); 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate if (p->pw_gid == g->gr_gid || getuid() == 0) 2227c478bd9Sstevel@tonic-gate return (g->gr_gid); 2237c478bd9Sstevel@tonic-gate for (t = g->gr_mem; *t; ++t) { 2247c478bd9Sstevel@tonic-gate if (strcmp(p->pw_name, *t) == 0) 2257c478bd9Sstevel@tonic-gate return (g->gr_gid); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate if (*g->gr_passwd) { 2287c478bd9Sstevel@tonic-gate if (!isatty(fileno(stdin))) { 2297c478bd9Sstevel@tonic-gate error(PD); 2307c478bd9Sstevel@tonic-gate } 2317c478bd9Sstevel@tonic-gate if (strcmp(g->gr_passwd, 2327c478bd9Sstevel@tonic-gate crypt(getpass(PW), g->gr_passwd)) == 0) { 2337c478bd9Sstevel@tonic-gate audit_newgrp_login(gname, 0); 2347c478bd9Sstevel@tonic-gate return (g->gr_gid); 2357c478bd9Sstevel@tonic-gate } 2367c478bd9Sstevel@tonic-gate audit_newgrp_login(gname, 1); 2377c478bd9Sstevel@tonic-gate } 2387c478bd9Sstevel@tonic-gate warn(NG); 2397c478bd9Sstevel@tonic-gate return (getgid()); 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate /* 2437c478bd9Sstevel@tonic-gate * return pointer to rightmost component of pathname 2447c478bd9Sstevel@tonic-gate */ 2457c478bd9Sstevel@tonic-gate char * 246*68300791Scf46844 rname(char *pn) 2477c478bd9Sstevel@tonic-gate { 248*68300791Scf46844 char *q; 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate q = pn; 2517c478bd9Sstevel@tonic-gate while (*pn) 2527c478bd9Sstevel@tonic-gate if (*pn++ == '/') 2537c478bd9Sstevel@tonic-gate q = pn; 2547c478bd9Sstevel@tonic-gate return (q); 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate 257*68300791Scf46844 void 258*68300791Scf46844 usage(void) 2597c478bd9Sstevel@tonic-gate { 2607c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 2617c478bd9Sstevel@tonic-gate "usage: newgrp [-l | -] [group]\n")); 2627c478bd9Sstevel@tonic-gate exit(2); 2637c478bd9Sstevel@tonic-gate } 264