Changeset 438

Show
Ignore:
Timestamp:
06/07/2009 01:39:17 (9 months ago)
Author:
douglas
Message:

Some cleanup and MySQL crap.

Location:
admin
Files:
11 modified
2 copied

Legend:

Unmodified
Added
Removed
  • admin/GNUmakefile

    r415 r438  
    1111COMMANDS := createuser date diff grep ldapmodify ldapsearch sed tail 
    1212CPPFLAGS := -DLDAPSEARCH=\"$(shell which ldapsearch)\" -DLDAPWHOAMI=\"$(shell which ldapwhoami)\" -DMASTER=\"$(MASTER)\" 
     13CFLAGS := -std=c99 -Wall -pedantic -g -O2 
    1314UNAME :=$(shell uname) 
    1415HOSTNAME :=$(shell hostname -f 2> /dev/null || uname -n) 
     
    3233COMMANDS += ldapadd ldappasswd slappasswd smbpasswd 
    3334MASTERSLAVE :=Master 
     35MYSQL_CFLAGS := $(shell mysql_config --include) 
     36MYSQL_LDFLAGS := $(shell mysql_config --libs) 
    3437else 
    3538ifeq ($(HOSTNAME),$(SLAVE)) 
     
    6265endif 
    6366 
    64 CFLAGS := -std=c99 -Wall -pedantic -g -O2 
    65  
    6667ifeq ($(shell sed --version </dev/null 2>/dev/null),) 
    6768E :=E 
     
    8283        $(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@ 
    8384 
     85ifdef ISMASTER 
     86$(MACHINE)/adduser: adduser.c mysql.h users.h $(MACHINE)/common.o $(MACHINE)/mysql.o 
     87        @mkdir -p $(MACHINE) 
     88        $(CC) $(CPPFLAGS) $(CFLAGS) $(MYSQL_CFLAGS) $(LDFLAGS) $(MYSQL_LDFLAGS) $(filter-out %.h,$^) $(LOADLIBES) $(LDLIBS) -o $@ 
     89 
     90$(MACHINE)/passwd: passwd.c mysql.h $(MACHINE)/common.o $(MACHINE)/mysql.o 
     91        @mkdir -p $(MACHINE) 
     92        $(CC) $(CPPFLAGS) $(CFLAGS) $(MYSQL_CFLAGS) $(LDFLAGS) $(MYSQL_LDFLAGS) $(filter-out %.h,$^) $(LOADLIBES) $(LDLIBS) -o $@ 
     93else 
    8494$(MACHINE)/adduser: adduser.c users.h $(MACHINE)/common.o 
    8595 
    8696$(MACHINE)/passwd: passwd.c $(MACHINE)/common.o 
     97endif 
    8798 
    8899$(MACHINE)/chsh: chsh.c $(MACHINE)/common.o 
     
    103114 
    104115$(MACHINE)/common.o: common.c common.h 
     116 
     117ifdef ISMASTER 
     118$(MACHINE)/mysql.o: mysql.c mysql.h 
     119        @mkdir -p $(MACHINE) 
     120        $(CC) -c $(CPPFLAGS) $(CFLAGS) $(MYSQL_CFLAGS) $< -o $@ 
     121endif 
    105122 
    106123$(MACHINE)/common.sh: GNUmakefile $(MACHINE)/shells 
     
    205222 
    206223clean: 
    207         -rm -rf $(MACHINE) *.core 
     224        -rm -f $(MACHINE)/* *.core 
  • admin/adduser.c

    r264 r438  
    77 
    88#include "common.h" 
     9 
     10#ifdef _Master_ 
     11#include "mysql.h" 
     12#endif 
    913 
    1014#include <errno.h> 
     
    7983        jmp_buf environment; 
    8084 
     85#ifdef _Master_ 
     86        MYSQL_BEGIN(); 
     87#endif 
     88 
    8189        switch (setjmp(environment)) 
    8290        { 
    8391        case 0: 
    8492                break; 
    85         case 1: 
     93        case POSIX_EXCEPTION: 
    8694                perror(argv[0]); 
    8795 
    88                 return 1; 
    89         default: 
     96                goto end; 
     97        case STRING_EXCEPTION: 
    9098                fprintf(stderr, "%s: %s\n", argv[0], exception); 
    91  
     99#ifdef _Master_ 
     100 
     101                goto end; 
     102        case MYSQL_EXCEPTION: 
     103                fprintf(stderr, "%s: %s\n", argv[0], mysql_error(mysql)); 
     104#endif 
     105 
     106end: 
     107#ifdef _Master_ 
     108                MYSQL_END(); 
     109 
     110#endif 
    92111                return 1; 
    93112        } 
     
    113132                        printf("Usage: %s [-user=user] [-name=name]\n", argv[0]); 
    114133 
    115                         return 1; 
     134                        goto end; 
    116135                } 
    117136        } 
     
    139158        } 
    140159        else if (exists(argv[0], user, environment)) 
    141                 longjmp(environment, (exception = "User exists", 3)); 
     160                longjmp(environment, (exception = "User exists", STRING_EXCEPTION)); 
    142161 
    143162        if (!name) 
     
    179198 
    180199        if (fprintf(smbpasswd, "%s\n%s\n", password, password) < 0) 
    181                 longjmp(environment, 1); 
     200                longjmp(environment, POSIX_EXCEPTION); 
    182201 
    183202        if (fclose(smbpasswd)) 
    184                 longjmp(environment, 1); 
     203                longjmp(environment, POSIX_EXCEPTION); 
    185204 
    186205        int status; 
     
    190209 
    191210        if (WEXITSTATUS(status)) 
    192                 return 1; 
     211                goto end; 
     212 
     213        mysqlpassword(mysql, user, password, environment); 
     214        MYSQL_END(); 
    193215 
    194216        struct passwd *entry = getpwnam(user); 
    195217 
    196218        if (!entry) 
    197                 longjmp(environment, 1); 
     219                longjmp(environment, POSIX_EXCEPTION); 
    198220 
    199221        check(mkdir(entry->pw_dir, S_IRWXU | S_IRWXG | S_IRWXO), environment); 
     
    204226 
    205227        if (!traversal) 
    206                 longjmp(environment, 1); 
     228                longjmp(environment, POSIX_EXCEPTION); 
    207229 
    208230        FTSENT *entity; 
     
    243265 
    244266        if (errno) 
    245                 longjmp(environment, 1); 
     267                longjmp(environment, POSIX_EXCEPTION); 
    246268 
    247269        if (fts_close(traversal)) 
    248                 longjmp(environment, 1); 
     270                longjmp(environment, POSIX_EXCEPTION); 
    249271#else 
    250         longjmp(environment, (exception = "Log in to " MASTER " to add users", 3)); 
     272        longjmp(environment, (exception = "Log in to " MASTER " to add users", STRING_EXCEPTION)); 
    251273#endif 
    252274 
  • admin/chfn.c

    r264 r438  
    2121        case 0: 
    2222                break; 
    23         case 1: 
     23        case POSIX_EXCEPTION: 
    2424                perror(argv[0]); 
    2525 
    2626                return 1; 
    27         default: 
     27        case STRING_EXCEPTION: 
    2828                fprintf(stderr, "%s: %s\n", argv[0], exception); 
    2929 
     
    6464                        goto on; 
    6565                else 
    66                         longjmp(environment, (exception = "Cannot change root full name", 3)); 
     66                        longjmp(environment, (exception = "Cannot change root full name", STRING_EXCEPTION)); 
    6767        } 
    6868        else 
     
    8484 
    8585        if (!entry) 
    86                 longjmp(environment, 1); 
     86                longjmp(environment, POSIX_EXCEPTION); 
    8787 
    8888        strlcpy(user, entry->pw_name, sizeof (user)); 
  • admin/chsh.c

    r264 r438  
    2323        case 0: 
    2424                break; 
    25         case 1: 
     25        case POSIX_EXCEPTION: 
    2626                perror(argv[0]); 
    2727 
    2828                return 1; 
    29         default: 
     29        case STRING_EXCEPTION: 
    3030                fprintf(stderr, "%s: %s\n", argv[0], exception); 
    3131 
     
    8585                        goto on; 
    8686                else 
    87                         longjmp(environment, (exception = "Cannot change root shell", 3)); 
     87                        longjmp(environment, (exception = "Cannot change root shell", STRING_EXCEPTION)); 
    8888        } 
    8989        else 
     
    105105 
    106106        if (!entry) 
    107                 longjmp(environment, 1); 
     107                longjmp(environment, POSIX_EXCEPTION); 
    108108 
    109109        strlcpy(user, entry->pw_name, sizeof (user)); 
  • admin/common.c

    r264 r438  
    3030{ 
    3131        if (value == -1) 
    32                 longjmp(environment, 1); 
     32                longjmp(environment, POSIX_EXCEPTION); 
    3333 
    3434        return value; 
     
    5353        { 
    5454                if (ferror(stream)) 
    55                         longjmp(environment, 1); 
     55                        longjmp(environment, POSIX_EXCEPTION); 
    5656                else 
    5757                { 
     
    7373 
    7474        if (!entry) 
    75                 longjmp(environment, 1); 
     75                longjmp(environment, POSIX_EXCEPTION); 
    7676 
    7777        char name[strlen(entry->pw_name) + 36]; 
     
    8383 
    8484        if (!strlen(password)) 
    85                 longjmp(environment, (exception = "Empty password", 3)); 
     85                longjmp(environment, (exception = "Empty password", STRING_EXCEPTION)); 
    8686 
    8787        char secret[] = "/tmp/secret.XXXXXX"; 
     
    113113 
    114114        if (WEXITSTATUS(status)) 
    115                 longjmp(environment, (exception = "Sorry", 3)); 
     115                longjmp(environment, (exception = "Sorry", STRING_EXCEPTION)); 
    116116 
    117117        char whoami[size]; 
     
    124124 
    125125        if (strcmp(whoami, whoami_)) 
    126                 longjmp(environment, (exception = "Sorry", 3)); 
     126                longjmp(environment, (exception = "Sorry", STRING_EXCEPTION)); 
    127127 
    128128        free(whoami_); 
     
    134134 
    135135        if (!strlen(password)) 
    136                 longjmp(environment, (exception = "Empty password", 3)); 
     136                longjmp(environment, (exception = "Empty password", STRING_EXCEPTION)); 
    137137 
    138138        if (strcmp(password, getpass("Confirm Password: "))) 
    139                 longjmp(environment, (exception = "Password and confirm password mismatched", 3)); 
     139                longjmp(environment, (exception = "Password and confirm password mismatched", STRING_EXCEPTION)); 
    140140} 
    141141 
  • admin/common.h

    r264 r438  
    4646#endif 
    4747 
     48#define POSIX_EXCEPTION 1 
     49#define STRING_EXCEPTION 2 
     50#define SECRET "/ccs/etc/secret" 
     51 
    4852extern char *exception; 
    4953 
  • admin/mysql.c

    r264 r438  
    66// $Id$ 
    77 
    8 #include "common.h" 
     8#include "mysql.h" 
    99 
    10 #include <ctype.h> 
     10#include <fcntl.h> 
     11#include <stdbool.h> 
    1112 
    12 #ifdef __sun__ 
    13 #define getpass getpassphrase 
    14 #endif 
    15  
    16 char *exception = NULL; 
    17  
    18 Shells shells[] = { 
    19         { "sh", "/bin/sh", "/bin/sh", "/bin/sh", "/usr/bin/sh", "/bin/sh", "/bin/sh" }, 
    20         { "csh", "/bin/csh", "/bin/csh", "/bin/csh", "/usr/bin/csh", "/bin/csh", "/bin/csh" }, 
    21         { "tcsh", "/bin/tcsh", "/bin/tcsh", "/bin/tcsh", "/usr/bin/tcsh", "/usr/local/bin/tcsh", "/usr/bin/tcsh" }, 
    22         { "bash", "/usr/local/bin/bash", "/bin/bash", "/opt/local/bin/bash", "/usr/local/bin/bash", "/usr/local/bin/bash", "/bin/bash" }, 
    23         { "ksh", "/usr/local/bin/ksh", "/bin/ksh", "/bin/ksh", "/usr/bin/ksh", "/bin/ksh", "/usr/bin/ksh" }, 
    24         { "zsh", "/usr/local/bin/zsh", "/bin/zsh", "/bin/zsh", "/usr/bin/zsh", "/usr/local/bin/zsh", "/usr/bin/zsh" }, 
    25         { "3sh", "/ccs/bin/3sh", "/ccs/bin/3sh", "/ccs/bin/3sh", "/ccs/bin/3sh", "/ccs/bin/3sh", "/ccs/bin/3sh" }, 
    26         { "custom", NULL, NULL, NULL, NULL, NULL, NULL } 
    27 }; 
    28  
    29 int check(int value, jmp_buf environment) 
     13void mysqlcheck(int value, jmp_buf environment) 
    3014{ 
    31         if (value == -1) 
    32                 longjmp(environment, 1); 
    33  
    34         return value; 
     15        if (value) 
     16                longjmp(environment, MYSQL_EXCEPTION); 
    3517} 
    3618 
    37 int regcheck(int value, const regex_t *regex, jmp_buf environment) 
     19void mysqlpassword(MYSQL *mysql, char user[MAXLOGNAME], char password[_PASSWORD_LEN], jmp_buf environment) 
    3820{ 
    39         if (value && value != REG_NOMATCH) 
    40         { 
    41                 char error[regerror(value, regex, NULL, 0)]; 
     21        FILE *stream = fopen(SECRET, "r"); 
    4222 
    43                 regerror(value, regex, error, sizeof (error)); 
    44                 longjmp(environment, (exception = error, 3)); 
    45         } 
     23        if (!stream) 
     24                longjmp(environment, POSIX_EXCEPTION); 
    4625 
    47         return value; 
    48 } 
     26        char rootpassword[_PASSWORD_LEN] = ""; 
    4927 
    50 char *fcheck(char *value, FILE *stream, jmp_buf environment) 
    51 { 
     28        fcheck(fgets(rootpassword, _PASSWORD_LEN, stream), stream, environment); 
     29 
     30        if (fclose(stream)) 
     31                longjmp(environment, POSIX_EXCEPTION); 
     32 
     33        if (!mysql_real_connect(mysql, NULL, "root", rootpassword, "mysql", 0, NULL, 0)) 
     34                longjmp(environment, MYSQL_EXCEPTION); 
     35 
     36        MYSQL_STMT *statement = mysql_stmt_init(mysql); 
     37 
     38        if (!statement) 
     39                longjmp(environment, MYSQL_EXCEPTION); 
     40 
     41        char *update = "update user set Password = PASSWORD(?) where User = ?"; 
     42 
     43        mysqlcheck(mysql_stmt_prepare(statement, update, strlen(update)), environment); 
     44 
     45        size_t userlength = strlen(user), passwordlength = strlen(password); 
     46        MYSQL_BIND parameters[2] = { { .buffer_type = MYSQL_TYPE_STRING, .buffer = password, .buffer_length = passwordlength, .length = &passwordlength, .is_null = NULL, .is_unsigned = false, .error = NULL, 0 }, { .buffer_type = MYSQL_TYPE_STRING, .buffer = user, .buffer_length = userlength, .length = &userlength, .is_null = NULL, .is_unsigned = false, .error = NULL, 0 } }; 
     47 
     48        mysqlcheck(mysql_stmt_bind_param(statement, parameters), environment); 
     49        mysqlcheck(mysql_stmt_execute(statement), environment); 
     50 
     51        my_ulonglong value = mysql_stmt_affected_rows(statement); 
     52 
    5253        if (!value) 
    5354        { 
    54                 if (ferror(stream)) 
    55                         longjmp(environment, 1); 
    56                 else 
     55                char userescaped[userlength * 2 + 1], passwordescaped[passwordlength * 2 + 1]; 
     56                unsigned long userescapedlength = mysql_real_escape_string(mysql, userescaped, user, userlength), passwordescapedlength = mysql_real_escape_string(mysql, passwordescaped, password, passwordlength); 
     57 
     58                char *hosts[] = { "localhost", "%" }; 
     59 
     60                for (unsigned index = 0; index != sizeof (hosts) / sizeof (*hosts); ++index) 
    5761                { 
    58                         clearerr(stream); 
     62                        char *host = hosts[index]; 
     63                        size_t hostlength = strlen(host); 
     64                        char create[userescapedlength + hostlength + passwordescapedlength + 35], grant[userescapedlength * 2 + hostlength + 31]; 
    5965 
    60                         if (stream == stdin) 
    61                                 printf("\n"); 
    62  
    63                         return ""; 
     66                        sprintf(create, "create user '%s'@'%s' identified by '%s'", userescaped, host, passwordescaped); 
     67                        mysqlcheck(mysql_query(mysql, create), environment); 
     68                        sprintf(grant, "grant all on `%s\\_%%`.* to '%s'@'%s'", userescaped, userescaped, host); 
     69                        mysqlcheck(mysql_query(mysql, grant), environment); 
    6470                } 
    6571        } 
     72        else if (value < 0) 
     73                longjmp(environment, MYSQL_EXCEPTION); 
    6674 
    67         return value; 
     75        mysqlcheck(mysql_stmt_close(statement), environment); 
    6876} 
    69  
    70 void authenticate(const char *program, jmp_buf environment) 
    71 { 
    72         struct passwd *entry = getpwuid(getuid()); 
    73  
    74         if (!entry) 
    75                 longjmp(environment, 1); 
    76  
    77         char name[strlen(entry->pw_name) + 36]; 
    78  
    79         sprintf(name, "uid=%s,ou=People,dc=ccs,dc=ucsb,dc=edu", entry->pw_name); 
    80         umask(022); 
    81  
    82         char *password = getpass("Password: "); 
    83  
    84         if (!strlen(password)) 
    85                 longjmp(environment, (exception = "Empty password", 3)); 
    86  
    87         char secret[] = "/tmp/secret.XXXXXX"; 
    88  
    89         putpassword(password, secret, environment); 
    90  
    91         int pipe_[2]; 
    92  
    93         check(pipe(pipe_), environment); 
    94  
    95         pid_t ldapwhoami; 
    96  
    97         if (!(ldapwhoami = check(fork(), environment))) 
    98         { 
    99                 check(dup2(pipe_[1], 1), environment); 
    100                 check(close(pipe_[0]), environment); 
    101                 check(execl(LDAPWHOAMI, program, "-D", name, "-H", "ldaps://" MASTER, "-W", "-x", "-y", secret, NULL), environment); 
    102         } 
    103  
    104         check(close(pipe_[1]), environment); 
    105  
    106         FILE *ldapwhoami_ = fdopen(pipe_[0], "r"); 
    107         size_t size; 
    108         char *whoami_ = fcheck(fgetln(ldapwhoami_, &size), ldapwhoami_, environment); 
    109         int status; 
    110  
    111         check(waitpid(ldapwhoami, &status, 0), environment); 
    112         check(unlink(secret), environment); 
    113  
    114         if (WEXITSTATUS(status)) 
    115                 longjmp(environment, (exception = "Sorry", 3)); 
    116  
    117         char whoami[size]; 
    118  
    119         strlcpy(whoami, whoami_, size); 
    120  
    121         whoami_ = malloc(sizeof (name) + 3); 
    122  
    123         sprintf(whoami_, "dn:%s", name); 
    124  
    125         if (strcmp(whoami, whoami_)) 
    126                 longjmp(environment, (exception = "Sorry", 3)); 
    127  
    128         free(whoami_); 
    129 } 
    130  
    131 void getpassword(char password[_PASSWORD_LEN], jmp_buf environment) 
    132 { 
    133         strcpy(password, getpass("New Password: ")); 
    134  
    135         if (!strlen(password)) 
    136                 longjmp(environment, (exception = "Empty password", 3)); 
    137  
    138         if (strcmp(password, getpass("Confirm Password: "))) 
    139                 longjmp(environment, (exception = "Password and confirm password mismatched", 3)); 
    140 } 
    141  
    142 void putpassword(char password[_PASSWORD_LEN], char *name, jmp_buf environment) 
    143 { 
    144         int file = check(mkstemp(name), environment); 
    145  
    146         check(write(file, password, strlen(password)), environment); 
    147         check(close(file), environment); 
    148 } 
    149  
    150 void get(const char *prompt, regex_t *regex, char **string, jmp_buf environment) 
    151 { 
    152         do 
    153         { 
    154                 printf("%s: ", prompt); 
    155  
    156                 size_t size; 
    157                 char *string_ = fcheck(fgetln(stdin, &size), stdin, environment); 
    158  
    159                 *string = *string ? realloc(*string, size) : malloc(size); 
    160  
    161                 strlcpy(*string, string_, size); 
    162         } 
    163         while (regcheck(regexec(regex, *string, 0, NULL, 0), regex, environment)); 
    164 } 
    165  
    166 void setshells(Shells *shells, jmp_buf environment) 
    167 { 
    168 #       define setshell(os) \ 
    169         { \ 
    170                 char bash[sizeof (#os)] = #os; \ 
    171                 \ 
    172                 for (unsigned index = 0; index != sizeof (#os) - 1; ++index) \ 
    173                         bash[index] = toupper(bash[index]); \ 
    174                 \ 
    175                 check(setenv(bash, shells->os, 1), environment); \ 
    176         } 
    177  
    178         setshell(shell); 
    179         setshell(freebsd); 
    180         setshell(linux); 
    181         setshell(darwin); 
    182         setshell(solaris); 
    183         setshell(netbsd); 
    184         setshell(debian); 
    185  
    186 #       undef setshell 
    187 } 
    188  
    189 #if !defined(__FreeBSD__) && !defined(__sun__) && !defined(__APPLE__) 
    190 size_t strlcpy(char *dst, const char *src, size_t size) 
    191 { 
    192         dst[size - 1] = '\0'; 
    193  
    194         return strlen(strncpy(dst, src, size - 1)); 
    195 } 
    196  
    197 size_t strlcat(char *dst, const char *src, size_t size) 
    198 { 
    199         dst[size - 1] = '\0'; 
    200  
    201         size_t size_ = strlen(dst); 
    202  
    203         return strlen(strncpy(dst + size_, src, size - size_ - 1)); 
    204 } 
    205  
    206 char *fgetln(FILE * restrict stream, size_t * restrict len) 
    207 { 
    208         static char *line = NULL; 
    209         static size_t size; 
    210  
    211         if ((*len = getline(&line, &size, stream)) == -1) 
    212                 return NULL; 
    213  
    214         return line; 
    215 } 
    216 #elif defined(__sun__) 
    217 char *fgetln(FILE * restrict stream, size_t * restrict len) 
    218 { 
    219         static char *line = NULL; 
    220  
    221         *len = 0; 
    222         line = line ? realloc(line, 1) : malloc(1); 
    223  
    224         while (line[*len] = getc(stream), line[(*len)++] != '\n') 
    225                 if (line[*len - 1] != EOF) 
    226                         line = realloc(line, *len + 1); 
    227                 else 
    228                         return NULL; 
    229  
    230         return line; 
    231 } 
    232  
    233 int setenv(const char *name, const char *value, int overwrite) 
    234 { 
    235         char *string_ = getenv(name); 
    236  
    237         if (overwrite || !string_) 
    238         { 
    239                 char *string = malloc(strlen(name) + strlen(value) + 2); 
    240  
    241                 sprintf(string, "%s=%s", name, value); 
    242  
    243                 int value_ = putenv(string); 
    244  
    245                 if (!string_) 
    246                         free(string_); 
    247  
    248                 return value_; 
    249         } 
    250  
    251         return 0; 
    252 } 
    253 #endif 
  • admin/mysql.h

    r264 r438  
    66// $Id$ 
    77 
    8 #ifndef _common_h_ 
    9 #define _common_h_ 
     8#ifndef _mysql_h_ 
     9#define _mysql_h_ 
    1010 
    11 #ifdef linux 
    12 #undef linux 
    13 #endif 
     11#include "common.h" 
    1412 
    15 #if !defined(__FreeBSD__) && !defined(__sun__) && !defined(__APPLE__) 
    16 #ifndef _BSD_SOURCE 
    17 #define _BSD_SOURCE 
    18 #endif 
    19 #ifndef _POSIX_SOURCE 
    20 #define _POSIX_SOURCE 
    21 #endif 
    22 #ifndef _GNU_SOURCE 
    23 #define _GNU_SOURCE 
    24 #endif 
    25 #elif defined(__sun__) 
    26 #define __EXTENSIONS__ 
    27 #endif 
     13#include <assert.h> 
     14#include <mysql.h> 
    2815 
    29 #include <pwd.h> 
    30 #include <regex.h> 
    31 #include <setjmp.h> 
    32 #include <stdio.h> 
    33 #include <stdlib.h> 
    34 #include <string.h> 
    35 #include <sys/param.h> 
    36 #include <sys/stat.h> 
    37 #include <sys/wait.h> 
    38 #include <unistd.h> 
     16#define MYSQL_EXCEPTION 3 
    3917 
    40 #ifndef _PASSWORD_LEN 
    41 #define _PASSWORD_LEN BUFSIZ 
    42 #endif 
     18#define MYSQL_BEGIN() MYSQL *mysql = mysql_init(NULL); \ 
     19        \ 
     20        assert(mysql != NULL) 
    4321 
    44 #ifndef MAXLOGNAME 
    45 #define MAXLOGNAME 17 
    46 #endif 
     22#define MYSQL_END() mysql_close(mysql); \ 
     23        mysql_library_end() 
    4724 
    48 extern char *exception; 
     25void mysqlcheck(int value, jmp_buf environment); 
     26void mysqlpassword(MYSQL *mysql, char user[MAXLOGNAME], char password[_PASSWORD_LEN], jmp_buf environment); 
    4927 
    50 typedef struct 
    51 { 
    52         const char *shell; 
    53         char *freebsd, *linux, *darwin, *solaris, *netbsd, *debian; 
    54 } 
    55 Shells; 
    56  
    57 typedef enum { sh, csh, tcsh, bash, ksh, zsh, _3sh, custom } Shell; 
    58  
    59 extern Shells shells[]; 
    60  
    61 int check(int value, jmp_buf environment); 
    62 int regcheck(int value, const regex_t *regex, jmp_buf environment); 
    63 char *fcheck(char *value, FILE *stream, jmp_buf environment); 
    64 void authenticate(const char *program, jmp_buf environment); 
    65 void getpassword(char password[_PASSWORD_LEN], jmp_buf environment); 
    66 void putpassword(char password[_PASSWORD_LEN], char *name, jmp_buf environment); 
    67 void get(const char *prompt, regex_t *regex, char **string, jmp_buf environment); 
    68 void setshells(Shells *shells, jmp_buf environment); 
    69  
    70 #if !defined(__FreeBSD__) && !defined(__sun__) && !defined(__APPLE__) 
    71 size_t strlcpy(char *dst, const char *src, size_t size); 
    72 size_t strlcat(char *dst, const char *src, size_t size); 
    73 char *fgetln(FILE * restrict stream, size_t * restrict len); 
    74 #elif defined(__sun__) 
    75 char *fgetln(FILE * restrict stream, size_t * restrict len); 
    76 int setenv(const char *name, const char *value, int overwrite); 
    77 #endif 
    78  
    79 #endif//_common_h_ 
     28#endif//_mysql_h_ 
  • admin/passwd.c

    r264 r438  
    77 
    88#include "common.h" 
     9 
     10#ifdef _Master_ 
     11#include "mysql.h" 
     12#endif 
    913 
    1014int main(int argc, char *argv[]) 
     
    1721        jmp_buf environment; 
    1822 
     23#ifdef _Master_ 
     24        MYSQL_BEGIN(); 
     25#endif 
     26 
    1927        switch (setjmp(environment)) 
    2028        { 
    2129        case 0: 
    2230                break; 
    23         case 1: 
     31        case POSIX_EXCEPTION: 
    2432                perror(argv[0]); 
    2533 
    26                 return 1; 
    27         default: 
     34                goto end; 
     35        case STRING_EXCEPTION: 
    2836                fprintf(stderr, "%s: %s\n", argv[0], exception); 
     37#ifdef _Master_ 
    2938 
     39                goto end; 
     40        case MYSQL_EXCEPTION: 
     41                fprintf(stderr, "%s: %s\n", argv[0], mysql_error(mysql)); 
     42#endif 
     43 
     44end: 
     45#ifdef _Master_ 
     46                MYSQL_END(); 
     47 
     48#endif 
    3049                return 1; 
    3150        } 
     
    5069                                printf("Usage %s -user=user\n", argv[0]); 
    5170 
    52                                 return 1; 
     71                                goto end; 
    5372                        } 
    5473                } 
     
    5978                        goto on; 
    6079                else 
    61                         longjmp(environment, (exception = "Cannot change root password", 3)); 
     80                        longjmp(environment, (exception = "Cannot change root password", STRING_EXCEPTION)); 
    6281        } 
    6382        else if (argc != 1) 
     
    6584                printf("Usage: %s\n", argv[0]); 
    6685 
    67                 return 1; 
     86                goto end; 
    6887        } 
    6988 
     
    7190 
    7291        if (!entry) 
    73                 longjmp(environment, 1); 
     92                longjmp(environment, POSIX_EXCEPTION); 
    7493 
    7594        strlcpy(user, entry->pw_name, sizeof (user)); 
     
    105124 
    106125        if (fprintf(smbpasswd, "%s\n%s\n", password, password) < 0) 
    107                 longjmp(environment, 1); 
     126                longjmp(environment, POSIX_EXCEPTION); 
    108127 
    109128        if (fclose(smbpasswd)) 
    110                 longjmp(environment, 1); 
     129                longjmp(environment, POSIX_EXCEPTION); 
    111130 
    112131        int status; 
     
    116135 
    117136        if (WEXITSTATUS(status)) 
    118                 return 1; 
     137                goto end; 
     138 
     139        mysqlpassword(mysql, user, password, environment); 
     140        MYSQL_END(); 
    119141#else 
    120         longjmp(environment, (exception = "Log in to " MASTER " to change password", 3)); 
     142        longjmp(environment, (exception = "Log in to " MASTER " to change password", STRING_EXCEPTION)); 
    121143#endif 
    122144 
  • admin/secret.c

    r264 r438  
    66// $Id$ 
    77 
     8#include "common.h" 
     9 
    810#include <fcntl.h> 
    9  
    10 #include "common.h" 
    1111 
    1212int main(int argc, char *argv[]) 
     
    1818        case 0: 
    1919                break; 
    20         case 1: 
     20        case POSIX_EXCEPTION: 
    2121                perror(argv[0]); 
    2222 
    2323                return 1; 
    24         default: 
     24        case STRING_EXCEPTION: 
    2525                fprintf(stderr, "%s: %s\n", argv[0], exception); 
    2626 
     
    3232        getpassword(password, environment); 
    3333 
    34         int file = check(open("/ccs/etc/secret", O_WRONLY | O_TRUNC), environment); 
     34        int file = check(open(SECRET, O_WRONLY | O_TRUNC), environment); 
    3535 
    3636        check(write(file, password, strlen(password)), environment); 
  • admin/shells.c

    r264 r438  
    66// $Id$ 
    77 
    8 #include <stdbool.h> 
    9  
    108#include "common.h" 
    119 
    1210#include <assert.h> 
    1311#include <ctype.h> 
     12#include <stdbool.h> 
    1413 
    1514int main(int argc, char *argv[]) 
     
    2120        case 0: 
    2221                break; 
    23         case 1: 
     22        case POSIX_EXCEPTION: 
    2423                perror(argv[0]); 
    2524 
    2625                return 1; 
    27         default: 
     26        case STRING_EXCEPTION: 
    2827                fprintf(stderr, "%s: %s\n", argv[0], exception); 
    2928 
     
    162161#                       undef checkshells 
    163162 
    164                         longjmp(environment, (exception = "Unknown shell layout", 3)); 
     163                        longjmp(environment, (exception = "Unknown shell layout", STRING_EXCEPTION)); 
    165164 
    166165on:                     for (Shell shell = sh; shell != _3sh; ++shell) 
  • admin/turnin.c

    r264 r438  
    1616        case 0: 
    1717                break; 
    18         case 1: 
     18        case POSIX_EXCEPTION: 
    1919                perror(argv[0]); 
    2020 
    2121                return 1; 
    22         default: 
     22        case STRING_EXCEPTION: 
    2323                fprintf(stderr, "%s: %s\n", argv[0], exception); 
    2424 
     
    2626        } 
    2727 
    28         longjmp(environment, (exception = "Um, so, yeah, I'm guessing you want to be logged in to a CSIL box.", 3)); 
     28        longjmp(environment, (exception = "Um, so, yeah, I'm guessing you want to be logged in to a CSIL box.", STRING_EXCEPTION)); 
    2929 
    3030        return 0; 
  • admin/users.h

    r415 r438  
    9191                "plugdev", 
    9292                "polkit", 
     93                "polkituser", 
    9394                "pop", 
    9495                "portage", 
     
    9899                "postmaster", 
    99100                "power", 
     101                "powerdev", 
    100102                "proxy", 
     103                "pulse", 
    101104                "qmail", 
    102105                "qmaild", 
     
    110113                "rpc", 
    111114                "sambashare", 
     115                "saned", 
    112116                "sashroot", 
    113117                "sasl",