2 * This file is part of DisOrder.
3 * Copyright (C) 2007 Richard Kettlewell
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.
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.
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
20 /** @file server/setup.c
21 * @brief Automated setup functions
31 #include <sys/types.h>
39 #include "configuration.h"
44 /** @brief Create config.private with a login for root */
45 void make_root_login(void) {
47 char *privconfig
, *privconfignew
;
55 if(!(pw
= getpwnam(config
->user
)))
56 fatal(0, "cannot find user %s", config
->user
);
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)
65 /* Choose a new root password */
66 gcry_randomize(pwbin
, sizeof pwbin
, GCRY_STRONG_RANDOM
);
67 pwhex
= hex(pwbin
, sizeof pwbin
);
69 if((fd
= open(privconfignew
, O_WRONLY
|O_CREAT
, 0600)) < 0)
70 fatal(errno
, "error creating %s", privconfignew
);
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
);
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
83 fatal(errno
, "error writing %s", privconfignew
);
84 /* Rename into place */
85 if(rename(privconfignew
, privconfig
) < 0)
86 fatal(errno
, "error renaming %s", privconfignew
);