]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/auth.c
89011f7db89ab0e2ef1cc58fd89f6190a6da9959
2 * "$Id: auth.c 5083 2006-02-06 02:57: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 * compare_locations() - Compare two locations.
49 * cups_crypt() - Encrypt the password using the DES or MD5
50 * algorithms, as needed.
51 * pam_func() - PAM conversation function.
52 * to64() - Base64-encode an integer value...
56 * Include necessary headers...
63 #endif /* HAVE_SHADOW_H */
66 #endif /* HAVE_CRYPT_H */
68 # ifdef HAVE_PAM_PAM_APPL_H
69 # include <pam/pam_appl.h>
71 # include <security/pam_appl.h>
72 # endif /* HAVE_PAM_PAM_APPL_H */
73 #endif /* HAVE_LIBPAM */
76 #endif /* HAVE_USERSEC_H */
77 #ifdef HAVE_MEMBERSHIP_H
78 # include <membership.h>
79 #endif /* HAVE_MEMBERSHIP_H */
86 static cupsd_authmask_t
*add_allow(cupsd_location_t
*loc
);
87 static cupsd_authmask_t
*add_deny(cupsd_location_t
*loc
);
88 static int compare_locations(cupsd_location_t
*a
,
91 static char *cups_crypt(const char *pw
, const char *salt
);
92 #endif /* !HAVE_LIBPAM */
94 static int pam_func(int, const struct pam_message
**,
95 struct pam_response
**, void *);
97 static void to64(char *s
, unsigned long v
, int n
);
98 #endif /* HAVE_LIBPAM */
102 * Local structures...
106 typedef struct cupsd_authdata_s
/**** Authentication data ****/
108 char username
[33], /* Username string */
109 password
[33]; /* Password string */
111 #endif /* HAVE_LIBPAM */
118 #if defined(__hpux) && defined(HAVE_LIBPAM)
119 static cupsd_authdata_t
*auth_datat
; /* Current client being authenticated */
120 #endif /* __hpux && HAVE_LIBPAM */
124 * 'cupsdAddLocation()' - Add a location for authorization.
127 cupsd_location_t
* /* O - Pointer to new location record */
128 cupsdAddLocation(const char *location
) /* I - Location path */
130 cupsd_location_t
*temp
; /* New location */
134 * Make sure the locations array is created...
138 Locations
= cupsArrayNew((cups_array_func_t
)compare_locations
, NULL
);
144 * Try to allocate memory for the new location.
147 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
151 * Initialize the record and copy the name over...
154 temp
->location
= strdup(location
);
155 temp
->length
= strlen(temp
->location
);
157 cupsArrayAdd(Locations
, temp
);
159 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddLocation: added location \'%s\'",
163 * Return the new record...
171 * 'cupsdAddName()' - Add a name to a location...
175 cupsdAddName(cupsd_location_t
*loc
, /* I - Location to add to */
176 char *name
) /* I - Name to add */
178 char **temp
; /* Pointer to names array */
181 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddName(loc=%p, name=\"%s\")",
184 if (loc
->num_names
== 0)
185 temp
= malloc(sizeof(char *));
187 temp
= realloc(loc
->names
, (loc
->num_names
+ 1) * sizeof(char *));
191 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to add name to location %s: %s",
192 loc
->location
, strerror(errno
));
198 if ((temp
[loc
->num_names
] = strdup(name
)) == NULL
)
200 cupsdLogMessage(CUPSD_LOG_ERROR
,
201 "Unable to duplicate name for location %s: %s",
202 loc
->location
, strerror(errno
));
211 * 'cupsdAllowHost()' - Add a host name that is allowed to access the location.
215 cupsdAllowHost(cupsd_location_t
*loc
, /* I - Location to add to */
216 char *name
) /* I - Name of host or domain to add */
218 cupsd_authmask_t
*temp
; /* New host/domain mask */
219 char ifname
[32], /* Interface name */
220 *ifptr
; /* Pointer to end of name */
223 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAllowHost(loc=%p(%s), name=\"%s\")",
224 loc
, loc
->location
, name
);
226 if ((temp
= add_allow(loc
)) == NULL
)
229 if (!strcasecmp(name
, "@LOCAL"))
232 * Allow *interface*...
235 temp
->type
= AUTH_INTERFACE
;
236 temp
->mask
.name
.name
= strdup("*");
237 temp
->mask
.name
.length
= 1;
239 else if (!strncasecmp(name
, "@IF(", 4))
242 * Allow *interface*...
245 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
247 ifptr
= ifname
+ strlen(ifname
);
249 if (ifptr
[-1] == ')')
255 temp
->type
= AUTH_INTERFACE
;
256 temp
->mask
.name
.name
= strdup(ifname
);
257 temp
->mask
.name
.length
= ifptr
- ifname
;
265 temp
->type
= AUTH_NAME
;
266 temp
->mask
.name
.name
= strdup(name
);
267 temp
->mask
.name
.length
= strlen(name
);
273 * 'cupsdAllowIP()' - Add an IP address or network that is allowed to access
278 cupsdAllowIP(cupsd_location_t
*loc
, /* I - Location to add to */
279 unsigned address
[4], /* I - IP address to add */
280 unsigned netmask
[4]) /* I - Netmask of address */
282 cupsd_authmask_t
*temp
; /* New host/domain mask */
285 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
286 "cupsdAllowIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
287 loc
, loc
->location
, address
[0], address
[1], address
[2],
288 address
[3], netmask
[0], netmask
[1], netmask
[2],
291 if ((temp
= add_allow(loc
)) == NULL
)
294 temp
->type
= AUTH_IP
;
295 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
296 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
301 * 'cupsdAuthorize()' - Validate any authorization credentials.
305 cupsdAuthorize(cupsd_client_t
*con
) /* I - Client connection */
307 int type
; /* Authentication type */
308 char *authorization
, /* Pointer into Authorization string */
309 *ptr
, /* Pointer into string */
310 username
[65], /* Username string */
311 password
[33]; /* Password string */
312 const char *localuser
; /* Certificate username */
313 char nonce
[HTTP_MAX_VALUE
], /* Nonce value from client */
314 md5
[33], /* MD5 password */
315 basicmd5
[33]; /* MD5 of Basic password */
316 static const char * const states
[] = /* HTTP client states... */
336 * Locate the best matching location so we know what kind of
337 * authentication to expect...
340 con
->best
= cupsdFindBest(con
->uri
, con
->http
.state
);
342 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
343 "cupsdAuthorize: con->uri=\"%s\", con->best=%p(%s)",
344 con
->uri
, con
->best
, con
->best
? con
->best
->location
: "");
346 if (con
->best
&& con
->best
->type
!= AUTH_NONE
)
347 type
= con
->best
->type
;
349 type
= DefaultAuthType
;
352 * Decode the Authorization string...
355 authorization
= con
->http
.fields
[HTTP_FIELD_AUTHORIZATION
];
357 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAuthorize: Authorization=\"%s\"",
363 if (type
== AUTH_NONE
)
366 * No authorization required, return early...
369 cupsdLogMessage(CUPSD_LOG_DEBUG
,
370 "cupsdAuthorize: No authentication required.");
373 else if (!*authorization
)
376 * No authorization data provided, return early...
379 cupsdLogMessage(CUPSD_LOG_DEBUG
,
380 "cupsdAuthorize: No authentication data provided.");
383 else if (!strncmp(authorization
, "Local", 5) &&
384 !strcasecmp(con
->http
.hostname
, "localhost"))
387 * Get Local certificate authentication data...
391 while (isspace(*authorization
))
394 if ((localuser
= cupsdFindCert(authorization
)) != NULL
)
395 strlcpy(username
, localuser
, sizeof(username
));
398 cupsdLogMessage(CUPSD_LOG_ERROR
,
399 "cupsdAuthorize: Local authentication certificate not "
404 else if (!strncmp(authorization
, "Basic", 5) &&
405 (type
== AUTH_BASIC
|| type
== AUTH_BASICDIGEST
))
408 * Get the Basic authentication data...
411 int userlen
; /* Username:password length */
415 while (isspace(*authorization
))
418 userlen
= sizeof(username
);
419 httpDecode64_2(username
, &userlen
, authorization
);
422 * Pull the username and password out...
425 if ((ptr
= strchr(username
, ':')) == NULL
)
427 cupsdLogMessage(CUPSD_LOG_ERROR
,
428 "cupsdAuthorize: Missing Basic password!");
437 * Username must not be empty...
440 cupsdLogMessage(CUPSD_LOG_ERROR
,
441 "cupsdAuthorize: Empty Basic username!");
448 * Password must not be empty...
451 cupsdLogMessage(CUPSD_LOG_ERROR
,
452 "cupsdAuthorize: Empty Basic password!");
456 strlcpy(password
, ptr
, sizeof(password
));
459 * Validate the username and password...
468 * Only use PAM to do authentication. This supports MD5
469 * passwords, among other things...
472 pam_handle_t
*pamh
; /* PAM authentication handle */
473 int pamerr
; /* PAM error code */
474 struct pam_conv pamdata
;/* PAM conversation data */
475 cupsd_authdata_t data
; /* Authentication data */
478 strlcpy(data
.username
, username
, sizeof(data
.username
));
479 strlcpy(data
.password
, password
, sizeof(data
.password
));
481 pamdata
.conv
= pam_func
;
482 pamdata
.appdata_ptr
= &data
;
486 * Workaround for HP-UX bug in pam_unix; see pam_func() below for
493 pamerr
= pam_start("cups", username
, &pamdata
, &pamh
);
494 if (pamerr
!= PAM_SUCCESS
)
496 cupsdLogMessage(CUPSD_LOG_ERROR
,
497 "cupsdAuthorize: pam_start() returned %d (%s)!\n",
498 pamerr
, pam_strerror(pamh
, pamerr
));
503 pamerr
= pam_authenticate(pamh
, PAM_SILENT
);
504 if (pamerr
!= PAM_SUCCESS
)
506 cupsdLogMessage(CUPSD_LOG_ERROR
,
507 "cupsdAuthorize: pam_authenticate() returned %d "
509 pamerr
, pam_strerror(pamh
, pamerr
));
514 pamerr
= pam_acct_mgmt(pamh
, PAM_SILENT
);
515 if (pamerr
!= PAM_SUCCESS
)
517 cupsdLogMessage(CUPSD_LOG_ERROR
,
518 "cupsdAuthorize: pam_acct_mgmt() returned %d "
520 pamerr
, pam_strerror(pamh
, pamerr
));
525 pam_end(pamh
, PAM_SUCCESS
);
527 #elif defined(HAVE_USERSEC_H)
529 * Use AIX authentication interface...
532 char *authmsg
; /* Authentication message */
533 char *loginmsg
; /* Login message */
534 int reenter
; /* ??? */
537 cupsdLogMessage(CUPSD_LOG_DEBUG
,
538 "cupsdAuthorize: AIX authenticate of username \"%s\"",
542 if (authenticate(username
, password
, &reenter
, &authmsg
) != 0)
544 cupsdLogMessage(CUPSD_LOG_DEBUG
,
545 "cupsdAuthorize: Unable to authenticate username "
547 username
, strerror(errno
));
553 * Use normal UNIX password file-based authentication...
556 char *pass
; /* Encrypted password */
557 struct passwd
*pw
; /* User password data */
558 # ifdef HAVE_SHADOW_H
559 struct spwd
*spw
; /* Shadow password data */
560 # endif /* HAVE_SHADOW_H */
563 pw
= getpwnam(username
); /* Get the current password */
564 endpwent(); /* Close the password file */
572 cupsdLogMessage(CUPSD_LOG_ERROR
,
573 "cupsdAuthorize: Unknown username \"%s\"!",
575 return (HTTP_UNAUTHORIZED
);
578 # ifdef HAVE_SHADOW_H
579 spw
= getspnam(username
);
582 if (!spw
&& !strcmp(pw
->pw_passwd
, "x"))
585 * Don't allow blank passwords!
588 cupsdLogMessage(CUPSD_LOG_ERROR
,
589 "cupsdAuthorize: Username \"%s\" has no shadow "
590 "password!", username
);
594 if (spw
&& !spw
->sp_pwdp
[0] && !pw
->pw_passwd
[0])
596 if (!pw
->pw_passwd
[0])
597 # endif /* HAVE_SHADOW_H */
600 * Don't allow blank passwords!
603 cupsdLogMessage(CUPSD_LOG_ERROR
,
604 "cupsdAuthorize: Username \"%s\" has no password!",
610 * OK, the password isn't blank, so compare with what came from the
614 pass
= cups_crypt(password
, pw
->pw_passwd
);
616 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
617 "cupsdAuthorize: pw_passwd=\"%s\", crypt=\"%s\"",
618 pw
->pw_passwd
, pass
);
620 if (!pass
|| strcmp(pw
->pw_passwd
, pass
))
622 # ifdef HAVE_SHADOW_H
625 pass
= cups_crypt(password
, spw
->sp_pwdp
);
627 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
628 "cupsdAuthorize: sp_pwdp=\"%s\", crypt=\"%s\"",
631 if (pass
== NULL
|| strcmp(spw
->sp_pwdp
, pass
))
633 cupsdLogMessage(CUPSD_LOG_ERROR
,
634 "cupsdAuthorize: Authentication failed for "
641 # endif /* HAVE_SHADOW_H */
643 cupsdLogMessage(CUPSD_LOG_ERROR
,
644 "cupsdAuthorize: Authentication failed for "
650 #endif /* HAVE_LIBPAM */
654 case AUTH_BASICDIGEST
:
656 * Do Basic authentication with the Digest password file...
659 if (!cupsdGetMD5Passwd(username
, NULL
, md5
))
661 cupsdLogMessage(CUPSD_LOG_ERROR
,
662 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
667 httpMD5(username
, "CUPS", password
, basicmd5
);
669 if (strcmp(md5
, basicmd5
))
671 cupsdLogMessage(CUPSD_LOG_ERROR
,
672 "cupsdAuthorize: Authentication failed for \"%s\"!",
679 else if (!strncmp(authorization
, "Digest", 6) && type
== AUTH_DIGEST
)
682 * Get the username, password, and nonce from the Digest attributes...
685 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "username",
686 username
, sizeof(username
)) || !username
[0])
689 * Username must not be empty...
692 cupsdLogMessage(CUPSD_LOG_ERROR
,
693 "cupsdAuthorize: Empty or missing Digest username!");
697 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "response",
698 password
, sizeof(password
)) || !password
[0])
701 * Password must not be empty...
704 cupsdLogMessage(CUPSD_LOG_ERROR
,
705 "cupsdAuthorize: Empty or missing Digest password!");
709 if (!httpGetSubField(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "nonce",
712 cupsdLogMessage(CUPSD_LOG_ERROR
,
713 "cupsdAuthorize: No nonce value for Digest "
718 if (strcmp(con
->http
.hostname
, nonce
))
720 cupsdLogMessage(CUPSD_LOG_ERROR
,
721 "cupsdAuthorize: Bad nonce value, expected \"%s\", "
722 "got \"%s\"!", con
->http
.hostname
, nonce
);
727 * Validate the username and password...
730 if (!cupsdGetMD5Passwd(username
, NULL
, md5
))
732 cupsdLogMessage(CUPSD_LOG_ERROR
,
733 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
738 httpMD5Final(nonce
, states
[con
->http
.state
], con
->uri
, md5
);
740 if (strcmp(md5
, password
))
742 cupsdLogMessage(CUPSD_LOG_ERROR
,
743 "cupsdAuthorize: Authentication failed for \"%s\"!",
750 cupsdLogMessage(CUPSD_LOG_DEBUG
,
751 "cupsdAuthorize: Bad authentication data.");
756 * If we get here, then we were able to validate the username and
757 * password - copy the validated username and password to the client
761 strlcpy(con
->username
, username
, sizeof(con
->username
));
762 strlcpy(con
->password
, password
, sizeof(con
->password
));
764 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: username=\"%s\"",
770 * 'cupsdCheckAuth()' - Check authorization masks.
773 int /* O - 1 if mask matches, 0 otherwise */
775 unsigned ip
[4], /* I - Client address */
776 char *name
, /* I - Client hostname */
777 int name_len
, /* I - Length of hostname */
778 int num_masks
, /* I - Number of masks */
779 cupsd_authmask_t
*masks
) /* I - Masks */
781 int i
; /* Looping var */
782 cupsd_netif_t
*iface
; /* Network interface */
783 unsigned netip4
; /* IPv4 network address */
785 unsigned netip6
[4]; /* IPv6 network address */
786 #endif /* AF_INET6 */
788 while (num_masks
> 0)
792 case AUTH_INTERFACE
:
794 * Check for a match with a network interface...
797 netip4
= htonl(ip
[3]);
800 netip6
[0] = htonl(ip
[0]);
801 netip6
[1] = htonl(ip
[1]);
802 netip6
[2] = htonl(ip
[2]);
803 netip6
[3] = htonl(ip
[3]);
804 #endif /* AF_INET6 */
806 if (!strcmp(masks
->mask
.name
.name
, "*"))
809 * Check against all local interfaces...
814 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
816 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
819 * Only check local interfaces...
822 if (!iface
->is_local
)
825 if (iface
->address
.addr
.sa_family
== AF_INET
)
828 * Check IPv4 address...
831 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
832 (iface
->address
.ipv4
.sin_addr
.s_addr
&
833 iface
->mask
.ipv4
.sin_addr
.s_addr
))
840 * Check IPv6 address...
843 for (i
= 0; i
< 4; i
++)
844 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
845 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
846 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
852 #endif /* AF_INET6 */
858 * Check the named interface...
861 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
862 iface
&& !strcmp(masks
->mask
.name
.name
, iface
->name
);
863 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
865 if (iface
->address
.addr
.sa_family
== AF_INET
)
868 * Check IPv4 address...
871 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
872 (iface
->address
.ipv4
.sin_addr
.s_addr
&
873 iface
->mask
.ipv4
.sin_addr
.s_addr
))
880 * Check IPv6 address...
883 for (i
= 0; i
< 4; i
++)
884 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
885 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
886 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
892 #endif /* AF_INET6 */
899 * Check for exact name match...
902 if (!strcasecmp(name
, masks
->mask
.name
.name
))
906 * Check for domain match...
909 if (name_len
>= masks
->mask
.name
.length
&&
910 masks
->mask
.name
.name
[0] == '.' &&
911 !strcasecmp(name
+ name_len
- masks
->mask
.name
.length
,
912 masks
->mask
.name
.name
))
918 * Check for IP/network address match...
921 for (i
= 0; i
< 4; i
++)
922 if ((ip
[i
] & masks
->mask
.ip
.netmask
[i
]) !=
923 masks
->mask
.ip
.address
[i
])
940 * 'cupsdCheckGroup()' - Check for a user's group membership.
943 int /* O - 1 if user is a member, 0 otherwise */
945 const char *username
, /* I - User name */
946 struct passwd
*user
, /* I - System user info */
947 const char *groupname
) /* I - Group name */
949 int i
; /* Looping var */
950 struct group
*group
; /* System group info */
951 char junk
[33]; /* MD5 password (not used) */
952 #ifdef HAVE_MBR_UID_TO_UUID
953 uuid_t useruuid
, /* UUID for username */
954 groupuuid
; /* UUID for groupname */
955 int is_member
; /* True if user is a member of group */
956 #endif /* HAVE_MBR_UID_TO_UUID */
959 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
960 "cupsdCheckGroup(username=\"%s\", user=%p, groupname=\"%s\")",
961 username
, user
, groupname
);
967 if (!username
|| !groupname
)
971 * Check to see if the user is a member of the named group...
974 group
= getgrnam(groupname
);
980 * Group exists, check it...
983 for (i
= 0; group
->gr_mem
[i
]; i
++)
984 if (!strcasecmp(username
, group
->gr_mem
[i
]))
989 * Group doesn't exist or user not in group list, check the group ID
990 * against the user's group ID...
993 if (user
&& group
&& group
->gr_gid
== user
->pw_gid
)
996 #ifdef HAVE_MBR_UID_TO_UUID
998 * Check group membership through MacOS X membership API...
1002 if (!mbr_uid_to_uuid(user
->pw_uid
, useruuid
))
1003 if (!mbr_gid_to_uuid(group
->gr_gid
, groupuuid
))
1004 if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1007 #endif /* HAVE_MBR_UID_TO_UUID */
1010 * Username not found, group not found, or user is not part of the
1011 * system group... Check for a user and group in the MD5 password
1015 if (cupsdGetMD5Passwd(username
, groupname
, junk
) != NULL
)
1019 * If we get this far, then the user isn't part of the named group...
1027 * 'cupsdCopyLocation()' - Make a copy of a location...
1030 cupsd_location_t
* /* O - New location */
1032 cupsd_location_t
**loc
) /* IO - Original location */
1034 int i
; /* Looping var */
1035 cupsd_location_t
*temp
; /* New location */
1036 char location
[HTTP_MAX_URI
];
1037 /* Location of resource */
1041 * Use a local copy of location because cupsdAddLocation may cause
1042 * this memory to be moved...
1045 strlcpy(location
, (*loc
)->location
, sizeof(location
));
1047 if ((temp
= cupsdAddLocation(location
)) == NULL
)
1051 * Copy the information from the original location to the new one.
1054 temp
->limit
= (*loc
)->limit
;
1055 temp
->order_type
= (*loc
)->order_type
;
1056 temp
->type
= (*loc
)->type
;
1057 temp
->level
= (*loc
)->level
;
1058 temp
->satisfy
= (*loc
)->satisfy
;
1059 temp
->encryption
= (*loc
)->encryption
;
1061 if ((temp
->num_names
= (*loc
)->num_names
) > 0)
1064 * Copy the names array...
1067 if ((temp
->names
= calloc(temp
->num_names
, sizeof(char *))) == NULL
)
1069 cupsdLogMessage(CUPSD_LOG_ERROR
,
1070 "cupsdCopyLocation: Unable to allocate memory for %d names: %s",
1071 temp
->num_names
, strerror(errno
));
1073 cupsdDeleteLocation(temp
);
1077 for (i
= 0; i
< temp
->num_names
; i
++)
1078 if ((temp
->names
[i
] = strdup((*loc
)->names
[i
])) == NULL
)
1080 cupsdLogMessage(CUPSD_LOG_ERROR
,
1081 "cupsdCopyLocation: Unable to copy name \"%s\": %s",
1082 (*loc
)->names
[i
], strerror(errno
));
1084 cupsdDeleteLocation(temp
);
1089 if ((temp
->num_allow
= (*loc
)->num_allow
) > 0)
1092 * Copy allow rules...
1095 if ((temp
->allow
= calloc(temp
->num_allow
, sizeof(cupsd_authmask_t
))) == NULL
)
1097 cupsdLogMessage(CUPSD_LOG_ERROR
,
1098 "cupsdCopyLocation: Unable to allocate memory for %d allow rules: %s",
1099 temp
->num_allow
, strerror(errno
));
1100 cupsdDeleteLocation(temp
);
1104 for (i
= 0; i
< temp
->num_allow
; i
++)
1105 switch (temp
->allow
[i
].type
= (*loc
)->allow
[i
].type
)
1108 temp
->allow
[i
].mask
.name
.length
= (*loc
)->allow
[i
].mask
.name
.length
;
1109 temp
->allow
[i
].mask
.name
.name
= strdup((*loc
)->allow
[i
].mask
.name
.name
);
1111 if (temp
->allow
[i
].mask
.name
.name
== NULL
)
1113 cupsdLogMessage(CUPSD_LOG_ERROR
,
1114 "cupsdCopyLocation: Unable to copy allow name \"%s\": %s",
1115 (*loc
)->allow
[i
].mask
.name
.name
, strerror(errno
));
1116 cupsdDeleteLocation(temp
);
1121 memcpy(&(temp
->allow
[i
].mask
.ip
), &((*loc
)->allow
[i
].mask
.ip
),
1122 sizeof(cupsd_ipmask_t
));
1127 if ((temp
->num_deny
= (*loc
)->num_deny
) > 0)
1130 * Copy deny rules...
1133 if ((temp
->deny
= calloc(temp
->num_deny
, sizeof(cupsd_authmask_t
))) == NULL
)
1135 cupsdLogMessage(CUPSD_LOG_ERROR
,
1136 "cupsdCopyLocation: Unable to allocate memory for %d deny rules: %s",
1137 temp
->num_deny
, strerror(errno
));
1138 cupsdDeleteLocation(temp
);
1142 for (i
= 0; i
< temp
->num_deny
; i
++)
1143 switch (temp
->deny
[i
].type
= (*loc
)->deny
[i
].type
)
1146 temp
->deny
[i
].mask
.name
.length
= (*loc
)->deny
[i
].mask
.name
.length
;
1147 temp
->deny
[i
].mask
.name
.name
= strdup((*loc
)->deny
[i
].mask
.name
.name
);
1149 if (temp
->deny
[i
].mask
.name
.name
== NULL
)
1151 cupsdLogMessage(CUPSD_LOG_ERROR
,
1152 "cupsdCopyLocation: Unable to copy deny name \"%s\": %s",
1153 (*loc
)->deny
[i
].mask
.name
.name
, strerror(errno
));
1154 cupsdDeleteLocation(temp
);
1159 memcpy(&(temp
->deny
[i
].mask
.ip
), &((*loc
)->deny
[i
].mask
.ip
),
1160 sizeof(cupsd_ipmask_t
));
1170 * 'cupsdDeleteAllLocations()' - Free all memory used for location authorization.
1174 cupsdDeleteAllLocations(void)
1176 cupsd_location_t
*loc
; /* Current location */
1180 * Free all of the allow/deny records first...
1183 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1185 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1186 cupsdDeleteLocation(loc
);
1189 * Then free the location array...
1192 cupsArrayDelete(Locations
);
1198 * 'cupsdDeleteLocation()' - Free all memory used by a location.
1202 cupsdDeleteLocation(
1203 cupsd_location_t
*loc
) /* I - Location to delete */
1205 int i
; /* Looping var */
1206 cupsd_authmask_t
*mask
; /* Current mask */
1209 cupsArrayRemove(Locations
, loc
);
1211 for (i
= loc
->num_names
- 1; i
>= 0; i
--)
1212 free(loc
->names
[i
]);
1214 if (loc
->num_names
> 0)
1217 for (i
= loc
->num_allow
, mask
= loc
->allow
; i
> 0; i
--, mask
++)
1218 if (mask
->type
== AUTH_NAME
|| mask
->type
== AUTH_INTERFACE
)
1219 free(mask
->mask
.name
.name
);
1221 if (loc
->num_allow
> 0)
1224 for (i
= loc
->num_deny
, mask
= loc
->deny
; i
> 0; i
--, mask
++)
1225 if (mask
->type
== AUTH_NAME
|| mask
->type
== AUTH_INTERFACE
)
1226 free(mask
->mask
.name
.name
);
1228 if (loc
->num_deny
> 0)
1231 free(loc
->location
);
1237 * 'cupsdDenyHost()' - Add a host name that is not allowed to access the
1242 cupsdDenyHost(cupsd_location_t
*loc
, /* I - Location to add to */
1243 char *name
) /* I - Name of host or domain to add */
1245 cupsd_authmask_t
*temp
; /* New host/domain mask */
1246 char ifname
[32], /* Interface name */
1247 *ifptr
; /* Pointer to end of name */
1250 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDenyHost(loc=%p(%s), name=\"%s\")",
1251 loc
, loc
->location
, name
);
1253 if ((temp
= add_deny(loc
)) == NULL
)
1256 if (!strcasecmp(name
, "@LOCAL"))
1259 * Deny *interface*...
1262 temp
->type
= AUTH_INTERFACE
;
1263 temp
->mask
.name
.name
= strdup("*");
1264 temp
->mask
.name
.length
= 1;
1266 else if (!strncasecmp(name
, "@IF(", 4))
1269 * Deny *interface*...
1272 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
1274 ifptr
= ifname
+ strlen(ifname
);
1276 if (ifptr
[-1] == ')')
1282 temp
->type
= AUTH_INTERFACE
;
1283 temp
->mask
.name
.name
= strdup(ifname
);
1284 temp
->mask
.name
.length
= ifptr
- ifname
;
1292 temp
->type
= AUTH_NAME
;
1293 temp
->mask
.name
.name
= strdup(name
);
1294 temp
->mask
.name
.length
= strlen(name
);
1300 * 'cupsdDenyIP()' - Add an IP address or network that is not allowed to
1301 * access the location.
1305 cupsdDenyIP(cupsd_location_t
*loc
, /* I - Location to add to */
1306 unsigned address
[4],/* I - IP address to add */
1307 unsigned netmask
[4])/* I - Netmask of address */
1309 cupsd_authmask_t
*temp
; /* New host/domain mask */
1312 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1313 "cupsdDenyIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
1314 loc
, loc
->location
, address
[0], address
[1], address
[2],
1315 address
[3], netmask
[0], netmask
[1], netmask
[2],
1318 if ((temp
= add_deny(loc
)) == NULL
)
1321 temp
->type
= AUTH_IP
;
1322 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
1323 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
1328 * 'cupsdFindBest()' - Find the location entry that best matches the resource.
1331 cupsd_location_t
* /* O - Location that matches */
1332 cupsdFindBest(const char *path
, /* I - Resource path */
1333 http_state_t state
) /* I - HTTP state/request */
1335 char uri
[HTTP_MAX_URI
],
1336 /* URI in request... */
1337 *uriptr
; /* Pointer into URI */
1338 cupsd_location_t
*loc
, /* Current location */
1339 *best
; /* Best match for location so far */
1340 int bestlen
; /* Length of best match */
1341 int limit
; /* Limit field */
1342 static const int limits
[] = /* Map http_status_t to AUTH_LIMIT_xyz */
1362 * First copy the connection URI to a local string so we have drop
1363 * any .ppd extension from the pathname in /printers or /classes
1367 strlcpy(uri
, path
, sizeof(uri
));
1369 if (!strncmp(uri
, "/printers/", 10) ||
1370 !strncmp(uri
, "/classes/", 9))
1373 * Check if the URI has .ppd on the end...
1376 uriptr
= uri
+ strlen(uri
) - 4; /* len > 4 if we get here... */
1378 if (!strcmp(uriptr
, ".ppd"))
1382 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: uri = \"%s\"...", uri
);
1385 * Loop through the list of locations to find a match...
1388 limit
= limits
[state
];
1392 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1394 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1396 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: Location %s Limit %x",
1397 loc
->location
, loc
->limit
);
1399 if (!strncmp(uri
, "/printers/", 10) || !strncmp(uri
, "/classes/", 9))
1402 * Use case-insensitive comparison for queue names...
1405 if (loc
->length
> bestlen
&&
1406 !strncasecmp(uri
, loc
->location
, loc
->length
) &&
1407 loc
->location
[0] == '/' &&
1408 (limit
& loc
->limit
) != 0)
1411 bestlen
= loc
->length
;
1417 * Use case-sensitive comparison for other URIs...
1420 if (loc
->length
> bestlen
&&
1421 !strncmp(uri
, loc
->location
, loc
->length
) &&
1422 loc
->location
[0] == '/' &&
1423 (limit
& loc
->limit
) != 0)
1426 bestlen
= loc
->length
;
1432 * Return the match, if any...
1435 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: best = %s",
1436 best
? best
->location
: "NONE");
1443 * 'cupsdFindLocation()' - Find the named location.
1446 cupsd_location_t
* /* O - Location that matches */
1447 cupsdFindLocation(const char *location
) /* I - Connection */
1449 cupsd_location_t key
; /* Search key */
1452 key
.location
= (char *)location
;
1454 return ((cupsd_location_t
*)cupsArrayFind(Locations
, &key
));
1459 * 'cupsdGetMD5Passwd()' - Get an MD5 password.
1462 char * /* O - MD5 password string */
1463 cupsdGetMD5Passwd(const char *username
, /* I - Username */
1464 const char *group
, /* I - Group */
1465 char passwd
[33])/* O - MD5 password string */
1467 cups_file_t
*fp
; /* passwd.md5 file */
1468 char filename
[1024], /* passwd.md5 filename */
1469 line
[256], /* Line from file */
1470 tempuser
[33], /* User from file */
1471 tempgroup
[33]; /* Group from file */
1474 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1475 "cupsdGetMD5Passwd(username=\"%s\", group=\"%s\", passwd=%p)",
1476 username
, group
? group
: "(null)", passwd
);
1478 snprintf(filename
, sizeof(filename
), "%s/passwd.md5", ServerRoot
);
1479 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1481 if (errno
!= ENOENT
)
1482 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", filename
,
1488 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
1490 if (sscanf(line
, "%32[^:]:%32[^:]:%32s", tempuser
, tempgroup
, passwd
) != 3)
1492 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad MD5 password line: %s", line
);
1496 if (!strcmp(username
, tempuser
) &&
1497 (group
== NULL
|| !strcmp(group
, tempgroup
)))
1500 * Found the password entry!
1503 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Found MD5 user %s, group %s...",
1504 username
, tempgroup
);
1512 * Didn't find a password entry - return NULL!
1521 * 'cupsdIsAuthorized()' - Check to see if the user is authorized...
1524 http_status_t
/* O - HTTP_OK if authorized or error code */
1525 cupsdIsAuthorized(cupsd_client_t
*con
, /* I - Connection */
1526 const char *owner
)/* I - Owner of object */
1528 int i
, j
, /* Looping vars */
1529 auth
; /* Authorization status */
1530 unsigned address
[4]; /* Authorization address */
1531 cupsd_location_t
*best
; /* Best match for location so far */
1532 int hostlen
; /* Length of hostname */
1533 const char *username
; /* Username to authorize */
1534 struct passwd
*pw
; /* User password data */
1535 static const char * const levels
[] = /* Auth levels */
1541 static const char * const types
[] = /* Auth types */
1550 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1551 "cupsdIsAuthorized: con->uri=\"%s\", con->best=%p(%s)",
1552 con
->uri
, con
->best
, con
->best
? con
->best
->location
: "");
1554 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1555 "cupsdIsAuthorized: owner=\"%s\"", owner
);
1558 * If there is no "best" authentication rule for this request, then
1559 * access is allowed from the local system and denied from other
1565 if (!strcmp(con
->http
.hostname
, "localhost") ||
1566 !strcmp(con
->http
.hostname
, ServerName
))
1569 return (HTTP_FORBIDDEN
);
1574 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1575 "cupsdIsAuthorized: level=AUTH_%s, type=AUTH_%s, "
1576 "satisfy=AUTH_SATISFY_%s, num_names=%d",
1577 levels
[best
->level
], types
[best
->type
],
1578 best
->satisfy
? "ANY" : "ALL", best
->num_names
);
1580 if (best
->limit
== AUTH_LIMIT_IPP
)
1581 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: op=%x(%s)",
1582 best
->op
, ippOpString(best
->op
));
1585 * Check host/ip-based accesses...
1589 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
1592 * Copy IPv6 address...
1595 address
[0] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0]);
1596 address
[1] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1]);
1597 address
[2] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]);
1598 address
[3] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[3]);
1601 #endif /* AF_INET6 */
1602 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET
)
1605 * Copy IPv4 address...
1611 address
[3] = ntohl(con
->http
.hostaddr
->ipv4
.sin_addr
.s_addr
);
1614 memset(address
, 0, sizeof(address
));
1616 hostlen
= strlen(con
->http
.hostname
);
1618 if (!strcasecmp(con
->http
.hostname
, "localhost"))
1621 * Access from localhost (127.0.0.1 or :::1) is always allowed...
1629 * Do authorization checks on the domain/address...
1632 switch (best
->order_type
)
1635 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1638 case AUTH_ALLOW
: /* Order Deny,Allow */
1641 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1642 best
->num_deny
, best
->deny
))
1645 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1646 best
->num_allow
, best
->allow
))
1650 case AUTH_DENY
: /* Order Allow,Deny */
1653 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1654 best
->num_allow
, best
->allow
))
1657 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1658 best
->num_deny
, best
->deny
))
1664 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: auth=AUTH_%s...",
1665 auth
? "DENY" : "ALLOW");
1667 if (auth
== AUTH_DENY
&& best
->satisfy
== AUTH_SATISFY_ALL
)
1668 return (HTTP_FORBIDDEN
);
1672 * See if encryption is required...
1675 if (best
->encryption
>= HTTP_ENCRYPT_REQUIRED
&& !con
->http
.tls
&&
1676 best
->satisfy
== AUTH_SATISFY_ALL
)
1678 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1679 "cupsdIsAuthorized: Need upgrade to TLS...");
1680 return (HTTP_UPGRADE_REQUIRED
);
1682 #endif /* HAVE_SSL */
1685 * Now see what access level is required...
1688 if (best
->level
== AUTH_ANON
|| /* Anonymous access - allow it */
1689 (best
->type
== AUTH_NONE
&& best
->num_names
== 0))
1692 if (!con
->username
[0] && best
->type
== AUTH_NONE
&&
1693 best
->limit
== AUTH_LIMIT_IPP
)
1696 * Check for unauthenticated username...
1699 ipp_attribute_t
*attr
; /* requesting-user-name attribute */
1702 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
1705 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1706 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
1707 attr
->values
[0].string
.text
);
1708 username
= attr
->values
[0].string
.text
;
1710 else if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
1711 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1713 return (HTTP_OK
); /* unless overridden with Satisfy */
1717 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: username=\"%s\"",
1720 if (!con
->username
[0])
1722 if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
1723 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1725 return (HTTP_OK
); /* unless overridden with Satisfy */
1728 username
= con
->username
;
1732 * OK, got a username. See if we need normal user access, or group
1733 * access... (root always matches)
1736 if (!strcmp(username
, "root"))
1740 * Get the user info...
1743 pw
= getpwnam(username
);
1746 if (best
->level
== AUTH_USER
)
1749 * If there are no names associated with this location, then
1750 * any valid user is OK...
1753 if (best
->num_names
== 0)
1757 * Otherwise check the user list and return OK if this user is
1761 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1762 "cupsdIsAuthorized: Checking user membership...");
1764 for (i
= 0; i
< best
->num_names
; i
++)
1766 if (!strcasecmp(best
->names
[i
], "@OWNER") && owner
&&
1767 !strcasecmp(username
, owner
))
1769 else if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
1771 for (j
= 0; j
< NumSystemGroups
; j
++)
1772 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
1775 else if (best
->names
[i
][0] == '@')
1777 if (cupsdCheckGroup(username
, pw
, best
->names
[i
] + 1))
1780 else if (!strcasecmp(username
, best
->names
[i
]))
1784 return (HTTP_UNAUTHORIZED
);
1788 * Check to see if this user is in any of the named groups...
1791 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1792 "cupsdIsAuthorized: Checking group membership...");
1795 * Check to see if this user is in any of the named groups...
1798 for (i
= 0; i
< best
->num_names
; i
++)
1800 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1801 "cupsdIsAuthorized: Checking group \"%s\" membership...",
1804 if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
1806 for (j
= 0; j
< NumSystemGroups
; j
++)
1807 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
1810 else if (cupsdCheckGroup(username
, pw
, best
->names
[i
]))
1815 * The user isn't part of the specified group, so deny access...
1818 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1819 "cupsdIsAuthorized: User not in group(s)!");
1821 return (HTTP_UNAUTHORIZED
);
1826 * 'add_allow()' - Add an allow mask to the location.
1829 static cupsd_authmask_t
* /* O - New mask record */
1830 add_allow(cupsd_location_t
*loc
) /* I - Location to add to */
1832 cupsd_authmask_t
*temp
; /* New mask record */
1843 * Try to allocate memory for the record...
1846 if (loc
->num_allow
== 0)
1847 temp
= malloc(sizeof(cupsd_authmask_t
));
1849 temp
= realloc(loc
->allow
, sizeof(cupsd_authmask_t
) * (loc
->num_allow
+ 1));
1855 temp
+= loc
->num_allow
;
1859 * Clear the mask record and return...
1862 memset(temp
, 0, sizeof(cupsd_authmask_t
));
1868 * 'add_deny()' - Add a deny mask to the location.
1871 static cupsd_authmask_t
* /* O - New mask record */
1872 add_deny(cupsd_location_t
*loc
) /* I - Location to add to */
1874 cupsd_authmask_t
*temp
; /* New mask record */
1885 * Try to allocate memory for the record...
1888 if (loc
->num_deny
== 0)
1889 temp
= malloc(sizeof(cupsd_authmask_t
));
1891 temp
= realloc(loc
->deny
, sizeof(cupsd_authmask_t
) * (loc
->num_deny
+ 1));
1897 temp
+= loc
->num_deny
;
1901 * Clear the mask record and return...
1904 memset(temp
, 0, sizeof(cupsd_authmask_t
));
1910 * 'compare_locations()' - Compare two locations.
1913 static int /* O - Result of comparison */
1914 compare_locations(cupsd_location_t
*a
, /* I - First location */
1915 cupsd_location_t
*b
) /* I - Second location */
1917 return (strcmp(b
->location
, a
->location
));
1923 * 'cups_crypt()' - Encrypt the password using the DES or MD5 algorithms,
1927 static char * /* O - Encrypted password */
1928 cups_crypt(const char *pw
, /* I - Password string */
1929 const char *salt
) /* I - Salt (key) string */
1931 if (!strncmp(salt
, "$1$", 3))
1934 * Use MD5 passwords without the benefit of PAM; this is for
1935 * Slackware Linux, and the algorithm was taken from the
1936 * old shadow-19990827/lib/md5crypt.c source code... :(
1939 int i
; /* Looping var */
1940 unsigned long n
; /* Output number */
1941 int pwlen
; /* Length of password string */
1942 const char *salt_end
; /* End of "salt" data for MD5 */
1943 char *ptr
; /* Pointer into result string */
1944 _cups_md5_state_t state
; /* Primary MD5 state info */
1945 _cups_md5_state_t state2
; /* Secondary MD5 state info */
1946 unsigned char digest
[16]; /* MD5 digest result */
1947 static char result
[120]; /* Final password string */
1951 * Get the salt data between dollar signs, e.g. $1$saltdata$md5.
1952 * Get a maximum of 8 characters of salt data after $1$...
1955 for (salt_end
= salt
+ 3; *salt_end
&& (salt_end
- salt
) < 11; salt_end
++)
1956 if (*salt_end
== '$')
1960 * Compute the MD5 sum we need...
1965 _cups_md5_init(&state
);
1966 _cups_md5_append(&state
, (unsigned char *)pw
, pwlen
);
1967 _cups_md5_append(&state
, (unsigned char *)salt
, salt_end
- salt
);
1969 _cups_md5_init(&state2
);
1970 _cups_md5_append(&state2
, (unsigned char *)pw
, pwlen
);
1971 _cups_md5_append(&state2
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
1972 _cups_md5_append(&state2
, (unsigned char *)pw
, pwlen
);
1973 _cups_md5_finish(&state2
, digest
);
1975 for (i
= pwlen
; i
> 0; i
-= 16)
1976 _cups_md5_append(&state
, digest
, i
> 16 ? 16 : i
);
1978 for (i
= pwlen
; i
> 0; i
>>= 1)
1979 _cups_md5_append(&state
, (unsigned char *)((i
& 1) ? "" : pw
), 1);
1981 _cups_md5_finish(&state
, digest
);
1983 for (i
= 0; i
< 1000; i
++)
1985 _cups_md5_init(&state
);
1988 _cups_md5_append(&state
, (unsigned char *)pw
, pwlen
);
1990 _cups_md5_append(&state
, digest
, 16);
1993 _cups_md5_append(&state
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
1996 _cups_md5_append(&state
, (unsigned char *)pw
, pwlen
);
1999 _cups_md5_append(&state
, digest
, 16);
2001 _cups_md5_append(&state
, (unsigned char *)pw
, pwlen
);
2003 _cups_md5_finish(&state
, digest
);
2007 * Copy the final sum to the result string and return...
2010 memcpy(result
, salt
, salt_end
- salt
);
2011 ptr
= result
+ (salt_end
- salt
);
2014 for (i
= 0; i
< 5; i
++, ptr
+= 4)
2016 n
= (((digest
[i
] << 8) | digest
[i
+ 6]) << 8);
2019 n
|= digest
[i
+ 12];
2026 to64(ptr
, digest
[11], 2);
2035 * Use the standard crypt() function...
2038 return (crypt(pw
, salt
));
2041 #endif /* !HAVE_LIBPAM */
2046 * 'pam_func()' - PAM conversation function.
2049 static int /* O - Success or failure */
2051 int num_msg
, /* I - Number of messages */
2052 const struct pam_message
**msg
, /* I - Messages */
2053 struct pam_response
**resp
, /* O - Responses */
2055 /* I - Pointer to connection */
2057 int i
; /* Looping var */
2058 struct pam_response
*replies
; /* Replies */
2059 cupsd_authdata_t
*data
; /* Pointer to auth data */
2063 * Allocate memory for the responses...
2066 if ((replies
= malloc(sizeof(struct pam_response
) * num_msg
)) == NULL
)
2067 return (PAM_CONV_ERR
);
2070 * Answer all of the messages...
2073 DEBUG_printf(("pam_func: appdata_ptr = %p\n", appdata_ptr
));
2077 * Apparently some versions of HP-UX 11 have a broken pam_unix security
2078 * module. This is a workaround...
2084 data
= (cupsd_authdata_t
*)appdata_ptr
;
2087 for (i
= 0; i
< num_msg
; i
++)
2089 DEBUG_printf(("pam_func: Message = \"%s\"\n", msg
[i
]->msg
));
2091 switch (msg
[i
]->msg_style
)
2093 case PAM_PROMPT_ECHO_ON
:
2094 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_ON, returning \"%s\"...\n",
2096 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2097 replies
[i
].resp
= strdup(data
->username
);
2100 case PAM_PROMPT_ECHO_OFF
:
2101 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_OFF, returning \"%s\"...\n",
2103 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2104 replies
[i
].resp
= strdup(data
->password
);
2108 DEBUG_puts("pam_func: PAM_TEXT_INFO...");
2109 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2110 replies
[i
].resp
= NULL
;
2114 DEBUG_puts("pam_func: PAM_ERROR_MSG...");
2115 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2116 replies
[i
].resp
= NULL
;
2120 DEBUG_printf(("pam_func: Unknown PAM message %d...\n",
2121 msg
[i
]->msg_style
));
2123 return (PAM_CONV_ERR
);
2128 * Return the responses back to PAM...
2133 return (PAM_SUCCESS
);
2139 * 'to64()' - Base64-encode an integer value...
2143 to64(char *s
, /* O - Output string */
2144 unsigned long v
, /* I - Value to encode */
2145 int n
) /* I - Number of digits */
2147 const char *itoa64
= "./0123456789"
2148 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2149 "abcdefghijklmnopqrstuvwxyz";
2152 for (; n
> 0; n
--, v
>>= 6)
2153 *s
++ = itoa64
[v
& 0x3f];
2155 #endif /* HAVE_LIBPAM */
2159 * End of "$Id: auth.c 5083 2006-02-06 02:57:43Z mike $".