]>
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 the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 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/string.h>
35 #include <cups/cups.h>
36 #include <cups/i18n.h>
58 static void usage(FILE *fp
);
62 * 'main()' - Add, change, or delete passwords from the MD5 password file.
65 int /* O - Exit status */
66 main(int argc
, /* I - Number of command-line arguments */
67 char *argv
[]) /* I - Command-line arguments */
69 int i
; /* Looping var */
70 char *opt
; /* Option pointer */
71 const char *username
; /* Pointer to username */
72 const char *groupname
; /* Pointer to group name */
73 int op
; /* Operation (add, change, delete) */
74 const char *passwd
; /* Password string */
75 FILE *infile
, /* Input file */
76 *outfile
; /* Output file */
77 char line
[256], /* Line from file */
78 userline
[17], /* User from line */
79 groupline
[17], /* Group from line */
80 md5line
[33], /* MD5-sum from line */
81 md5new
[33]; /* New MD5 sum */
82 const char *root
; /* CUPS server root directory */
83 char passwdmd5
[1024], /* passwd.md5 file */
84 passwdold
[1024], /* passwd.old file */
85 passwdnew
[1024]; /* passwd.tmp file */
86 char *newpass
, /* new password */
87 *oldpass
; /* old password */
88 int flag
; /* Password check flags... */
89 int fd
; /* Password file descriptor */
90 int error
; /* Write error */
91 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
92 struct sigaction action
; /* Signal action */
93 #endif /* HAVE_SIGACTION && !HAVE_SIGSET*/
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...
117 * We use the CUPS_SERVERROOT environment variable when we are running
118 * as root or when lppasswd is not setuid...
121 if ((root
= getenv("CUPS_SERVERROOT")) == NULL
||
122 (getuid() != geteuid() && getuid()))
123 root
= CUPS_SERVERROOT
;
125 snprintf(passwdmd5
, sizeof(passwdmd5
), "%s/passwd.md5", root
);
126 snprintf(passwdold
, sizeof(passwdold
), "%s/passwd.old", root
);
127 snprintf(passwdnew
, sizeof(passwdnew
), "%s/passwd.new", root
);
130 * Find the default system group...
133 if (getgrnam(CUPS_DEFAULT_GROUP
))
134 groupname
= CUPS_DEFAULT_GROUP
;
136 groupname
= "unknown";
144 * Parse command-line options...
147 for (i
= 1; i
< argc
; i
++)
148 if (argv
[i
][0] == '-')
149 for (opt
= argv
[i
] + 1; *opt
; opt
++)
155 case 'x' : /* Delete */
158 case 'g' : /* Group */
165 case 'h' : /* Help */
168 default : /* Bad option */
178 * See if we are trying to add or delete a password when we aren't logged in
182 if (getuid() && getuid() != geteuid() && (op
!= CHANGE
|| username
))
184 _cupsLangPuts(stderr
,
185 _("lppasswd: Only root can add or delete passwords\n"));
190 * Fill in missing info...
194 username
= cupsUser();
196 oldpass
= newpass
= NULL
;
199 * Obtain old and new password _before_ locking the database
200 * to keep users from locking the file indefinitely.
203 if (op
== CHANGE
&& getuid())
205 if ((passwd
= cupsGetPassword(_("Enter old password:"))) == NULL
)
208 if ((oldpass
= strdup(passwd
)) == NULL
)
210 _cupsLangPrintf(stderr
,
211 _("lppasswd: Unable to copy password string: %s\n"),
218 * Now get the new password, if necessary...
223 if ((passwd
= cupsGetPassword(_("Enter password:"))) == NULL
)
226 if ((newpass
= strdup(passwd
)) == NULL
)
228 _cupsLangPrintf(stderr
,
229 _("lppasswd: Unable to copy password string: %s\n"),
234 if ((passwd
= cupsGetPassword(_("Enter password again:"))) == NULL
)
237 if (strcmp(passwd
, newpass
) != 0)
239 _cupsLangPuts(stderr
,
240 _("lppasswd: Sorry, passwords don't match\n"));
245 * Check that the password contains at least one letter and number.
250 for (passwd
= newpass
; *passwd
; passwd
++)
251 if (isdigit(*passwd
& 255))
253 else if (isalpha(*passwd
& 255))
257 * Only allow passwords that are at least 6 chars, have a letter and
258 * a number, and don't contain the username.
261 if (strlen(newpass
) < 6 || strstr(newpass
, username
) != NULL
|| flag
!= 3)
263 _cupsLangPuts(stderr
,
264 _("lppasswd: Sorry, password rejected.\n"
265 "Your password must be at least 6 characters long, "
267 "your username, and must contain at least one letter "
274 * Ignore SIGHUP, SIGINT, SIGTERM, and SIGXFSZ (if defined) for the
275 * remainder of the time so that we won't end up with bogus password
280 # if defined(HAVE_SIGSET)
281 sigset(SIGHUP
, SIG_IGN
);
282 sigset(SIGINT
, SIG_IGN
);
283 sigset(SIGTERM
, SIG_IGN
);
285 sigset(SIGXFSZ
, SIG_IGN
);
286 # endif /* SIGXFSZ */
287 # elif defined(HAVE_SIGACTION)
288 memset(&action
, 0, sizeof(action
));
289 action
.sa_handler
= SIG_IGN
;
291 sigaction(SIGHUP
, &action
, NULL
);
292 sigaction(SIGINT
, &action
, NULL
);
293 sigaction(SIGTERM
, &action
, NULL
);
295 sigaction(SIGXFSZ
, &action
, NULL
);
296 # endif /* SIGXFSZ */
298 signal(SIGHUP
, SIG_IGN
);
299 signal(SIGINT
, SIG_IGN
);
300 signal(SIGTERM
, SIG_IGN
);
302 signal(SIGXFSZ
, SIG_IGN
);
303 # endif /* SIGXFSZ */
308 * Open the output file.
311 if ((fd
= open(passwdnew
, O_WRONLY
| O_CREAT
| O_EXCL
, 0400)) < 0)
314 _cupsLangPuts(stderr
, _("lppasswd: Password file busy\n"));
316 _cupsLangPrintf(stderr
,
317 _("lppasswd: Unable to open password file: %s\n"),
323 if ((outfile
= fdopen(fd
, "w")) == NULL
)
325 _cupsLangPrintf(stderr
,
326 _("lppasswd: Unable to open password file: %s\n"),
334 setbuf(outfile
, NULL
);
337 * Open the existing password file and create a new one...
340 infile
= fopen(passwdmd5
, "r");
341 if (infile
== NULL
&& errno
!= ENOENT
&& op
!= ADD
)
343 _cupsLangPrintf(stderr
,
344 _("lppasswd: Unable to open password file: %s\n"),
355 * Read lines from the password file; the format is:
357 * username:group:MD5-sum
367 while (fgets(line
, sizeof(line
), infile
) != NULL
)
369 if (sscanf(line
, "%16[^:]:%16[^:]:%32s", userline
, groupline
, md5line
) != 3)
372 if (strcmp(username
, userline
) == 0 &&
373 strcmp(groupname
, groupline
) == 0)
376 if (fputs(line
, outfile
) == EOF
)
378 _cupsLangPrintf(stderr
,
379 _("lppasswd: Unable to write to password file: %s\n"),
388 while (fgets(line
, sizeof(line
), infile
) != NULL
)
389 if (fputs(line
, outfile
) == EOF
)
391 _cupsLangPrintf(stderr
,
392 _("lppasswd: Unable to write to password file: %s\n"),
401 (strcmp(username
, userline
) || strcmp(groupname
, groupline
)))
403 _cupsLangPrintf(stderr
,
404 _("lppasswd: user \"%s\" and group \"%s\" do not exist.\n"),
405 username
, groupname
);
408 else if (op
!= DELETE
)
411 strcmp(httpMD5(username
, "CUPS", oldpass
, md5new
), md5line
) != 0)
413 _cupsLangPuts(stderr
,
414 _("lppasswd: Sorry, password doesn't match\n"));
419 snprintf(line
, sizeof(line
), "%s:%s:%s\n", username
, groupname
,
420 httpMD5(username
, "CUPS", newpass
, md5new
));
421 if (fputs(line
, outfile
) == EOF
)
423 _cupsLangPrintf(stderr
,
424 _("lppasswd: Unable to write to password file: %s\n"),
438 if (fclose(outfile
) == EOF
)
442 * Error out gracefully as needed...
447 _cupsLangPuts(stderr
, _("lppasswd: Password file not updated\n"));
455 * Save old passwd file
459 if (link(passwdmd5
, passwdold
) && errno
!= ENOENT
)
461 _cupsLangPrintf(stderr
,
462 _("lppasswd: failed to backup old password file: %s\n"),
469 * Install new password file
472 if (rename(passwdnew
, passwdmd5
) < 0)
474 _cupsLangPrintf(stderr
,
475 _("lppasswd: failed to rename password file: %s\n"),
486 * 'usage()' - Show program usage.
490 usage(FILE *fp
) /* I - File to send usage to */
493 _cupsLangPuts(fp
, _("Usage: lppasswd [-g groupname]\n"));
496 _("Usage: lppasswd [-g groupname] [username]\n"
497 " lppasswd [-g groupname] -a [username]\n"
498 " lppasswd [-g groupname] -x [username]\n"));
505 * End of "$Id: lppasswd.c 6649 2007-07-11 21:46:42Z mike $".