summaryrefslogtreecommitdiffstats
path: root/login-utils/setpwnam.c
diff options
context:
space:
mode:
authorSami Kerola2011-11-13 19:54:14 +0100
committerSami Kerola2011-11-29 17:58:00 +0100
commit7ff9c2aa8fd985a6c48241ce955c2ea30e7b635e (patch)
treecdfc1600a017420580f641de719957e4e1027240 /login-utils/setpwnam.c
parentselinux_utils: fix coding style (diff)
downloadkernel-qcow2-util-linux-7ff9c2aa8fd985a6c48241ce955c2ea30e7b635e.tar.gz
kernel-qcow2-util-linux-7ff9c2aa8fd985a6c48241ce955c2ea30e7b635e.tar.xz
kernel-qcow2-util-linux-7ff9c2aa8fd985a6c48241ce955c2ea30e7b635e.zip
setpwnam: fix coding style
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'login-utils/setpwnam.c')
-rw-r--r--login-utils/setpwnam.c333
1 files changed, 169 insertions, 164 deletions
diff --git a/login-utils/setpwnam.c b/login-utils/setpwnam.c
index 92d1ac90f..d5b8a5bea 100644
--- a/login-utils/setpwnam.c
+++ b/login-utils/setpwnam.c
@@ -1,6 +1,5 @@
/*
- * setpwnam.c --
- * edit an entry in a password database.
+ * setpwnam.c -- edit an entry in a password database.
*
* (c) 1994 Salvatore Valente <svalente@mit.edu>
* This file is free software; you can redistribute it and/or
@@ -9,31 +8,30 @@
* License, or (at your option) any later version.
*
* Edited 11/10/96 (DD/MM/YY ;-) by Nicolai Langfeldt (janl@math.uio.no)
- * to read /etc/passwd directly so that passwd, chsh and chfn can work
- * on machines that run NIS (né YP). Changes will not be made to
- * usernames starting with +.
- *
+ * to read /etc/passwd directly so that passwd, chsh and chfn can work on
+ * machines that run NIS (né YP). Changes will not be made to usernames
+ * starting with +.
+ *
* This file is distributed with no warranty.
*
* Usage:
* 1) get a struct passwd * from getpwnam().
- * You should assume a struct passwd has an infinite number of fields,
- * so you should not try to create one from scratch.
+ * You should assume a struct passwd has an infinite number of fields, so
+ * you should not try to create one from scratch.
* 2) edit the fields you want to edit.
* 3) call setpwnam() with the edited struct passwd.
*
- * A _normal user_ program should never directly manipulate
- * /etc/passwd but use getpwnam() and (family, as well as)
- * setpwnam().
+ * A _normal user_ program should never directly manipulate etc/passwd but
+ * /use getpwnam() and (family, as well as) setpwnam().
*
- * But, setpwnam was made to _edit_ the password file. For use by
- * chfn, chsh and passwd. _I_ _HAVE_ to read and write /etc/passwd
- * directly. Let those who say nay be forever silent and think about
- * how getpwnam (and family) works on a machine running YP.
+ * But, setpwnam was made to _edit_ the password file. For use by chfn,
+ * chsh and passwd. _I_ _HAVE_ to read and write /etc/passwd directly. Let
+ * those who say nay be forever silent and think about how getpwnam (and
+ * family) works on a machine running YP.
*
- * Added checks for failure of malloc() and removed error reporting
- * to stderr, this is a library function and should not print on the
- * screen, but return appropriate error codes.
+ * Added checks for failure of malloc() and removed error reporting to
+ * stderr, this is a library function and should not print on the screen,
+ * but return appropriate error codes.
* 27-Jan-97 - poe@daimi.aau.dk
*
* Thanks to "two guys named Ian".
@@ -41,27 +39,26 @@
* $Author: poer $
* $Revision: 1.13 $
* $Date: 1997/06/23 08:26:29 $
- *
*/
#undef DEBUG
-#include <sys/types.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
+#include <errno.h>
#include <fcntl.h>
+#include <paths.h>
#include <pwd.h>
-#include <errno.h>
#include <signal.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <sys/resource.h>
#include <sys/stat.h>
-#include <paths.h>
-#include <stdbool.h>
+#include <sys/types.h>
+#include <unistd.h>
-#include "setpwnam.h"
#include "c.h"
+#include "setpwnam.h"
static void pw_init(void);
@@ -71,155 +68,163 @@ static void pw_init(void);
* If the given username exists in the passwd file, the entry is
* replaced with the given entry.
*/
-int
-setpwnam (struct passwd *pwd)
+int setpwnam(struct passwd *pwd)
{
- FILE *fp = NULL, *pwf = NULL;
- int x, save_errno, fd, ret;
- int found;
- int oldumask;
- int namelen;
- int buflen = 256;
- int contlen;
- char *linebuf = NULL;
-
- oldumask = umask(0); /* Create with exact permissions */
-
- pw_init();
-
- /* sanity check */
- for (x = 0; x < 3; x++) {
- if (x > 0) sleep(1);
- fd = open(PTMPTMP_FILE, O_WRONLY|O_CREAT|O_EXCL, 0644);
- if (fd == -1) {
- umask(oldumask);
- return -1;
+ FILE *fp = NULL, *pwf = NULL;
+ int x, save_errno, fd, ret;
+ int found;
+ int oldumask;
+ int namelen;
+ int buflen = 256;
+ int contlen;
+ char *linebuf = NULL;
+
+ oldumask = umask(0); /* Create with exact permissions */
+
+ pw_init();
+
+ /* sanity check */
+ for (x = 0; x < 3; x++) {
+ if (x > 0)
+ sleep(1);
+ fd = open(PTMPTMP_FILE, O_WRONLY | O_CREAT | O_EXCL, 0644);
+ if (fd == -1) {
+ umask(oldumask);
+ return -1;
+ }
+ ret = link(PTMPTMP_FILE, PTMP_FILE);
+ unlink(PTMPTMP_FILE);
+ if (ret == -1)
+ close(fd);
+ else
+ break;
}
- ret = link(PTMPTMP_FILE, PTMP_FILE);
- unlink(PTMPTMP_FILE);
+ umask(oldumask);
if (ret == -1)
- close(fd);
- else
- break;
- }
- umask(oldumask);
- if (ret == -1) return -1;
-
- /* ptmp should be owned by root.root or root.wheel */
- if (chown(PTMP_FILE, (uid_t) 0, (gid_t) 0) < 0) return -1;
-
- /* open ptmp for writing and passwd for reading */
- fp = fdopen(fd, "w");
- if (!fp) goto fail;
-
- pwf = fopen(PASSWD_FILE, "r");
- if (!pwf) goto fail;
-
- namelen = strlen(pwd->pw_name);
-
- linebuf = malloc(buflen);
- if (!linebuf) goto fail;
-
- /* parse the passwd file */
- found = false;
- /* Do you wonder why I don't use getpwent? Read comments at top of file */
- while (fgets(linebuf, buflen, pwf) != NULL) {
- contlen = strlen(linebuf);
- while (linebuf[contlen-1] != '\n' && !feof(pwf)) {
- char *tmp;
-
- /* Extend input buffer if it failed getting the whole line */
-
- /* So now we double the buffer size */
- buflen *= 2;
-
- tmp = realloc(linebuf, buflen);
- if (tmp== NULL) goto fail;
- linebuf = tmp;
-
- /* And fill the rest of the buffer */
- if (fgets(&linebuf[contlen], buflen/2, pwf) == NULL) break;
- contlen = strlen(linebuf);
-
- /* That was a lot of work for nothing. Gimme perl! */
+ return -1;
+
+ /* ptmp should be owned by root.root or root.wheel */
+ if (chown(PTMP_FILE, (uid_t) 0, (gid_t) 0) < 0)
+ return -1;
+
+ /* open ptmp for writing and passwd for reading */
+ fp = fdopen(fd, "w");
+ if (!fp)
+ goto fail;
+
+ pwf = fopen(PASSWD_FILE, "r");
+ if (!pwf)
+ goto fail;
+
+ namelen = strlen(pwd->pw_name);
+
+ linebuf = malloc(buflen);
+ if (!linebuf)
+ goto fail;
+
+ /* parse the passwd file */
+ found = false;
+
+ /* Do you wonder why I don't use getpwent? Read comments at top of
+ * file */
+ while (fgets(linebuf, buflen, pwf) != NULL) {
+ contlen = strlen(linebuf);
+ while (linebuf[contlen - 1] != '\n' && !feof(pwf)) {
+ char *tmp;
+ /* Extend input buffer if it failed getting the whole line,
+ * so now we double the buffer size */
+ buflen *= 2;
+ tmp = realloc(linebuf, buflen);
+ if (tmp == NULL)
+ goto fail;
+ linebuf = tmp;
+ /* And fill the rest of the buffer */
+ if (fgets(&linebuf[contlen], buflen / 2, pwf) == NULL)
+ break;
+ contlen = strlen(linebuf);
+ /* That was a lot of work for nothing. Gimme perl! */
+ }
+
+ /* Is this the username we were sent to change? */
+ if (!found && linebuf[namelen] == ':' &&
+ !strncmp(linebuf, pwd->pw_name, namelen)) {
+ /* Yes! So go forth in the name of the Lord and
+ * change it! */
+ if (putpwent(pwd, fp) < 0)
+ goto fail;
+ found = true;
+ continue;
+ }
+ /* Nothing in particular happened, copy input to output */
+ fputs(linebuf, fp);
}
- /* Is this the username we were sent to change? */
- if (!found && linebuf[namelen] == ':' &&
- !strncmp(linebuf, pwd->pw_name, namelen)) {
- /* Yes! So go forth in the name of the Lord and change it! */
- if (putpwent(pwd, fp) < 0) goto fail;
- found = true;
- continue;
+ if (fclose(fp) < 0)
+ goto fail;
+ fp = NULL;
+ close(fd);
+ fd = -1;
+ fclose(pwf); /* I don't think I want to know if this failed */
+ pwf = NULL;
+
+ if (!found) {
+ errno = ENOENT; /* give me something better */
+ goto fail;
}
- /* Nothing in particular happened, copy input to output */
- fputs(linebuf, fp);
- }
-
- if (fclose(fp) < 0) goto fail;
- fp = NULL;
- close (fd);
- fd = -1;
- fclose (pwf); /* I don't think I want to know if this failed */
- pwf = NULL;
-
- if (!found) {
- errno = ENOENT; /* give me something better */
- goto fail;
- }
-
- /* we don't care if we can't remove the backup file */
- unlink(PASSWD_FILE".OLD");
- /* we don't care if we can't create the backup file */
- ignore_result( link(PASSWD_FILE, PASSWD_FILE".OLD") );
- /* we DO care if we can't rename to the passwd file */
- if(rename(PTMP_FILE, PASSWD_FILE) < 0)
- goto fail;
- /* finally: success */
- return 0;
-
-fail:
- save_errno = errno;
- if (fp != NULL) fclose (fp);
- if (pwf != NULL) fclose(pwf);
- if (fd >= 0) close (fd);
- free(linebuf);
- unlink(PTMP_FILE);
- errno = save_errno;
- return -1;
+
+ /* we don't care if we can't remove the backup file */
+ unlink(PASSWD_FILE ".OLD");
+ /* we don't care if we can't create the backup file */
+ ignore_result(link(PASSWD_FILE, PASSWD_FILE ".OLD"));
+ /* we DO care if we can't rename to the passwd file */
+ if (rename(PTMP_FILE, PASSWD_FILE) < 0)
+ goto fail;
+ /* finally: success */
+ return 0;
+
+ fail:
+ save_errno = errno;
+ if (fp != NULL)
+ fclose(fp);
+ if (pwf != NULL)
+ fclose(pwf);
+ if (fd >= 0)
+ close(fd);
+ free(linebuf);
+ unlink(PTMP_FILE);
+ errno = save_errno;
+ return -1;
}
/* Set up the limits so that we're not foiled */
-
-static void
-pw_init()
+static void pw_init()
{
- struct rlimit rlim;
+ struct rlimit rlim;
- /* Unlimited resource limits. */
- rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
- setrlimit(RLIMIT_CPU, &rlim);
- setrlimit(RLIMIT_FSIZE, &rlim);
- setrlimit(RLIMIT_STACK, &rlim);
- setrlimit(RLIMIT_DATA, &rlim);
- setrlimit(RLIMIT_RSS, &rlim);
+ /* Unlimited resource limits. */
+ rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
+ setrlimit(RLIMIT_CPU, &rlim);
+ setrlimit(RLIMIT_FSIZE, &rlim);
+ setrlimit(RLIMIT_STACK, &rlim);
+ setrlimit(RLIMIT_DATA, &rlim);
+ setrlimit(RLIMIT_RSS, &rlim);
#ifndef DEBUG
- /* Don't drop core (not really necessary, but GP's). */
- rlim.rlim_cur = rlim.rlim_max = 0;
- setrlimit(RLIMIT_CORE, &rlim);
+ /* Don't drop core (not really necessary, but GP's). */
+ rlim.rlim_cur = rlim.rlim_max = 0;
+ setrlimit(RLIMIT_CORE, &rlim);
#endif
- /* Turn off signals. */
- signal(SIGALRM, SIG_IGN);
- signal(SIGHUP, SIG_IGN);
- signal(SIGINT, SIG_IGN);
- signal(SIGPIPE, SIG_IGN);
- signal(SIGQUIT, SIG_IGN);
- signal(SIGTERM, SIG_IGN);
- signal(SIGTSTP, SIG_IGN);
- signal(SIGTTOU, SIG_IGN);
-
- /* Create with exact permissions. */
- umask(0);
+ /* Turn off signals. */
+ signal(SIGALRM, SIG_IGN);
+ signal(SIGHUP, SIG_IGN);
+ signal(SIGINT, SIG_IGN);
+ signal(SIGPIPE, SIG_IGN);
+ signal(SIGQUIT, SIG_IGN);
+ signal(SIGTERM, SIG_IGN);
+ signal(SIGTSTP, SIG_IGN);
+ signal(SIGTTOU, SIG_IGN);
+
+ /* Create with exact permissions. */
+ umask(0);
}