]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lppasswd.c
2 * "$Id: lppasswd.c 6649 2007-07-11 21:46:42Z mike $"
4 * MD5 password program for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * main() - Add, change, or delete passwords from the MD5 password file.
18 * usage() - Show program usage.
22 * Include necessary headers...
31 #include <sys/types.h>
34 #include <cups/globals.h>
56 static void usage(FILE *fp
);
60 * 'main()' - Add, change, or delete passwords from the MD5 password file.
63 int /* O - Exit status */
64 main(int argc
, /* I - Number of command-line arguments */
65 char *argv
[]) /* I - Command-line arguments */
67 int i
; /* Looping var */
68 char *opt
; /* Option pointer */
69 const char *username
; /* Pointer to username */
70 const char *groupname
; /* Pointer to group name */
71 int op
; /* Operation (add, change, delete) */
72 const char *passwd
; /* Password string */
73 FILE *infile
, /* Input file */
74 *outfile
; /* Output file */
75 char line
[256], /* Line from file */
76 userline
[17], /* User from line */
77 groupline
[17], /* Group from line */
78 md5line
[33], /* MD5-sum from line */
79 md5new
[33]; /* New MD5 sum */
80 char passwdmd5
[1024], /* passwd.md5 file */
81 passwdold
[1024], /* passwd.old file */
82 passwdnew
[1024]; /* passwd.tmp file */
83 char *newpass
, /* new password */
84 *oldpass
; /* old password */
85 int flag
; /* Password check flags... */
86 int fd
; /* Password file descriptor */
87 int error
; /* Write error */
88 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
89 cups_lang_t
*lang
; /* Language info */
90 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
91 struct sigaction action
; /* Signal action */
92 #endif /* HAVE_SIGACTION && !HAVE_SIGSET*/
96 lang
= cupsLangDefault();
99 * Check to see if stdin, stdout, and stderr are still open...
102 if (fcntl(0, F_GETFD
, &i
) ||
103 fcntl(1, F_GETFD
, &i
) ||
104 fcntl(2, F_GETFD
, &i
))
107 * No, return exit status 2 and don't try to send any output since
108 * someone is trying to bypass the security on the server.
115 * Find the server directory...
118 snprintf(passwdmd5
, sizeof(passwdmd5
), "%s/passwd.md5", cg
->cups_serverroot
);
119 snprintf(passwdold
, sizeof(passwdold
), "%s/passwd.old", cg
->cups_serverroot
);
120 snprintf(passwdnew
, sizeof(passwdnew
), "%s/passwd.new", cg
->cups_serverroot
);
123 * Find the default system group...
126 if (getgrnam(CUPS_DEFAULT_GROUP
))
127 groupname
= CUPS_DEFAULT_GROUP
;
129 groupname
= "unknown";
137 * Parse command-line options...
140 for (i
= 1; i
< argc
; i
++)
141 if (argv
[i
][0] == '-')
142 for (opt
= argv
[i
] + 1; *opt
; opt
++)
148 case 'x' : /* Delete */
151 case 'g' : /* Group */
158 case 'h' : /* Help */
161 default : /* Bad option */
171 * See if we are trying to add or delete a password when we aren't logged in
175 if (getuid() && getuid() != geteuid() && (op
!= CHANGE
|| username
))
177 _cupsLangPuts(stderr
,
178 _("lppasswd: Only root can add or delete passwords\n"));
183 * Fill in missing info...
187 username
= cupsUser();
189 oldpass
= newpass
= NULL
;
192 * Obtain old and new password _before_ locking the database
193 * to keep users from locking the file indefinitely.
196 if (op
== CHANGE
&& getuid())
198 if ((passwd
= cupsGetPassword(_("Enter old password:"))) == NULL
)
201 if ((oldpass
= strdup(passwd
)) == NULL
)
203 _cupsLangPrintf(stderr
,
204 _("lppasswd: Unable to copy password string: %s\n"),
211 * Now get the new password, if necessary...
216 if ((passwd
= cupsGetPassword(
217 _cupsLangString(lang
, _("Enter password:")))) == NULL
)
220 if ((newpass
= strdup(passwd
)) == NULL
)
222 _cupsLangPrintf(stderr
,
223 _("lppasswd: Unable to copy password string: %s\n"),
228 if ((passwd
= cupsGetPassword(
229 _cupsLangString(lang
, _("Enter password again:")))) == NULL
)
232 if (strcmp(passwd
, newpass
) != 0)
234 _cupsLangPuts(stderr
,
235 _("lppasswd: Sorry, passwords don't match\n"));
240 * Check that the password contains at least one letter and number.
245 for (passwd
= newpass
; *passwd
; passwd
++)
246 if (isdigit(*passwd
& 255))
248 else if (isalpha(*passwd
& 255))
252 * Only allow passwords that are at least 6 chars, have a letter and
253 * a number, and don't contain the username.
256 if (strlen(newpass
) < 6 || strstr(newpass
, username
) != NULL
|| flag
!= 3)
258 _cupsLangPuts(stderr
,
259 _("lppasswd: Sorry, password rejected.\n"
260 "Your password must be at least 6 characters long, "
262 "your username, and must contain at least one letter "
269 * Ignore SIGHUP, SIGINT, SIGTERM, and SIGXFSZ (if defined) for the
270 * remainder of the time so that we won't end up with bogus password
275 # if defined(HAVE_SIGSET)
276 sigset(SIGHUP
, SIG_IGN
);
277 sigset(SIGINT
, SIG_IGN
);
278 sigset(SIGTERM
, SIG_IGN
);
280 sigset(SIGXFSZ
, SIG_IGN
);
281 # endif /* SIGXFSZ */
282 # elif defined(HAVE_SIGACTION)
283 memset(&action
, 0, sizeof(action
));
284 action
.sa_handler
= SIG_IGN
;
286 sigaction(SIGHUP
, &action
, NULL
);
287 sigaction(SIGINT
, &action
, NULL
);
288 sigaction(SIGTERM
, &action
, NULL
);
290 sigaction(SIGXFSZ
, &action
, NULL
);
291 # endif /* SIGXFSZ */
293 signal(SIGHUP
, SIG_IGN
);
294 signal(SIGINT
, SIG_IGN
);
295 signal(SIGTERM
, SIG_IGN
);
297 signal(SIGXFSZ
, SIG_IGN
);
298 # endif /* SIGXFSZ */
303 * Open the output file.
306 if ((fd
= open(passwdnew
, O_WRONLY
| O_CREAT
| O_EXCL
, 0400)) < 0)
309 _cupsLangPuts(stderr
, _("lppasswd: Password file busy\n"));
311 _cupsLangPrintf(stderr
,
312 _("lppasswd: Unable to open password file: %s\n"),
318 if ((outfile
= fdopen(fd
, "w")) == NULL
)
320 _cupsLangPrintf(stderr
,
321 _("lppasswd: Unable to open password file: %s\n"),
329 setbuf(outfile
, NULL
);
332 * Open the existing password file and create a new one...
335 infile
= fopen(passwdmd5
, "r");
336 if (infile
== NULL
&& errno
!= ENOENT
&& op
!= ADD
)
338 _cupsLangPrintf(stderr
,
339 _("lppasswd: Unable to open password file: %s\n"),
350 * Read lines from the password file; the format is:
352 * username:group:MD5-sum
362 while (fgets(line
, sizeof(line
), infile
) != NULL
)
364 if (sscanf(line
, "%16[^:]:%16[^:]:%32s", userline
, groupline
, md5line
) != 3)
367 if (strcmp(username
, userline
) == 0 &&
368 strcmp(groupname
, groupline
) == 0)
371 if (fputs(line
, outfile
) == EOF
)
373 _cupsLangPrintf(stderr
,
374 _("lppasswd: Unable to write to password file: %s\n"),
383 while (fgets(line
, sizeof(line
), infile
) != NULL
)
384 if (fputs(line
, outfile
) == EOF
)
386 _cupsLangPrintf(stderr
,
387 _("lppasswd: Unable to write to password file: %s\n"),
396 (strcmp(username
, userline
) || strcmp(groupname
, groupline
)))
398 _cupsLangPrintf(stderr
,
399 _("lppasswd: user \"%s\" and group \"%s\" do not exist.\n"),
400 username
, groupname
);
403 else if (op
!= DELETE
)
406 strcmp(httpMD5(username
, "CUPS", oldpass
, md5new
), md5line
) != 0)
408 _cupsLangPuts(stderr
,
409 _("lppasswd: Sorry, password doesn't match\n"));
414 snprintf(line
, sizeof(line
), "%s:%s:%s\n", username
, groupname
,
415 httpMD5(username
, "CUPS", newpass
, md5new
));
416 if (fputs(line
, outfile
) == EOF
)
418 _cupsLangPrintf(stderr
,
419 _("lppasswd: Unable to write to password file: %s\n"),
433 if (fclose(outfile
) == EOF
)
437 * Error out gracefully as needed...
442 _cupsLangPuts(stderr
, _("lppasswd: Password file not updated\n"));
450 * Save old passwd file
454 if (link(passwdmd5
, passwdold
) && errno
!= ENOENT
)
456 _cupsLangPrintf(stderr
,
457 _("lppasswd: failed to backup old password file: %s\n"),
464 * Install new password file
467 if (rename(passwdnew
, passwdmd5
) < 0)
469 _cupsLangPrintf(stderr
,
470 _("lppasswd: failed to rename password file: %s\n"),
481 * 'usage()' - Show program usage.
485 usage(FILE *fp
) /* I - File to send usage to */
488 _cupsLangPuts(fp
, _("Usage: lppasswd [-g groupname]\n"));
491 _("Usage: lppasswd [-g groupname] [username]\n"
492 " lppasswd [-g groupname] -a [username]\n"
493 " lppasswd [-g groupname] -x [username]\n"));
500 * End of "$Id: lppasswd.c 6649 2007-07-11 21:46:42Z mike $".