]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/auth.c
2 * "$Id: auth.c 4929 2006-01-13 16:38:43Z mike $"
4 * Authorization routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsdAddLocation() - Add a location for authorization.
27 * cupsdAddName() - Add a name to a location...
28 * cupsdAllowHost() - Add a host name that is allowed to access the
30 * cupsdAllowIP() - Add an IP address or network that is allowed to
31 * access the location.
32 * cupsdCheckAuth() - Check authorization masks.
33 * cupsdCheckGroup() - Check for a user's group membership.
34 * cupsdCopyLocation() - Make a copy of a location...
35 * cupsdDeleteAllLocations() - Free all memory used for location authorization.
36 * cupsdDeleteLocation() - Free all memory used by a location.
37 * cupsdDenyHost() - Add a host name that is not allowed to access the
39 * cupsdDenyIP() - Add an IP address or network that is not allowed
40 * to access the location.
41 * cupsdFindBest() - Find the location entry that best matches the
43 * cupsdFindLocation() - Find the named location.
44 * cupsdGetMD5Passwd() - Get an MD5 password.
45 * cupsdIsAuthorized() - Check to see if the user is authorized...
46 * add_allow() - Add an allow mask to the location.
47 * add_deny() - Add a deny mask to the location.
48 * cups_crypt() - Encrypt the password using the DES or MD5
49 * algorithms, as needed.
50 * pam_func() - PAM conversation function.
51 * to64() - Base64-encode an integer value...
55 * Include necessary headers...
62 #endif /* HAVE_SHADOW_H */
65 #endif /* HAVE_CRYPT_H */
67 # ifdef HAVE_PAM_PAM_APPL_H
68 # include <pam/pam_appl.h>
70 # include <security/pam_appl.h>
71 # endif /* HAVE_PAM_PAM_APPL_H */
72 #endif /* HAVE_LIBPAM */
75 #endif /* HAVE_USERSEC_H */
82 static cupsd_authmask_t
*add_allow(cupsd_location_t
*loc
);
83 static cupsd_authmask_t
*add_deny(cupsd_location_t
*loc
);
85 static char *cups_crypt(const char *pw
, const char *salt
);
86 #endif /* !HAVE_LIBPAM */
88 static int pam_func(int, const struct pam_message
**,
89 struct pam_response
**, void *);
91 static void to64(char *s
, unsigned long v
, int n
);
92 #endif /* HAVE_LIBPAM */
100 typedef struct cupsd_authdata_s
/**** Authentication data ****/
102 char username
[33], /* Username string */
103 password
[33]; /* Password string */
105 #endif /* HAVE_LIBPAM */
112 #if defined(__hpux) && defined(HAVE_LIBPAM)
113 static cupsd_authdata_t
*auth_datat
; /* Current client being authenticated */
114 #endif /* __hpux && HAVE_LIBPAM */
118 * 'cupsdAddLocation()' - Add a location for authorization.
121 cupsd_location_t
* /* O - Pointer to new location record */
122 cupsdAddLocation(const char *location
) /* I - Location path */
124 cupsd_location_t
*temp
; /* New location */
128 * Try to allocate memory for the new location.
131 if (NumLocations
== 0)
132 temp
= malloc(sizeof(cupsd_location_t
));
134 temp
= realloc(Locations
, sizeof(cupsd_location_t
) * (NumLocations
+ 1));
140 temp
+= NumLocations
;
144 * Initialize the record and copy the name over...
147 memset(temp
, 0, sizeof(cupsd_location_t
));
148 strlcpy(temp
->location
, location
, sizeof(temp
->location
));
149 temp
->length
= strlen(temp
->location
);
151 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAddLocation: added location \'%s\'",
155 * Return the new record...
163 * 'cupsdAddName()' - Add a name to a location...
167 cupsdAddName(cupsd_location_t
*loc
, /* I - Location to add to */
168 char *name
) /* I - Name to add */
170 char **temp
; /* Pointer to names array */
173 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddName(loc=%p, name=\"%s\")",
176 if (loc
->num_names
== 0)
177 temp
= malloc(sizeof(char *));
179 temp
= realloc(loc
->names
, (loc
->num_names
+ 1) * sizeof(char *));
183 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to add name to location %s: %s",
184 loc
->location
, strerror(errno
));
190 if ((temp
[loc
->num_names
] = strdup(name
)) == NULL
)
192 cupsdLogMessage(CUPSD_LOG_ERROR
,
193 "Unable to duplicate name for location %s: %s",
194 loc
->location
, strerror(errno
));
203 * 'cupsdAllowHost()' - Add a host name that is allowed to access the location.
207 cupsdAllowHost(cupsd_location_t
*loc
, /* I - Location to add to */
208 char *name
) /* I - Name of host or domain to add */
210 cupsd_authmask_t
*temp
; /* New host/domain mask */
211 char ifname
[32], /* Interface name */
212 *ifptr
; /* Pointer to end of name */
215 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAllowHost(loc=%p(%s), name=\"%s\")",
216 loc
, loc
->location
, name
);
218 if ((temp
= add_allow(loc
)) == NULL
)
221 if (strcasecmp(name
, "@LOCAL") == 0)
224 * Allow *interface*...
227 temp
->type
= AUTH_INTERFACE
;
228 temp
->mask
.name
.name
= strdup("*");
229 temp
->mask
.name
.length
= 1;
231 else if (strncasecmp(name
, "@IF(", 4) == 0)
234 * Allow *interface*...
237 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
239 ifptr
= ifname
+ strlen(ifname
);
241 if (ifptr
[-1] == ')')
247 temp
->type
= AUTH_INTERFACE
;
248 temp
->mask
.name
.name
= strdup(ifname
);
249 temp
->mask
.name
.length
= ifptr
- ifname
;
257 temp
->type
= AUTH_NAME
;
258 temp
->mask
.name
.name
= strdup(name
);
259 temp
->mask
.name
.length
= strlen(name
);
265 * 'cupsdAllowIP()' - Add an IP address or network that is allowed to access
270 cupsdAllowIP(cupsd_location_t
*loc
, /* I - Location to add to */
271 unsigned address
[4], /* I - IP address to add */
272 unsigned netmask
[4]) /* I - Netmask of address */
274 cupsd_authmask_t
*temp
; /* New host/domain mask */
277 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
278 "cupsdAllowIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
279 loc
, loc
->location
, address
[0], address
[1], address
[2],
280 address
[3], netmask
[0], netmask
[1], netmask
[2],
283 if ((temp
= add_allow(loc
)) == NULL
)
286 temp
->type
= AUTH_IP
;
287 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
288 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
293 * 'cupsdAuthorize()' - Validate any authorization credentials.
297 cupsdAuthorize(cupsd_client_t
*con
) /* I - Client connection */
299 int type
; /* Authentication type */
300 char *authorization
, /* Pointer into Authorization string */
301 *ptr
, /* Pointer into string */
302 username
[65], /* Username string */
303 password
[33]; /* Password string */
304 const char *localuser
; /* Certificate username */
305 char nonce
[HTTP_MAX_VALUE
], /* Nonce value from client */
306 md5
[33], /* MD5 password */
307 basicmd5
[33]; /* MD5 of Basic password */
308 static const char * const states
[] = /* HTTP client states... */
328 * Locate the best matching location so we know what kind of
329 * authentication to expect...
332 con
->best
= cupsdFindBest(con
->uri
, con
->http
.state
);
334 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
335 "cupsdAuthorize: con->uri=\"%s\", con->best=%p(%s)",
336 con
->uri
, con
->best
, con
->best
? con
->best
->location
: "");
338 if (con
->best
&& con
->best
->type
!= AUTH_NONE
)
339 type
= con
->best
->type
;
341 type
= DefaultAuthType
;
344 * Decode the Authorization string...
347 authorization
= con
->http
.fields
[HTTP_FIELD_AUTHORIZATION
];
349 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAuthorize: Authorization=\"%s\"",
355 if (type
== AUTH_NONE
)
358 * No authorization required, return early...
361 cupsdLogMessage(CUPSD_LOG_DEBUG
,
362 "cupsdAuthorize: No authentication required.");
365 else if (!*authorization
)
368 * No authorization data provided, return early...
371 cupsdLogMessage(CUPSD_LOG_DEBUG
,
372 "cupsdAuthorize: No authentication data provided.");
375 else if (!strncmp(authorization
, "Local", 5) &&
376 !strcasecmp(con
->http
.hostname
, "localhost"))
379 * Get Local certificate authentication data...
383 while (isspace(*authorization
))
386 if ((localuser
= cupsdFindCert(authorization
)) != NULL
)
387 strlcpy(username
, localuser
, sizeof(username
));
390 cupsdLogMessage(CUPSD_LOG_ERROR
,
391 "cupsdAuthorize: Local authentication certificate not "
396 else if (!strncmp(authorization
, "Basic", 5) &&
397 (type
== AUTH_BASIC
|| type
== AUTH_BASICDIGEST
))
400 * Get the Basic authentication data...
403 int userlen
; /* Username:password length */
407 while (isspace(*authorization
))
410 userlen
= sizeof(username
);
411 httpDecode64_2(username
, &userlen
, authorization
);
414 * Pull the username and password out...
417 if ((ptr
= strchr(username
, ':')) == NULL
)
419 cupsdLogMessage(CUPSD_LOG_ERROR
,
420 "cupsdAuthorize: Missing Basic password!");
429 * Username must not be empty...
432 cupsdLogMessage(CUPSD_LOG_ERROR
,
433 "cupsdAuthorize: Empty Basic username!");
440 * Password must not be empty...
443 cupsdLogMessage(CUPSD_LOG_ERROR
,
444 "cupsdAuthorize: Empty Basic password!");
448 strlcpy(password
, ptr
, sizeof(password
));
451 * Validate the username and password...
460 * Only use PAM to do authentication. This supports MD5
461 * passwords, among other things...
464 pam_handle_t
*pamh
; /* PAM authentication handle */
465 int pamerr
; /* PAM error code */
466 struct pam_conv pamdata
;/* PAM conversation data */
467 cupsd_authdata_t data
; /* Authentication data */
470 strlcpy(data
.username
, username
, sizeof(data
.username
));
471 strlcpy(data
.password
, password
, sizeof(data
.password
));
473 pamdata
.conv
= pam_func
;
474 pamdata
.appdata_ptr
= &data
;
478 * Workaround for HP-UX bug in pam_unix; see pam_func() below for
485 pamerr
= pam_start("cups", username
, &pamdata
, &pamh
);
486 if (pamerr
!= PAM_SUCCESS
)
488 cupsdLogMessage(CUPSD_LOG_ERROR
,
489 "cupsdAuthorize: pam_start() returned %d (%s)!\n",
490 pamerr
, pam_strerror(pamh
, pamerr
));
495 pamerr
= pam_authenticate(pamh
, PAM_SILENT
);
496 if (pamerr
!= PAM_SUCCESS
)
498 cupsdLogMessage(CUPSD_LOG_ERROR
,
499 "cupsdAuthorize: pam_authenticate() returned %d "
501 pamerr
, pam_strerror(pamh
, pamerr
));
506 pamerr
= pam_acct_mgmt(pamh
, PAM_SILENT
);
507 if (pamerr
!= PAM_SUCCESS
)
509 cupsdLogMessage(CUPSD_LOG_ERROR
,
510 "cupsdAuthorize: pam_acct_mgmt() returned %d "
512 pamerr
, pam_strerror(pamh
, pamerr
));
517 pam_end(pamh
, PAM_SUCCESS
);
519 #elif defined(HAVE_USERSEC_H)
521 * Use AIX authentication interface...
524 char *authmsg
; /* Authentication message */
525 char *loginmsg
; /* Login message */
526 int reenter
; /* ??? */
529 cupsdLogMessage(CUPSD_LOG_DEBUG
,
530 "cupsdAuthorize: AIX authenticate of username \"%s\"",
534 if (authenticate(username
, password
, &reenter
, &authmsg
) != 0)
536 cupsdLogMessage(CUPSD_LOG_DEBUG
,
537 "cupsdAuthorize: Unable to authenticate username "
539 username
, strerror(errno
));
545 * Use normal UNIX password file-based authentication...
548 char *pass
; /* Encrypted password */
549 struct passwd
*pw
; /* User password data */
550 # ifdef HAVE_SHADOW_H
551 struct spwd
*spw
; /* Shadow password data */
552 # endif /* HAVE_SHADOW_H */
555 pw
= getpwnam(username
); /* Get the current password */
556 endpwent(); /* Close the password file */
564 cupsdLogMessage(CUPSD_LOG_ERROR
,
565 "cupsdAuthorize: Unknown username \"%s\"!",
567 return (HTTP_UNAUTHORIZED
);
570 # ifdef HAVE_SHADOW_H
571 spw
= getspnam(username
);
574 if (!spw
&& !strcmp(pw
->pw_passwd
, "x"))
577 * Don't allow blank passwords!
580 cupsdLogMessage(CUPSD_LOG_ERROR
,
581 "cupsdAuthorize: Username \"%s\" has no shadow "
582 "password!", username
);
586 if (spw
&& !spw
->sp_pwdp
[0] && !pw
->pw_passwd
[0])
588 if (!pw
->pw_passwd
[0])
589 # endif /* HAVE_SHADOW_H */
592 * Don't allow blank passwords!
595 cupsdLogMessage(CUPSD_LOG_ERROR
,
596 "cupsdAuthorize: Username \"%s\" has no password!",
602 * OK, the password isn't blank, so compare with what came from the
606 pass
= cups_crypt(password
, pw
->pw_passwd
);
608 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
609 "cupsdAuthorize: pw_passwd=\"%s\", crypt=\"%s\"",
610 pw
->pw_passwd
, pass
);
612 if (!pass
|| strcmp(pw
->pw_passwd
, pass
))
614 # ifdef HAVE_SHADOW_H
617 pass
= cups_crypt(password
, spw
->sp_pwdp
);
619 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
620 "cupsdAuthorize: sp_pwdp=\"%s\", crypt=\"%s\"",
623 if (pass
== NULL
|| strcmp(spw
->sp_pwdp
, pass
))
625 cupsdLogMessage(CUPSD_LOG_ERROR
,
626 "cupsdAuthorize: Authentication failed for "
633 # endif /* HAVE_SHADOW_H */
635 cupsdLogMessage(CUPSD_LOG_ERROR
,
636 "cupsdAuthorize: Authentication failed for "
642 #endif /* HAVE_LIBPAM */
646 case AUTH_BASICDIGEST
:
648 * Do Basic authentication with the Digest password file...
651 if (!cupsdGetMD5Passwd(username
, NULL
, md5
))
653 cupsdLogMessage(CUPSD_LOG_ERROR
,
654 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
659 httpMD5(username
, "CUPS", password
, basicmd5
);
661 if (strcmp(md5
, basicmd5
))
663 cupsdLogMessage(CUPSD_LOG_ERROR
,
664 "cupsdAuthorize: Authentication failed for \"%s\"!",
671 else if (!strncmp(authorization
, "Digest", 6) && type
== AUTH_DIGEST
)
674 * Get the username, password, and nonce from the Digest attributes...
677 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "username",
678 username
, sizeof(username
)) || !username
[0])
681 * Username must not be empty...
684 cupsdLogMessage(CUPSD_LOG_ERROR
,
685 "cupsdAuthorize: Empty or missing Digest username!");
689 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "response",
690 password
, sizeof(password
)) || !password
[0])
693 * Password must not be empty...
696 cupsdLogMessage(CUPSD_LOG_ERROR
,
697 "cupsdAuthorize: Empty or missing Digest password!");
701 if (!httpGetSubField(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "nonce",
704 cupsdLogMessage(CUPSD_LOG_ERROR
,
705 "cupsdAuthorize: No nonce value for Digest "
710 if (strcmp(con
->http
.hostname
, nonce
))
712 cupsdLogMessage(CUPSD_LOG_ERROR
,
713 "cupsdAuthorize: Bad nonce value, expected \"%s\", "
714 "got \"%s\"!", con
->http
.hostname
, nonce
);
719 * Validate the username and password...
722 if (!cupsdGetMD5Passwd(username
, NULL
, md5
))
724 cupsdLogMessage(CUPSD_LOG_ERROR
,
725 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
730 httpMD5Final(nonce
, states
[con
->http
.state
], con
->uri
, md5
);
732 if (strcmp(md5
, password
))
734 cupsdLogMessage(CUPSD_LOG_ERROR
,
735 "cupsdAuthorize: Authentication failed for \"%s\"!",
742 cupsdLogMessage(CUPSD_LOG_DEBUG
,
743 "cupsdAuthorize: Bad authentication data.");
748 * If we get here, then we were able to validate the username and
749 * password - copy the validated username and password to the client
753 strlcpy(con
->username
, username
, sizeof(con
->username
));
754 strlcpy(con
->password
, password
, sizeof(con
->password
));
756 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: username=\"%s\"",
762 * 'cupsdCheckAuth()' - Check authorization masks.
765 int /* O - 1 if mask matches, 0 otherwise */
767 unsigned ip
[4], /* I - Client address */
768 char *name
, /* I - Client hostname */
769 int name_len
, /* I - Length of hostname */
770 int num_masks
, /* I - Number of masks */
771 cupsd_authmask_t
*masks
) /* I - Masks */
773 int i
; /* Looping var */
774 cupsd_netif_t
*iface
; /* Network interface */
775 unsigned netip4
; /* IPv4 network address */
777 unsigned netip6
[4]; /* IPv6 network address */
778 #endif /* AF_INET6 */
780 while (num_masks
> 0)
784 case AUTH_INTERFACE
:
786 * Check for a match with a network interface...
789 netip4
= htonl(ip
[3]);
792 netip6
[0] = htonl(ip
[0]);
793 netip6
[1] = htonl(ip
[1]);
794 netip6
[2] = htonl(ip
[2]);
795 netip6
[3] = htonl(ip
[3]);
796 #endif /* AF_INET6 */
798 if (!strcmp(masks
->mask
.name
.name
, "*"))
801 * Check against all local interfaces...
806 for (iface
= NetIFList
; iface
!= NULL
; iface
= iface
->next
)
809 * Only check local interfaces...
812 if (!iface
->is_local
)
815 if (iface
->address
.addr
.sa_family
== AF_INET
)
818 * Check IPv4 address...
821 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
822 (iface
->address
.ipv4
.sin_addr
.s_addr
&
823 iface
->mask
.ipv4
.sin_addr
.s_addr
))
830 * Check IPv6 address...
833 for (i
= 0; i
< 4; i
++)
834 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
835 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
836 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
842 #endif /* AF_INET6 */
848 * Check the named interface...
851 for (iface
= cupsdNetIFFind(masks
->mask
.name
.name
);
852 iface
&& !strcasecmp(masks
->mask
.name
.name
, iface
->name
);
855 if (iface
->address
.addr
.sa_family
== AF_INET
)
858 * Check IPv4 address...
861 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
862 (iface
->address
.ipv4
.sin_addr
.s_addr
&
863 iface
->mask
.ipv4
.sin_addr
.s_addr
))
870 * Check IPv6 address...
873 for (i
= 0; i
< 4; i
++)
874 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
875 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
876 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
882 #endif /* AF_INET6 */
889 * Check for exact name match...
892 if (!strcasecmp(name
, masks
->mask
.name
.name
))
896 * Check for domain match...
899 if (name_len
>= masks
->mask
.name
.length
&&
900 masks
->mask
.name
.name
[0] == '.' &&
901 strcasecmp(name
+ name_len
- masks
->mask
.name
.length
,
902 masks
->mask
.name
.name
) == 0)
908 * Check for IP/network address match...
911 for (i
= 0; i
< 4; i
++)
912 if ((ip
[i
] & masks
->mask
.ip
.netmask
[i
]) !=
913 masks
->mask
.ip
.address
[i
])
930 * 'cupsdCheckGroup()' - Check for a user's group membership.
933 int /* O - 1 if user is a member, 0 otherwise */
935 const char *username
, /* I - User name */
936 struct passwd
*user
, /* I - System user info */
937 const char *groupname
) /* I - Group name */
939 int i
; /* Looping var */
940 struct group
*group
; /* System group info */
941 char junk
[33]; /* MD5 password (not used) */
944 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
945 "cupsdCheckGroup(username=\"%s\", user=%p, groupname=\"%s\")",
946 username
, user
, groupname
);
952 if (!username
|| !groupname
)
956 * Check to see if the user is a member of the named group...
959 group
= getgrnam(groupname
);
965 * Group exists, check it...
968 for (i
= 0; group
->gr_mem
[i
]; i
++)
969 if (!strcasecmp(username
, group
->gr_mem
[i
]))
974 * Group doesn't exist or user not in group list, check the group ID
975 * against the user's group ID...
978 if (user
&& group
&& group
->gr_gid
== user
->pw_gid
)
982 * Username not found, group not found, or user is not part of the
983 * system group... Check for a user and group in the MD5 password
987 if (cupsdGetMD5Passwd(username
, groupname
, junk
) != NULL
)
991 * If we get this far, then the user isn't part of the named group...
999 * 'cupsdCopyLocation()' - Make a copy of a location...
1002 cupsd_location_t
* /* O - New location */
1004 cupsd_location_t
**loc
) /* IO - Original location */
1006 int i
; /* Looping var */
1007 int locindex
; /* Index into Locations array */
1008 cupsd_location_t
*temp
; /* New location */
1009 char location
[HTTP_MAX_URI
];
1010 /* Location of resource */
1014 * Add the new location, updating the original location
1015 * pointer as needed...
1018 locindex
= *loc
- Locations
;
1021 * Use a local copy of location because cupsdAddLocation may cause
1022 * this memory to be moved...
1025 strlcpy(location
, (*loc
)->location
, sizeof(location
));
1027 if ((temp
= cupsdAddLocation(location
)) == NULL
)
1030 *loc
= Locations
+ locindex
;
1033 * Copy the information from the original location to the new one.
1036 temp
->limit
= (*loc
)->limit
;
1037 temp
->order_type
= (*loc
)->order_type
;
1038 temp
->type
= (*loc
)->type
;
1039 temp
->level
= (*loc
)->level
;
1040 temp
->satisfy
= (*loc
)->satisfy
;
1041 temp
->encryption
= (*loc
)->encryption
;
1043 if ((temp
->num_names
= (*loc
)->num_names
) > 0)
1046 * Copy the names array...
1049 if ((temp
->names
= calloc(temp
->num_names
, sizeof(char *))) == NULL
)
1051 cupsdLogMessage(CUPSD_LOG_ERROR
,
1052 "cupsdCopyLocation: Unable to allocate memory for %d names: %s",
1053 temp
->num_names
, strerror(errno
));
1058 for (i
= 0; i
< temp
->num_names
; i
++)
1059 if ((temp
->names
[i
] = strdup((*loc
)->names
[i
])) == NULL
)
1061 cupsdLogMessage(CUPSD_LOG_ERROR
,
1062 "cupsdCopyLocation: Unable to copy name \"%s\": %s",
1063 (*loc
)->names
[i
], strerror(errno
));
1070 if ((temp
->num_allow
= (*loc
)->num_allow
) > 0)
1073 * Copy allow rules...
1076 if ((temp
->allow
= calloc(temp
->num_allow
, sizeof(cupsd_authmask_t
))) == NULL
)
1078 cupsdLogMessage(CUPSD_LOG_ERROR
,
1079 "cupsdCopyLocation: Unable to allocate memory for %d allow rules: %s",
1080 temp
->num_allow
, strerror(errno
));
1085 for (i
= 0; i
< temp
->num_allow
; i
++)
1086 switch (temp
->allow
[i
].type
= (*loc
)->allow
[i
].type
)
1089 temp
->allow
[i
].mask
.name
.length
= (*loc
)->allow
[i
].mask
.name
.length
;
1090 temp
->allow
[i
].mask
.name
.name
= strdup((*loc
)->allow
[i
].mask
.name
.name
);
1092 if (temp
->allow
[i
].mask
.name
.name
== NULL
)
1094 cupsdLogMessage(CUPSD_LOG_ERROR
,
1095 "cupsdCopyLocation: Unable to copy allow name \"%s\": %s",
1096 (*loc
)->allow
[i
].mask
.name
.name
, strerror(errno
));
1102 memcpy(&(temp
->allow
[i
].mask
.ip
), &((*loc
)->allow
[i
].mask
.ip
),
1103 sizeof(cupsd_ipmask_t
));
1108 if ((temp
->num_deny
= (*loc
)->num_deny
) > 0)
1111 * Copy deny rules...
1114 if ((temp
->deny
= calloc(temp
->num_deny
, sizeof(cupsd_authmask_t
))) == NULL
)
1116 cupsdLogMessage(CUPSD_LOG_ERROR
,
1117 "cupsdCopyLocation: Unable to allocate memory for %d deny rules: %s",
1118 temp
->num_deny
, strerror(errno
));
1123 for (i
= 0; i
< temp
->num_deny
; i
++)
1124 switch (temp
->deny
[i
].type
= (*loc
)->deny
[i
].type
)
1127 temp
->deny
[i
].mask
.name
.length
= (*loc
)->deny
[i
].mask
.name
.length
;
1128 temp
->deny
[i
].mask
.name
.name
= strdup((*loc
)->deny
[i
].mask
.name
.name
);
1130 if (temp
->deny
[i
].mask
.name
.name
== NULL
)
1132 cupsdLogMessage(CUPSD_LOG_ERROR
,
1133 "cupsdCopyLocation: Unable to copy deny name \"%s\": %s",
1134 (*loc
)->deny
[i
].mask
.name
.name
, strerror(errno
));
1140 memcpy(&(temp
->deny
[i
].mask
.ip
), &((*loc
)->deny
[i
].mask
.ip
),
1141 sizeof(cupsd_ipmask_t
));
1151 * 'cupsdDeleteAllLocations()' - Free all memory used for location authorization.
1155 cupsdDeleteAllLocations(void)
1157 int i
; /* Looping var */
1158 cupsd_location_t
*loc
; /* Current location */
1162 * Free all of the allow/deny records first...
1165 for (i
= NumLocations
, loc
= Locations
; i
> 0; i
--, loc
++)
1166 cupsdDeleteLocation(loc
);
1169 * Then free the location array...
1172 if (NumLocations
> 0)
1181 * 'cupsdDeleteLocation()' - Free all memory used by a location.
1185 cupsdDeleteLocation(
1186 cupsd_location_t
*loc
) /* I - Location to delete */
1188 int i
; /* Looping var */
1189 cupsd_authmask_t
*mask
; /* Current mask */
1192 for (i
= loc
->num_names
- 1; i
>= 0; i
--)
1193 free(loc
->names
[i
]);
1195 if (loc
->num_names
> 0)
1198 for (i
= loc
->num_allow
, mask
= loc
->allow
; i
> 0; i
--, mask
++)
1199 if (mask
->type
== AUTH_NAME
|| mask
->type
== AUTH_INTERFACE
)
1200 free(mask
->mask
.name
.name
);
1202 if (loc
->num_allow
> 0)
1205 for (i
= loc
->num_deny
, mask
= loc
->deny
; i
> 0; i
--, mask
++)
1206 if (mask
->type
== AUTH_NAME
|| mask
->type
== AUTH_INTERFACE
)
1207 free(mask
->mask
.name
.name
);
1209 if (loc
->num_deny
> 0)
1215 * 'cupsdDenyHost()' - Add a host name that is not allowed to access the
1220 cupsdDenyHost(cupsd_location_t
*loc
, /* I - Location to add to */
1221 char *name
) /* I - Name of host or domain to add */
1223 cupsd_authmask_t
*temp
; /* New host/domain mask */
1224 char ifname
[32], /* Interface name */
1225 *ifptr
; /* Pointer to end of name */
1228 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDenyHost(loc=%p(%s), name=\"%s\")",
1229 loc
, loc
->location
, name
);
1231 if ((temp
= add_deny(loc
)) == NULL
)
1234 if (strcasecmp(name
, "@LOCAL") == 0)
1237 * Deny *interface*...
1240 temp
->type
= AUTH_INTERFACE
;
1241 temp
->mask
.name
.name
= strdup("*");
1242 temp
->mask
.name
.length
= 1;
1244 else if (strncasecmp(name
, "@IF(", 4) == 0)
1247 * Deny *interface*...
1250 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
1252 ifptr
= ifname
+ strlen(ifname
);
1254 if (ifptr
[-1] == ')')
1260 temp
->type
= AUTH_INTERFACE
;
1261 temp
->mask
.name
.name
= strdup(ifname
);
1262 temp
->mask
.name
.length
= ifptr
- ifname
;
1270 temp
->type
= AUTH_NAME
;
1271 temp
->mask
.name
.name
= strdup(name
);
1272 temp
->mask
.name
.length
= strlen(name
);
1278 * 'cupsdDenyIP()' - Add an IP address or network that is not allowed to
1279 * access the location.
1283 cupsdDenyIP(cupsd_location_t
*loc
, /* I - Location to add to */
1284 unsigned address
[4],/* I - IP address to add */
1285 unsigned netmask
[4])/* I - Netmask of address */
1287 cupsd_authmask_t
*temp
; /* New host/domain mask */
1290 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1291 "cupsdDenyIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
1292 loc
, loc
->location
, address
[0], address
[1], address
[2],
1293 address
[3], netmask
[0], netmask
[1], netmask
[2],
1296 if ((temp
= add_deny(loc
)) == NULL
)
1299 temp
->type
= AUTH_IP
;
1300 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
1301 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
1306 * 'cupsdFindBest()' - Find the location entry that best matches the resource.
1309 cupsd_location_t
* /* O - Location that matches */
1310 cupsdFindBest(const char *path
, /* I - Resource path */
1311 http_state_t state
) /* I - HTTP state/request */
1313 int i
; /* Looping var */
1314 char uri
[HTTP_MAX_URI
],
1315 /* URI in request... */
1316 *uriptr
; /* Pointer into URI */
1317 cupsd_location_t
*loc
, /* Current location */
1318 *best
; /* Best match for location so far */
1319 int bestlen
; /* Length of best match */
1320 int limit
; /* Limit field */
1321 static const int limits
[] = /* Map http_status_t to AUTH_LIMIT_xyz */
1341 * First copy the connection URI to a local string so we have drop
1342 * any .ppd extension from the pathname in /printers or /classes
1346 strlcpy(uri
, path
, sizeof(uri
));
1348 if (!strncmp(uri
, "/printers/", 10) ||
1349 !strncmp(uri
, "/classes/", 9))
1352 * Check if the URI has .ppd on the end...
1355 uriptr
= uri
+ strlen(uri
) - 4; /* len > 4 if we get here... */
1357 if (!strcmp(uriptr
, ".ppd"))
1361 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: uri = \"%s\"...", uri
);
1364 * Loop through the list of locations to find a match...
1367 limit
= limits
[state
];
1371 for (i
= NumLocations
, loc
= Locations
; i
> 0; i
--, loc
++)
1373 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: Location %s Limit %x",
1374 loc
->location
, loc
->limit
);
1376 if (!strncmp(uri
, "/printers/", 10) || !strncmp(uri
, "/classes/", 9))
1379 * Use case-insensitive comparison for queue names...
1382 if (loc
->length
> bestlen
&&
1383 strncasecmp(uri
, loc
->location
, loc
->length
) == 0 &&
1384 loc
->location
[0] == '/' &&
1385 (limit
& loc
->limit
) != 0)
1388 bestlen
= loc
->length
;
1394 * Use case-sensitive comparison for other URIs...
1397 if (loc
->length
> bestlen
&&
1398 !strncmp(uri
, loc
->location
, loc
->length
) &&
1399 loc
->location
[0] == '/' &&
1400 (limit
& loc
->limit
) != 0)
1403 bestlen
= loc
->length
;
1409 * Return the match, if any...
1412 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: best = %s",
1413 best
? best
->location
: "NONE");
1420 * 'cupsdFindLocation()' - Find the named location.
1423 cupsd_location_t
* /* O - Location that matches */
1424 cupsdFindLocation(const char *location
) /* I - Connection */
1426 int i
; /* Looping var */
1430 * Loop through the list of locations to find a match...
1433 for (i
= 0; i
< NumLocations
; i
++)
1434 if (!strcasecmp(Locations
[i
].location
, location
))
1435 return (Locations
+ i
);
1442 * 'cupsdGetMD5Passwd()' - Get an MD5 password.
1445 char * /* O - MD5 password string */
1446 cupsdGetMD5Passwd(const char *username
, /* I - Username */
1447 const char *group
, /* I - Group */
1448 char passwd
[33])/* O - MD5 password string */
1450 cups_file_t
*fp
; /* passwd.md5 file */
1451 char filename
[1024], /* passwd.md5 filename */
1452 line
[256], /* Line from file */
1453 tempuser
[33], /* User from file */
1454 tempgroup
[33]; /* Group from file */
1457 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1458 "cupsdGetMD5Passwd(username=\"%s\", group=\"%s\", passwd=%p)",
1459 username
, group
? group
: "(null)", passwd
);
1461 snprintf(filename
, sizeof(filename
), "%s/passwd.md5", ServerRoot
);
1462 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1464 if (errno
!= ENOENT
)
1465 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", filename
,
1471 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
1473 if (sscanf(line
, "%32[^:]:%32[^:]:%32s", tempuser
, tempgroup
, passwd
) != 3)
1475 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad MD5 password line: %s", line
);
1479 if (strcmp(username
, tempuser
) == 0 &&
1480 (group
== NULL
|| strcmp(group
, tempgroup
) == 0))
1483 * Found the password entry!
1486 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Found MD5 user %s, group %s...",
1487 username
, tempgroup
);
1495 * Didn't find a password entry - return NULL!
1504 * 'cupsdIsAuthorized()' - Check to see if the user is authorized...
1507 http_status_t
/* O - HTTP_OK if authorized or error code */
1508 cupsdIsAuthorized(cupsd_client_t
*con
, /* I - Connection */
1509 const char *owner
)/* I - Owner of object */
1511 int i
, j
, /* Looping vars */
1512 auth
; /* Authorization status */
1513 unsigned address
[4]; /* Authorization address */
1514 cupsd_location_t
*best
; /* Best match for location so far */
1515 int hostlen
; /* Length of hostname */
1516 const char *username
; /* Username to authorize */
1517 struct passwd
*pw
; /* User password data */
1518 static const char * const levels
[] = /* Auth levels */
1524 static const char * const types
[] = /* Auth types */
1533 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1534 "cupsdIsAuthorized: con->uri=\"%s\", con->best=%p(%s)",
1535 con
->uri
, con
->best
, con
->best
? con
->best
->location
: "");
1537 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1538 "cupsdIsAuthorized: owner=\"%s\"", owner
);
1541 * If there is no "best" authentication rule for this request, then
1542 * access is allowed from the local system and denied from other
1548 if (!strcmp(con
->http
.hostname
, "localhost") ||
1549 !strcmp(con
->http
.hostname
, ServerName
))
1552 return (HTTP_FORBIDDEN
);
1557 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1558 "cupsdIsAuthorized: level=AUTH_%s, type=AUTH_%s, satisfy=AUTH_SATISFY_%s, num_names=%d",
1559 levels
[best
->level
], types
[best
->type
],
1560 best
->satisfy
? "ANY" : "ALL", best
->num_names
);
1562 if (best
->limit
== AUTH_LIMIT_IPP
)
1563 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: op=%x(%s)",
1564 best
->op
, ippOpString(best
->op
));
1567 * Check host/ip-based accesses...
1571 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
1574 * Copy IPv6 address...
1577 address
[0] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0]);
1578 address
[1] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1]);
1579 address
[2] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]);
1580 address
[3] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[3]);
1583 #endif /* AF_INET6 */
1584 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET
)
1587 * Copy IPv4 address...
1593 address
[3] = ntohl(con
->http
.hostaddr
->ipv4
.sin_addr
.s_addr
);
1596 memset(address
, 0, sizeof(address
));
1598 hostlen
= strlen(con
->http
.hostname
);
1600 if (!strcasecmp(con
->http
.hostname
, "localhost"))
1603 * Access from localhost (127.0.0.1 or :::1) is always allowed...
1611 * Do authorization checks on the domain/address...
1614 switch (best
->order_type
)
1617 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1620 case AUTH_ALLOW
: /* Order Deny,Allow */
1623 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1624 best
->num_deny
, best
->deny
))
1627 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1628 best
->num_allow
, best
->allow
))
1632 case AUTH_DENY
: /* Order Allow,Deny */
1635 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1636 best
->num_allow
, best
->allow
))
1639 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1640 best
->num_deny
, best
->deny
))
1646 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: auth=AUTH_%s...",
1647 auth
? "DENY" : "ALLOW");
1649 if (auth
== AUTH_DENY
&& best
->satisfy
== AUTH_SATISFY_ALL
)
1650 return (HTTP_FORBIDDEN
);
1654 * See if encryption is required...
1657 if (best
->encryption
>= HTTP_ENCRYPT_REQUIRED
&& !con
->http
.tls
)
1659 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1660 "cupsdIsAuthorized: Need upgrade to TLS...");
1661 return (HTTP_UPGRADE_REQUIRED
);
1663 #endif /* HAVE_SSL */
1666 * Now see what access level is required...
1669 if (best
->level
== AUTH_ANON
|| /* Anonymous access - allow it */
1670 (best
->type
== AUTH_NONE
&& best
->num_names
== 0))
1673 if (!con
->username
[0] && best
->type
== AUTH_NONE
&&
1674 best
->limit
== AUTH_LIMIT_IPP
)
1677 * Check for unauthenticated username...
1680 ipp_attribute_t
*attr
; /* requesting-user-name attribute */
1683 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
1686 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1687 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
1688 attr
->values
[0].string
.text
);
1689 username
= attr
->values
[0].string
.text
;
1691 else if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
1692 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1694 return (HTTP_OK
); /* unless overridden with Satisfy */
1698 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: username=\"%s\"",
1701 if (!con
->username
[0])
1703 if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
1704 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1706 return (HTTP_OK
); /* unless overridden with Satisfy */
1709 username
= con
->username
;
1713 * OK, got a username. See if we need normal user access, or group
1714 * access... (root always matches)
1717 if (!strcmp(username
, "root"))
1721 * Get the user info...
1724 pw
= getpwnam(username
);
1727 if (best
->level
== AUTH_USER
)
1730 * If there are no names associated with this location, then
1731 * any valid user is OK...
1734 if (best
->num_names
== 0)
1738 * Otherwise check the user list and return OK if this user is
1742 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1743 "cupsdIsAuthorized: Checking user membership...");
1745 for (i
= 0; i
< best
->num_names
; i
++)
1747 if (!strcasecmp(best
->names
[i
], "@OWNER") && owner
&&
1748 !strcasecmp(username
, owner
))
1750 else if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
1752 for (j
= 0; j
< NumSystemGroups
; j
++)
1753 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
1756 else if (best
->names
[i
][0] == '@')
1758 if (cupsdCheckGroup(username
, pw
, best
->names
[i
] + 1))
1761 else if (!strcasecmp(username
, best
->names
[i
]))
1765 return (HTTP_UNAUTHORIZED
);
1769 * Check to see if this user is in any of the named groups...
1772 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1773 "cupsdIsAuthorized: Checking group membership...");
1776 * Check to see if this user is in any of the named groups...
1779 for (i
= 0; i
< best
->num_names
; i
++)
1781 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1782 "cupsdIsAuthorized: Checking group \"%s\" membership...",
1785 if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
1787 for (j
= 0; j
< NumSystemGroups
; j
++)
1788 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
1791 else if (cupsdCheckGroup(username
, pw
, best
->names
[i
]))
1796 * The user isn't part of the specified group, so deny access...
1799 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1800 "cupsdIsAuthorized: User not in group(s)!");
1802 return (HTTP_UNAUTHORIZED
);
1807 * 'add_allow()' - Add an allow mask to the location.
1810 static cupsd_authmask_t
* /* O - New mask record */
1811 add_allow(cupsd_location_t
*loc
) /* I - Location to add to */
1813 cupsd_authmask_t
*temp
; /* New mask record */
1824 * Try to allocate memory for the record...
1827 if (loc
->num_allow
== 0)
1828 temp
= malloc(sizeof(cupsd_authmask_t
));
1830 temp
= realloc(loc
->allow
, sizeof(cupsd_authmask_t
) * (loc
->num_allow
+ 1));
1836 temp
+= loc
->num_allow
;
1840 * Clear the mask record and return...
1843 memset(temp
, 0, sizeof(cupsd_authmask_t
));
1849 * 'add_deny()' - Add a deny mask to the location.
1852 static cupsd_authmask_t
* /* O - New mask record */
1853 add_deny(cupsd_location_t
*loc
) /* I - Location to add to */
1855 cupsd_authmask_t
*temp
; /* New mask record */
1866 * Try to allocate memory for the record...
1869 if (loc
->num_deny
== 0)
1870 temp
= malloc(sizeof(cupsd_authmask_t
));
1872 temp
= realloc(loc
->deny
, sizeof(cupsd_authmask_t
) * (loc
->num_deny
+ 1));
1878 temp
+= loc
->num_deny
;
1882 * Clear the mask record and return...
1885 memset(temp
, 0, sizeof(cupsd_authmask_t
));
1892 * 'cups_crypt()' - Encrypt the password using the DES or MD5 algorithms,
1896 static char * /* O - Encrypted password */
1897 cups_crypt(const char *pw
, /* I - Password string */
1898 const char *salt
) /* I - Salt (key) string */
1900 if (strncmp(salt
, "$1$", 3) == 0)
1903 * Use MD5 passwords without the benefit of PAM; this is for
1904 * Slackware Linux, and the algorithm was taken from the
1905 * old shadow-19990827/lib/md5crypt.c source code... :(
1908 int i
; /* Looping var */
1909 unsigned long n
; /* Output number */
1910 int pwlen
; /* Length of password string */
1911 const char *salt_end
; /* End of "salt" data for MD5 */
1912 char *ptr
; /* Pointer into result string */
1913 _cups_md5_state_t state
; /* Primary MD5 state info */
1914 _cups_md5_state_t state2
; /* Secondary MD5 state info */
1915 unsigned char digest
[16]; /* MD5 digest result */
1916 static char result
[120]; /* Final password string */
1920 * Get the salt data between dollar signs, e.g. $1$saltdata$md5.
1921 * Get a maximum of 8 characters of salt data after $1$...
1924 for (salt_end
= salt
+ 3; *salt_end
&& (salt_end
- salt
) < 11; salt_end
++)
1925 if (*salt_end
== '$')
1929 * Compute the MD5 sum we need...
1934 _cups_md5_init(&state
);
1935 _cups_md5_append(&state
, (unsigned char *)pw
, pwlen
);
1936 _cups_md5_append(&state
, (unsigned char *)salt
, salt_end
- salt
);
1938 _cups_md5_init(&state2
);
1939 _cups_md5_append(&state2
, (unsigned char *)pw
, pwlen
);
1940 _cups_md5_append(&state2
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
1941 _cups_md5_append(&state2
, (unsigned char *)pw
, pwlen
);
1942 _cups_md5_finish(&state2
, digest
);
1944 for (i
= pwlen
; i
> 0; i
-= 16)
1945 _cups_md5_append(&state
, digest
, i
> 16 ? 16 : i
);
1947 for (i
= pwlen
; i
> 0; i
>>= 1)
1948 _cups_md5_append(&state
, (unsigned char *)((i
& 1) ? "" : pw
), 1);
1950 _cups_md5_finish(&state
, digest
);
1952 for (i
= 0; i
< 1000; i
++)
1954 _cups_md5_init(&state
);
1957 _cups_md5_append(&state
, (unsigned char *)pw
, pwlen
);
1959 _cups_md5_append(&state
, digest
, 16);
1962 _cups_md5_append(&state
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
1965 _cups_md5_append(&state
, (unsigned char *)pw
, pwlen
);
1968 _cups_md5_append(&state
, digest
, 16);
1970 _cups_md5_append(&state
, (unsigned char *)pw
, pwlen
);
1972 _cups_md5_finish(&state
, digest
);
1976 * Copy the final sum to the result string and return...
1979 memcpy(result
, salt
, salt_end
- salt
);
1980 ptr
= result
+ (salt_end
- salt
);
1983 for (i
= 0; i
< 5; i
++, ptr
+= 4)
1985 n
= (((digest
[i
] << 8) | digest
[i
+ 6]) << 8);
1988 n
|= digest
[i
+ 12];
1995 to64(ptr
, digest
[11], 2);
2004 * Use the standard crypt() function...
2007 return (crypt(pw
, salt
));
2010 #endif /* !HAVE_LIBPAM */
2015 * 'pam_func()' - PAM conversation function.
2018 static int /* O - Success or failure */
2020 int num_msg
, /* I - Number of messages */
2021 const struct pam_message
**msg
, /* I - Messages */
2022 struct pam_response
**resp
, /* O - Responses */
2024 /* I - Pointer to connection */
2026 int i
; /* Looping var */
2027 struct pam_response
*replies
; /* Replies */
2028 cupsd_authdata_t
*data
; /* Pointer to auth data */
2032 * Allocate memory for the responses...
2035 if ((replies
= malloc(sizeof(struct pam_response
) * num_msg
)) == NULL
)
2036 return (PAM_CONV_ERR
);
2039 * Answer all of the messages...
2042 DEBUG_printf(("pam_func: appdata_ptr = %p\n", appdata_ptr
));
2046 * Apparently some versions of HP-UX 11 have a broken pam_unix security
2047 * module. This is a workaround...
2053 data
= (cupsd_authdata_t
*)appdata_ptr
;
2056 for (i
= 0; i
< num_msg
; i
++)
2058 DEBUG_printf(("pam_func: Message = \"%s\"\n", msg
[i
]->msg
));
2060 switch (msg
[i
]->msg_style
)
2062 case PAM_PROMPT_ECHO_ON
:
2063 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_ON, returning \"%s\"...\n",
2065 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2066 replies
[i
].resp
= strdup(data
->username
);
2069 case PAM_PROMPT_ECHO_OFF
:
2070 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_OFF, returning \"%s\"...\n",
2072 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2073 replies
[i
].resp
= strdup(data
->password
);
2077 DEBUG_puts("pam_func: PAM_TEXT_INFO...");
2078 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2079 replies
[i
].resp
= NULL
;
2083 DEBUG_puts("pam_func: PAM_ERROR_MSG...");
2084 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2085 replies
[i
].resp
= NULL
;
2089 DEBUG_printf(("pam_func: Unknown PAM message %d...\n",
2090 msg
[i
]->msg_style
));
2092 return (PAM_CONV_ERR
);
2097 * Return the responses back to PAM...
2102 return (PAM_SUCCESS
);
2108 * 'to64()' - Base64-encode an integer value...
2112 to64(char *s
, /* O - Output string */
2113 unsigned long v
, /* I - Value to encode */
2114 int n
) /* I - Number of digits */
2116 const char *itoa64
= "./0123456789"
2117 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2118 "abcdefghijklmnopqrstuvwxyz";
2121 for (; n
> 0; n
--, v
>>= 6)
2122 *s
++ = itoa64
[v
& 0x3f];
2124 #endif /* HAVE_LIBPAM */
2128 * End of "$Id: auth.c 4929 2006-01-13 16:38:43Z mike $".