chiark / gitweb /
shiner about... box
[disorder] / server / setup.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2007 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 /** @file server/setup.c
21  * @brief Automated setup functions
22  */
23
24 #include <config.h>
25 #include "types.h"
26
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <gcrypt.h>
30 #include <pwd.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <errno.h>
34 #include <stdio.h>
35
36 #include "log.h"
37 #include "mem.h"
38 #include "printf.h"
39 #include "configuration.h"
40 #include "setup.h"
41 #include "hex.h"
42 #include "defs.h"
43
44 /** @brief Create config.private with a login for root */
45 void make_root_login(void) {
46   struct stat sb;
47   char *privconfig,  *privconfignew;
48   int fd;
49   FILE *fp;
50   struct passwd *pw;
51   uint8_t pwbin[10];
52   char *pwhex;
53   
54   if(config->user) {
55     if(!(pw = getpwnam(config->user)))
56       fatal(0, "cannot find user %s", config->user);
57   } else
58     pw = 0;
59   /* Compute filenames */
60   byte_xasprintf(&privconfig, "%s/config.private", pkgconfdir);
61   byte_xasprintf(&privconfignew, "%s/config.private.new", pkgconfdir);
62   /* If config.private already exists don't overwrite it */
63   if(stat(privconfig, &sb) == 0)
64     return;
65   /* Choose a new root password */
66   gcry_randomize(pwbin, sizeof pwbin, GCRY_STRONG_RANDOM);
67   pwhex = hex(pwbin, sizeof pwbin);
68   /* Create the file */
69   if((fd = open(privconfignew, O_WRONLY|O_CREAT, 0600)) < 0)
70     fatal(errno, "error creating %s", privconfignew);
71   /* Fix permissions */
72   if(pw) {
73     if(fchown(fd, 0, pw->pw_gid) < 0)
74       fatal(errno, "error setting owner/group for %s", privconfignew);
75     if(fchmod(fd, 0640) < 0)
76       fatal(errno, "error setting permissions for %s", privconfignew);
77   }
78   /* Write the required 'allow' line */
79   if(!(fp = fdopen(fd, "w")))
80     fatal(errno, "fdopen");
81   if(fprintf(fp, "allow root %s\n", pwhex) < 0
82      || fclose(fp) < 0)
83     fatal(errno, "error writing %s", privconfignew);
84   /* Rename into place */
85   if(rename(privconfignew, privconfig) < 0)
86     fatal(errno, "error renaming %s", privconfignew);
87 }
88
89 /*
90 Local Variables:
91 c-basic-offset:2
92 comment-column:40
93 fill-column:79
94 indent-tabs-mode:nil
95 End:
96 */