chiark / gitweb /
don't fatal() if cannot create a root account
[disorder] / server / setup.c
CommitLineData
36be7e6a
RK
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 */
45void 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 */
b9ea01d8
RK
69 if((fd = open(privconfignew, O_WRONLY|O_CREAT, 0600)) < 0) {
70 error(errno, "error creating %s", privconfignew);
71 return; /* not fatal! */
72 }
36be7e6a
RK
73 /* Fix permissions */
74 if(pw) {
75 if(fchown(fd, 0, pw->pw_gid) < 0)
76 fatal(errno, "error setting owner/group for %s", privconfignew);
77 if(fchmod(fd, 0640) < 0)
78 fatal(errno, "error setting permissions for %s", privconfignew);
79 }
80 /* Write the required 'allow' line */
81 if(!(fp = fdopen(fd, "w")))
82 fatal(errno, "fdopen");
83 if(fprintf(fp, "allow root %s\n", pwhex) < 0
84 || fclose(fp) < 0)
85 fatal(errno, "error writing %s", privconfignew);
86 /* Rename into place */
87 if(rename(privconfignew, privconfig) < 0)
88 fatal(errno, "error renaming %s", privconfignew);
89}
90
91/*
92Local Variables:
93c-basic-offset:2
94comment-column:40
95fill-column:79
96indent-tabs-mode:nil
97End:
98*/