2009-09-17 10:42:13 +00:00
|
|
|
// This file is part of BOINC.
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2009 University of California
|
|
|
|
//
|
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// BOINC 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 Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/* AddRemoveUser.cpp */
|
|
|
|
|
|
|
|
#include <Carbon/Carbon.h>
|
|
|
|
|
|
|
|
#include <unistd.h> // getlogin
|
|
|
|
#include <sys/types.h> // getpwname, getpwuid, getuid
|
|
|
|
#include <pwd.h> // getpwname, getpwuid, getuid
|
|
|
|
#include <grp.h> // getgrnam
|
|
|
|
|
|
|
|
#include "LoginItemAPI.h" //please take a look at LoginItemAPI.h for an explanation of the routines available to you.
|
|
|
|
|
|
|
|
void printUsage(void);
|
2011-08-11 10:20:25 +00:00
|
|
|
void SetLoginItem(char *user, Boolean addLogInItem);
|
2009-09-17 10:42:13 +00:00
|
|
|
static char * PersistentFGets(char *buf, size_t buflen, FILE *f);
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
Boolean AddUsers = false;
|
|
|
|
Boolean SetSavers = false;
|
2010-02-05 04:34:43 +00:00
|
|
|
Boolean isBMGroupMember, isBPGroupMember;
|
2009-09-17 10:42:13 +00:00
|
|
|
Boolean saverIsSet = false;
|
|
|
|
passwd *pw;
|
|
|
|
uid_t saved_uid;
|
|
|
|
long OSVersion;
|
|
|
|
group grpBOINC_master, *grpBOINC_masterPtr;
|
2010-02-05 04:34:43 +00:00
|
|
|
group grpBOINC_project, *grpBOINC_projectPtr;
|
2009-09-17 10:42:13 +00:00
|
|
|
char bmBuf[32768];
|
2010-02-05 09:08:01 +00:00
|
|
|
char bpBuf[32768];
|
2009-09-17 10:42:13 +00:00
|
|
|
short index, i;
|
|
|
|
char *p;
|
|
|
|
char s[256];
|
|
|
|
FILE *f;
|
|
|
|
OSStatus err;
|
|
|
|
|
|
|
|
#ifndef _DEBUG
|
|
|
|
if (getuid() != 0) {
|
|
|
|
printf("This program must be run as root\n");
|
|
|
|
printUsage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (argc < 3) {
|
|
|
|
printUsage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(argv[1], "-a") == 0) {
|
|
|
|
AddUsers = true;
|
|
|
|
} else if (strcmp(argv[1], "-s") == 0) {
|
|
|
|
AddUsers = true;
|
|
|
|
SetSavers = true;
|
|
|
|
} else if (strcmp(argv[1], "-r") != 0) {
|
|
|
|
printUsage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = Gestalt(gestaltSystemVersion, &OSVersion);
|
|
|
|
if (err != noErr)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
err = getgrnam_r("boinc_master", &grpBOINC_master, bmBuf, sizeof(bmBuf), &grpBOINC_masterPtr);
|
|
|
|
if (err) { // Should never happen unless buffer too small
|
|
|
|
puts("getgrnam(\"boinc_master\") failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-02-05 09:08:01 +00:00
|
|
|
err = getgrnam_r("boinc_project", &grpBOINC_project, bpBuf, sizeof(bpBuf), &grpBOINC_projectPtr);
|
2010-02-05 04:34:43 +00:00
|
|
|
if (err) { // Should never happen unless buffer too small
|
|
|
|
puts("getgrnam(\"boinc_project\") failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-09-17 10:42:13 +00:00
|
|
|
for (index=2; index<argc; index++) {
|
2010-04-17 23:55:00 +00:00
|
|
|
// getpwnam works with either the full / login name (pw->pw_gecos)
|
|
|
|
// or the short / Posix name (pw->pw_name)
|
2010-02-05 04:34:43 +00:00
|
|
|
pw = getpwnam(argv[index]);
|
|
|
|
if (pw == NULL) {
|
|
|
|
printf("User %s not found.\n", argv[index]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
isBMGroupMember = false;
|
2009-09-17 10:42:13 +00:00
|
|
|
i = 0;
|
|
|
|
while ((p = grpBOINC_master.gr_mem[i]) != NULL) { // Step through all users in group boinc_master
|
2010-04-17 23:55:00 +00:00
|
|
|
if (strcmp(p, pw->pw_name) == 0) { // Only the short / Posix names are in the list
|
2009-09-17 10:42:13 +00:00
|
|
|
// User is a member of group boinc_master
|
2010-02-05 04:34:43 +00:00
|
|
|
isBMGroupMember = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
isBPGroupMember = false;
|
|
|
|
i = 0;
|
|
|
|
while ((p = grpBOINC_project.gr_mem[i]) != NULL) { // Step through all users in group boinc_project
|
2010-04-17 23:55:00 +00:00
|
|
|
if (strcmp(p, pw->pw_name) == 0) { // Only the short / Posix names are in the list
|
2010-02-05 04:34:43 +00:00
|
|
|
// User is a member of group boinc_master
|
|
|
|
isBPGroupMember = true;
|
2009-09-17 10:42:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2010-02-05 04:34:43 +00:00
|
|
|
if ((!isBMGroupMember) && AddUsers) {
|
2010-04-17 23:55:00 +00:00
|
|
|
sprintf(s, "dscl . -merge /groups/boinc_master users %s", pw->pw_name);
|
2009-09-17 10:42:13 +00:00
|
|
|
system(s);
|
|
|
|
}
|
|
|
|
|
2010-02-05 04:34:43 +00:00
|
|
|
if ((!isBPGroupMember) && AddUsers) {
|
2010-04-17 23:55:00 +00:00
|
|
|
sprintf(s, "dscl . -merge /groups/boinc_project users %s", pw->pw_name);
|
2010-02-05 04:34:43 +00:00
|
|
|
system(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isBMGroupMember && (!AddUsers)) {
|
2010-04-17 23:55:00 +00:00
|
|
|
sprintf(s, "dscl . -delete /Groups/boinc_master GroupMembership %s", pw->pw_name);
|
2009-09-17 10:42:13 +00:00
|
|
|
system(s);
|
|
|
|
}
|
|
|
|
|
2010-02-05 04:34:43 +00:00
|
|
|
if (isBPGroupMember && (!AddUsers)) {
|
2010-04-17 23:55:00 +00:00
|
|
|
sprintf(s, "dscl . -delete /Groups/boinc_project GroupMembership %s", pw->pw_name);
|
2010-02-05 04:34:43 +00:00
|
|
|
system(s);
|
2009-09-17 10:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
saved_uid = geteuid();
|
|
|
|
seteuid(pw->pw_uid); // Temporarily set effective uid to this user
|
|
|
|
|
2011-08-11 10:20:25 +00:00
|
|
|
SetLoginItem(pw->pw_name, AddUsers); // Set or remove login item for this user
|
2009-09-17 10:42:13 +00:00
|
|
|
|
|
|
|
if (OSVersion < 0x1060) {
|
|
|
|
sprintf(s, "sudo -u %s defaults -currentHost read com.apple.screensaver moduleName",
|
2010-04-17 23:55:00 +00:00
|
|
|
pw->pw_name);
|
2009-09-17 10:42:13 +00:00
|
|
|
} else {
|
|
|
|
sprintf(s, "sudo -u %s defaults -currentHost read com.apple.screensaver moduleDict -dict",
|
2010-04-17 23:55:00 +00:00
|
|
|
pw->pw_name);
|
2009-09-17 10:42:13 +00:00
|
|
|
}
|
|
|
|
f = popen(s, "r");
|
|
|
|
|
|
|
|
if (f) {
|
|
|
|
saverIsSet = false;
|
|
|
|
while (PersistentFGets(s, sizeof(s), f)) {
|
|
|
|
if (strstr(s, "BOINCSaver")) {
|
|
|
|
saverIsSet = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!saverIsSet) && SetSavers) {
|
|
|
|
if (OSVersion < 0x1060) {
|
|
|
|
sprintf(s, "sudo -u %s defaults -currentHost write com.apple.screensaver moduleName BOINCSaver",
|
2010-04-17 23:55:00 +00:00
|
|
|
pw->pw_name);
|
2009-09-17 10:42:13 +00:00
|
|
|
system(s);
|
|
|
|
sprintf(s, "sudo -u %s defaults -currentHost write com.apple.screensaver modulePath \"/Library/Screen Savers/BOINCSaver.saver\"",
|
2010-04-17 23:55:00 +00:00
|
|
|
pw->pw_name);
|
2009-09-17 10:42:13 +00:00
|
|
|
system(s);
|
|
|
|
} else {
|
|
|
|
sprintf(s, "sudo -u %s defaults -currentHost write com.apple.screensaver moduleDict -dict moduleName BOINCSaver path \"/Library/Screen Savers/BOINCSaver.saver\"",
|
2010-04-17 23:55:00 +00:00
|
|
|
pw->pw_name);
|
2009-09-17 10:42:13 +00:00
|
|
|
system(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (saverIsSet && (!AddUsers)) {
|
|
|
|
if (OSVersion < 0x1060) {
|
|
|
|
sprintf(s, "sudo -u %s defaults -currentHost write com.apple.screensaver moduleName Flurry",
|
2010-04-17 23:55:00 +00:00
|
|
|
pw->pw_name);
|
2009-09-17 10:42:13 +00:00
|
|
|
system(s);
|
|
|
|
sprintf(s, "sudo -u %s defaults -currentHost write com.apple.screensaver modulePath \"/System/Library/Screen Savers/Flurry.saver\"",
|
2010-04-17 23:55:00 +00:00
|
|
|
pw->pw_name);
|
2009-09-17 10:42:13 +00:00
|
|
|
system(s);
|
|
|
|
} else {
|
|
|
|
sprintf(s, "sudo -u %s defaults -currentHost write com.apple.screensaver moduleDict -dict moduleName Flurry path \"/System/Library/Screen Savers/Flurry.saver\"",
|
2010-04-17 23:55:00 +00:00
|
|
|
pw->pw_name);
|
2009-09-17 10:42:13 +00:00
|
|
|
system(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
seteuid(saved_uid); // Set effective uid back to privileged user
|
|
|
|
}
|
|
|
|
|
2011-08-11 10:20:25 +00:00
|
|
|
if (AddUsers) {
|
|
|
|
printf("WARNING: Changes may require a system restart to take effect.\n");
|
|
|
|
}
|
2009-09-17 10:42:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void printUsage() {
|
|
|
|
printf("Usage: sudo AddRemoveUser [-a | -s | -r] [user1 [user2 [user3...]]]\n");
|
|
|
|
printf(" -a: add users to those with permission to run BOINC Manager.\n");
|
|
|
|
printf(" -s: same as -a plus set users' screensaver to BOINC.\n");
|
|
|
|
printf(" -r: remove users' permission to run BOINC Manager, and \n");
|
|
|
|
printf(" if their screensaver was set to BOINC change it to Flurry.\n");
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2011-08-11 10:20:25 +00:00
|
|
|
void SetLoginItem(char *user, Boolean addLogInItem){
|
|
|
|
char cmd[2048];
|
|
|
|
OSErr err;
|
2009-09-17 10:42:13 +00:00
|
|
|
|
2011-08-11 10:20:25 +00:00
|
|
|
sprintf(cmd, "sudo -u %s osascript -e 'tell application \"System Events\"' -e 'delete (every login item whose path contains \"BOINCManager\")' -e 'end tell'", user);
|
2009-09-17 10:42:13 +00:00
|
|
|
|
2011-08-11 10:20:25 +00:00
|
|
|
err = system(cmd);
|
|
|
|
if (err) {
|
|
|
|
printf("Delete BOINCManager login item for user %s returned error %d\n", user, err);
|
|
|
|
}
|
|
|
|
|
2009-09-17 10:42:13 +00:00
|
|
|
if (addLogInItem) {
|
2011-08-11 10:20:25 +00:00
|
|
|
sprintf(cmd, "sudo -u %s osascript -e 'tell application \"System Events\"' -e 'make new login item at end with properties {path:\"/Applications/BOINCManager.app\", hidden:true, kind:Application, name:\"BOINCManager\"}' -e 'end tell'", user);
|
|
|
|
|
|
|
|
err = system(cmd);
|
|
|
|
if (err) {
|
|
|
|
printf("Add BOINCManager login item for user %s returned error %d\n", user, err);
|
|
|
|
}
|
2009-09-17 10:42:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char * PersistentFGets(char *buf, size_t buflen, FILE *f) {
|
|
|
|
char *p = buf;
|
|
|
|
size_t len = buflen;
|
|
|
|
size_t datalen = 0;
|
|
|
|
|
|
|
|
*buf = '\0';
|
|
|
|
while (datalen < (buflen - 1)) {
|
|
|
|
fgets(p, len, f);
|
|
|
|
if (feof(f)) break;
|
|
|
|
if (ferror(f) && (errno != EINTR)) break;
|
|
|
|
if (strchr(buf, '\n')) break;
|
|
|
|
datalen = strlen(buf);
|
|
|
|
p = buf + datalen;
|
|
|
|
len -= datalen;
|
|
|
|
}
|
|
|
|
return (buf[0] ? buf : NULL);
|
|
|
|
}
|