]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/auth.c
2 * "$Id: auth.c 181 2006-06-22 20:01:18Z jlovell $"
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
31 * to access the location.
32 * cupsdAuthorize() - Validate any authorization credentials.
33 * cupsdCheckAuth() - Check authorization masks.
34 * cupsdCheckGroup() - Check for a user's group membership.
35 * cupsdCopyLocation() - Make a copy of a location...
36 * cupsdDeleteAllLocations() - Free all memory used for location
38 * cupsdDeleteLocation() - Free all memory used by a location.
39 * cupsdDenyHost() - Add a host name that is not allowed to access
41 * cupsdDenyIP() - Add an IP address or network that is not
42 * allowed to access the location.
43 * cupsdFindBest() - Find the location entry that best matches the
45 * cupsdFindLocation() - Find the named location.
46 * cupsdIsAuthorized() - Check to see if the user is authorized...
47 * add_allow() - Add an allow mask to the location.
48 * add_deny() - Add a deny mask to the location.
49 * compare_locations() - Compare two locations.
50 * cups_crypt() - Encrypt the password using the DES or MD5
51 * algorithms, as needed.
52 * get_md5_password() - Get an MD5 password.
53 * pam_func() - PAM conversation function.
54 * to64() - Base64-encode an integer value...
58 * Include necessary headers...
65 #endif /* HAVE_SHADOW_H */
68 #endif /* HAVE_CRYPT_H */
70 # ifdef HAVE_PAM_PAM_APPL_H
71 # include <pam/pam_appl.h>
73 # include <security/pam_appl.h>
74 # endif /* HAVE_PAM_PAM_APPL_H */
75 #endif /* HAVE_LIBPAM */
78 #endif /* HAVE_USERSEC_H */
79 #ifdef HAVE_MEMBERSHIP_H
80 # include <membership.h>
81 #endif /* HAVE_MEMBERSHIP_H */
88 static cupsd_authmask_t
*add_allow(cupsd_location_t
*loc
);
89 static cupsd_authmask_t
*add_deny(cupsd_location_t
*loc
);
90 static int compare_locations(cupsd_location_t
*a
,
93 static char *cups_crypt(const char *pw
, const char *salt
);
94 #endif /* !HAVE_LIBPAM */
95 static char *get_md5_password(const char *username
,
96 const char *group
, char passwd
[33]);
98 static int pam_func(int, const struct pam_message
**,
99 struct pam_response
**, void *);
101 static void to64(char *s
, unsigned long v
, int n
);
102 #endif /* HAVE_LIBPAM */
106 * Local structures...
110 typedef struct cupsd_authdata_s
/**** Authentication data ****/
112 char username
[33], /* Username string */
113 password
[33]; /* Password string */
115 #endif /* HAVE_LIBPAM */
122 #if defined(__hpux) && HAVE_LIBPAM
123 static cupsd_authdata_t
*auth_data
; /* Current client being authenticated */
124 #endif /* __hpux && HAVE_LIBPAM */
128 * 'cupsdAddLocation()' - Add a location for authorization.
131 cupsd_location_t
* /* O - Pointer to new location record */
132 cupsdAddLocation(const char *location
) /* I - Location path */
134 cupsd_location_t
*temp
; /* New location */
138 * Make sure the locations array is created...
142 Locations
= cupsArrayNew((cups_array_func_t
)compare_locations
, NULL
);
148 * Try to allocate memory for the new location.
151 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
155 * Initialize the record and copy the name over...
158 temp
->location
= strdup(location
);
159 temp
->length
= strlen(temp
->location
);
161 cupsArrayAdd(Locations
, temp
);
163 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddLocation: added location \'%s\'",
167 * Return the new record...
175 * 'cupsdAddName()' - Add a name to a location...
179 cupsdAddName(cupsd_location_t
*loc
, /* I - Location to add to */
180 char *name
) /* I - Name to add */
182 char **temp
; /* Pointer to names array */
185 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddName(loc=%p, name=\"%s\")",
188 if (loc
->num_names
== 0)
189 temp
= malloc(sizeof(char *));
191 temp
= realloc(loc
->names
, (loc
->num_names
+ 1) * sizeof(char *));
195 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to add name to location %s: %s",
196 loc
->location
? loc
->location
: "nil", strerror(errno
));
202 if ((temp
[loc
->num_names
] = strdup(name
)) == NULL
)
204 cupsdLogMessage(CUPSD_LOG_ERROR
,
205 "Unable to duplicate name for location %s: %s",
206 loc
->location
? loc
->location
: "nil", strerror(errno
));
215 * 'cupsdAllowHost()' - Add a host name that is allowed to access the location.
219 cupsdAllowHost(cupsd_location_t
*loc
, /* I - Location to add to */
220 char *name
) /* I - Name of host or domain to add */
222 cupsd_authmask_t
*temp
; /* New host/domain mask */
223 char ifname
[32], /* Interface name */
224 *ifptr
; /* Pointer to end of name */
227 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAllowHost(loc=%p(%s), name=\"%s\")",
228 loc
, loc
->location
? loc
->location
: "nil", name
);
230 if ((temp
= add_allow(loc
)) == NULL
)
233 if (!strcasecmp(name
, "@LOCAL"))
236 * Allow *interface*...
239 temp
->type
= AUTH_INTERFACE
;
240 temp
->mask
.name
.name
= strdup("*");
241 temp
->mask
.name
.length
= 1;
243 else if (!strncasecmp(name
, "@IF(", 4))
246 * Allow *interface*...
249 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
251 ifptr
= ifname
+ strlen(ifname
);
253 if (ifptr
[-1] == ')')
259 temp
->type
= AUTH_INTERFACE
;
260 temp
->mask
.name
.name
= strdup(ifname
);
261 temp
->mask
.name
.length
= ifptr
- ifname
;
269 temp
->type
= AUTH_NAME
;
270 temp
->mask
.name
.name
= strdup(name
);
271 temp
->mask
.name
.length
= strlen(name
);
277 * 'cupsdAllowIP()' - Add an IP address or network that is allowed to access
282 cupsdAllowIP(cupsd_location_t
*loc
, /* I - Location to add to */
283 unsigned address
[4], /* I - IP address to add */
284 unsigned netmask
[4]) /* I - Netmask of address */
286 cupsd_authmask_t
*temp
; /* New host/domain mask */
289 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
290 "cupsdAllowIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
291 loc
, loc
->location
? loc
->location
: "nil",
292 address
[0], address
[1], address
[2], address
[3],
293 netmask
[0], netmask
[1], netmask
[2], netmask
[3]);
295 if ((temp
= add_allow(loc
)) == NULL
)
298 temp
->type
= AUTH_IP
;
299 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
300 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
305 * 'cupsdAuthorize()' - Validate any authorization credentials.
309 cupsdAuthorize(cupsd_client_t
*con
) /* I - Client connection */
311 int type
; /* Authentication type */
312 char *authorization
, /* Pointer into Authorization string */
313 *ptr
, /* Pointer into string */
314 username
[65], /* Username string */
315 password
[33]; /* Password string */
316 const char *localuser
; /* Certificate username */
317 char nonce
[HTTP_MAX_VALUE
], /* Nonce value from client */
318 md5
[33], /* MD5 password */
319 basicmd5
[33]; /* MD5 of Basic password */
320 static const char * const states
[] = /* HTTP client states... */
340 * Locate the best matching location so we know what kind of
341 * authentication to expect...
344 con
->best
= cupsdFindBest(con
->uri
, con
->http
.state
);
346 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
347 "cupsdAuthorize: con->uri=\"%s\", con->best=%p(%s)",
348 con
->uri
, con
->best
, con
->best
? con
->best
->location
: "");
350 if (con
->best
&& con
->best
->type
!= AUTH_NONE
)
351 type
= con
->best
->type
;
353 type
= DefaultAuthType
;
356 * Decode the Authorization string...
359 authorization
= con
->http
.fields
[HTTP_FIELD_AUTHORIZATION
];
361 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAuthorize: Authorization=\"%s\"",
367 if (type
== AUTH_NONE
)
370 * No authorization required, return early...
373 cupsdLogMessage(CUPSD_LOG_DEBUG
,
374 "cupsdAuthorize: No authentication required.");
377 else if (!*authorization
)
380 * No authorization data provided, return early...
383 cupsdLogMessage(CUPSD_LOG_DEBUG
,
384 "cupsdAuthorize: No authentication data provided.");
387 else if (!strncmp(authorization
, "Local", 5) &&
388 !strcasecmp(con
->http
.hostname
, "localhost"))
391 * Get Local certificate authentication data...
395 while (isspace(*authorization
& 255))
398 if ((localuser
= cupsdFindCert(authorization
)) != NULL
)
399 strlcpy(username
, localuser
, sizeof(username
));
402 cupsdLogMessage(CUPSD_LOG_ERROR
,
403 "cupsdAuthorize: Local authentication certificate not "
408 else if (!strncmp(authorization
, "Basic", 5) &&
409 (type
== AUTH_BASIC
|| type
== AUTH_BASICDIGEST
))
412 * Get the Basic authentication data...
415 int userlen
; /* Username:password length */
419 while (isspace(*authorization
& 255))
422 userlen
= sizeof(username
);
423 httpDecode64_2(username
, &userlen
, authorization
);
426 * Pull the username and password out...
429 if ((ptr
= strchr(username
, ':')) == NULL
)
431 cupsdLogMessage(CUPSD_LOG_ERROR
,
432 "cupsdAuthorize: Missing Basic password!");
441 * Username must not be empty...
444 cupsdLogMessage(CUPSD_LOG_ERROR
,
445 "cupsdAuthorize: Empty Basic username!");
452 * Password must not be empty...
455 cupsdLogMessage(CUPSD_LOG_ERROR
,
456 "cupsdAuthorize: Empty Basic password!");
460 strlcpy(password
, ptr
, sizeof(password
));
463 * Validate the username and password...
472 * Only use PAM to do authentication. This supports MD5
473 * passwords, among other things...
476 pam_handle_t
*pamh
; /* PAM authentication handle */
477 int pamerr
; /* PAM error code */
478 struct pam_conv pamdata
;/* PAM conversation data */
479 cupsd_authdata_t data
; /* Authentication data */
482 strlcpy(data
.username
, username
, sizeof(data
.username
));
483 strlcpy(data
.password
, password
, sizeof(data
.password
));
486 pamdata
.conv
= (int (*)(int, struct pam_message
**,
487 struct pam_response
**,
490 pamdata
.conv
= pam_func
;
492 pamdata
.appdata_ptr
= &data
;
496 * Workaround for HP-UX bug in pam_unix; see pam_func() below for
503 pamerr
= pam_start("cups", username
, &pamdata
, &pamh
);
504 if (pamerr
!= PAM_SUCCESS
)
506 cupsdLogMessage(CUPSD_LOG_ERROR
,
507 "cupsdAuthorize: pam_start() returned %d (%s)!\n",
508 pamerr
, pam_strerror(pamh
, pamerr
));
513 pamerr
= pam_authenticate(pamh
, PAM_SILENT
);
514 if (pamerr
!= PAM_SUCCESS
)
516 cupsdLogMessage(CUPSD_LOG_ERROR
,
517 "cupsdAuthorize: pam_authenticate() returned %d "
519 pamerr
, pam_strerror(pamh
, pamerr
));
524 pamerr
= pam_acct_mgmt(pamh
, PAM_SILENT
);
525 if (pamerr
!= PAM_SUCCESS
)
527 cupsdLogMessage(CUPSD_LOG_ERROR
,
528 "cupsdAuthorize: pam_acct_mgmt() returned %d "
530 pamerr
, pam_strerror(pamh
, pamerr
));
535 pam_end(pamh
, PAM_SUCCESS
);
537 #elif defined(HAVE_USERSEC_H)
539 * Use AIX authentication interface...
542 char *authmsg
; /* Authentication message */
543 char *loginmsg
; /* Login message */
544 int reenter
; /* ??? */
547 cupsdLogMessage(CUPSD_LOG_DEBUG
,
548 "cupsdAuthorize: AIX authenticate of username \"%s\"",
552 if (authenticate(username
, password
, &reenter
, &authmsg
) != 0)
554 cupsdLogMessage(CUPSD_LOG_DEBUG
,
555 "cupsdAuthorize: Unable to authenticate username "
557 username
, strerror(errno
));
563 * Use normal UNIX password file-based authentication...
566 char *pass
; /* Encrypted password */
567 struct passwd
*pw
; /* User password data */
568 # ifdef HAVE_SHADOW_H
569 struct spwd
*spw
; /* Shadow password data */
570 # endif /* HAVE_SHADOW_H */
573 pw
= getpwnam(username
); /* Get the current password */
574 endpwent(); /* Close the password file */
582 cupsdLogMessage(CUPSD_LOG_ERROR
,
583 "cupsdAuthorize: Unknown username \"%s\"!",
588 # ifdef HAVE_SHADOW_H
589 spw
= getspnam(username
);
592 if (!spw
&& !strcmp(pw
->pw_passwd
, "x"))
595 * Don't allow blank passwords!
598 cupsdLogMessage(CUPSD_LOG_ERROR
,
599 "cupsdAuthorize: Username \"%s\" has no shadow "
600 "password!", username
);
604 if (spw
&& !spw
->sp_pwdp
[0] && !pw
->pw_passwd
[0])
606 if (!pw
->pw_passwd
[0])
607 # endif /* HAVE_SHADOW_H */
610 * Don't allow blank passwords!
613 cupsdLogMessage(CUPSD_LOG_ERROR
,
614 "cupsdAuthorize: Username \"%s\" has no password!",
620 * OK, the password isn't blank, so compare with what came from the
624 pass
= cups_crypt(password
, pw
->pw_passwd
);
626 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
627 "cupsdAuthorize: pw_passwd=\"%s\", crypt=\"%s\"",
628 pw
->pw_passwd
, pass
);
630 if (!pass
|| strcmp(pw
->pw_passwd
, pass
))
632 # ifdef HAVE_SHADOW_H
635 pass
= cups_crypt(password
, spw
->sp_pwdp
);
637 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
638 "cupsdAuthorize: sp_pwdp=\"%s\", crypt=\"%s\"",
641 if (pass
== NULL
|| strcmp(spw
->sp_pwdp
, pass
))
643 cupsdLogMessage(CUPSD_LOG_ERROR
,
644 "cupsdAuthorize: Authentication failed for "
651 # endif /* HAVE_SHADOW_H */
653 cupsdLogMessage(CUPSD_LOG_ERROR
,
654 "cupsdAuthorize: Authentication failed for "
660 #endif /* HAVE_LIBPAM */
664 case AUTH_BASICDIGEST
:
666 * Do Basic authentication with the Digest password file...
669 if (!get_md5_password(username
, NULL
, md5
))
671 cupsdLogMessage(CUPSD_LOG_ERROR
,
672 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
677 httpMD5(username
, "CUPS", password
, basicmd5
);
679 if (strcmp(md5
, basicmd5
))
681 cupsdLogMessage(CUPSD_LOG_ERROR
,
682 "cupsdAuthorize: Authentication failed for \"%s\"!",
689 else if (!strncmp(authorization
, "Digest", 6) && type
== AUTH_DIGEST
)
692 * Get the username, password, and nonce from the Digest attributes...
695 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "username",
696 username
, sizeof(username
)) || !username
[0])
699 * Username must not be empty...
702 cupsdLogMessage(CUPSD_LOG_ERROR
,
703 "cupsdAuthorize: Empty or missing Digest username!");
707 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "response",
708 password
, sizeof(password
)) || !password
[0])
711 * Password must not be empty...
714 cupsdLogMessage(CUPSD_LOG_ERROR
,
715 "cupsdAuthorize: Empty or missing Digest password!");
719 if (!httpGetSubField(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "nonce",
722 cupsdLogMessage(CUPSD_LOG_ERROR
,
723 "cupsdAuthorize: No nonce value for Digest "
728 if (strcmp(con
->http
.hostname
, nonce
))
730 cupsdLogMessage(CUPSD_LOG_ERROR
,
731 "cupsdAuthorize: Bad nonce value, expected \"%s\", "
732 "got \"%s\"!", con
->http
.hostname
, nonce
);
737 * Validate the username and password...
740 if (!get_md5_password(username
, NULL
, md5
))
742 cupsdLogMessage(CUPSD_LOG_ERROR
,
743 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
748 httpMD5Final(nonce
, states
[con
->http
.state
], con
->uri
, md5
);
750 if (strcmp(md5
, password
))
752 cupsdLogMessage(CUPSD_LOG_ERROR
,
753 "cupsdAuthorize: Authentication failed for \"%s\"!",
760 cupsdLogMessage(CUPSD_LOG_DEBUG
,
761 "cupsdAuthorize: Bad authentication data.");
766 * If we get here, then we were able to validate the username and
767 * password - copy the validated username and password to the client
771 strlcpy(con
->username
, username
, sizeof(con
->username
));
772 strlcpy(con
->password
, password
, sizeof(con
->password
));
774 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: username=\"%s\"",
780 * 'cupsdCheckAuth()' - Check authorization masks.
783 int /* O - 1 if mask matches, 0 otherwise */
785 unsigned ip
[4], /* I - Client address */
786 char *name
, /* I - Client hostname */
787 int name_len
, /* I - Length of hostname */
788 int num_masks
, /* I - Number of masks */
789 cupsd_authmask_t
*masks
) /* I - Masks */
791 int i
; /* Looping var */
792 cupsd_netif_t
*iface
; /* Network interface */
793 unsigned netip4
; /* IPv4 network address */
795 unsigned netip6
[4]; /* IPv6 network address */
796 #endif /* AF_INET6 */
798 while (num_masks
> 0)
802 case AUTH_INTERFACE
:
804 * Check for a match with a network interface...
807 netip4
= htonl(ip
[3]);
810 netip6
[0] = htonl(ip
[0]);
811 netip6
[1] = htonl(ip
[1]);
812 netip6
[2] = htonl(ip
[2]);
813 netip6
[3] = htonl(ip
[3]);
814 #endif /* AF_INET6 */
816 if (!strcmp(masks
->mask
.name
.name
, "*"))
819 * Check against all local interfaces...
824 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
826 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
829 * Only check local interfaces...
832 if (!iface
->is_local
)
835 if (iface
->address
.addr
.sa_family
== AF_INET
)
838 * Check IPv4 address...
841 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
842 (iface
->address
.ipv4
.sin_addr
.s_addr
&
843 iface
->mask
.ipv4
.sin_addr
.s_addr
))
850 * Check IPv6 address...
853 for (i
= 0; i
< 4; i
++)
854 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
855 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
856 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
862 #endif /* AF_INET6 */
868 * Check the named interface...
871 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
873 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
875 if (strcmp(masks
->mask
.name
.name
, iface
->name
))
878 if (iface
->address
.addr
.sa_family
== AF_INET
)
881 * Check IPv4 address...
884 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
885 (iface
->address
.ipv4
.sin_addr
.s_addr
&
886 iface
->mask
.ipv4
.sin_addr
.s_addr
))
893 * Check IPv6 address...
896 for (i
= 0; i
< 4; i
++)
897 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
898 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
899 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
905 #endif /* AF_INET6 */
912 * Check for exact name match...
915 if (!strcasecmp(name
, masks
->mask
.name
.name
))
919 * Check for domain match...
922 if (name_len
>= masks
->mask
.name
.length
&&
923 masks
->mask
.name
.name
[0] == '.' &&
924 !strcasecmp(name
+ name_len
- masks
->mask
.name
.length
,
925 masks
->mask
.name
.name
))
931 * Check for IP/network address match...
934 for (i
= 0; i
< 4; i
++)
935 if ((ip
[i
] & masks
->mask
.ip
.netmask
[i
]) !=
936 masks
->mask
.ip
.address
[i
])
953 * 'cupsdCheckGroup()' - Check for a user's group membership.
956 int /* O - 1 if user is a member, 0 otherwise */
958 const char *username
, /* I - User name */
959 struct passwd
*user
, /* I - System user info */
960 const char *groupname
) /* I - Group name */
962 int i
; /* Looping var */
963 struct group
*group
; /* System group info */
964 char junk
[33]; /* MD5 password (not used) */
965 #ifdef HAVE_MBR_UID_TO_UUID
966 uuid_t useruuid
, /* UUID for username */
967 groupuuid
; /* UUID for groupname */
968 int is_member
; /* True if user is a member of group */
969 #endif /* HAVE_MBR_UID_TO_UUID */
972 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
973 "cupsdCheckGroup(username=\"%s\", user=%p, groupname=\"%s\")",
974 username
, user
, groupname
);
980 if (!username
|| !groupname
)
984 * Check to see if the user is a member of the named group...
987 group
= getgrnam(groupname
);
993 * Group exists, check it...
996 for (i
= 0; group
->gr_mem
[i
]; i
++)
997 if (!strcasecmp(username
, group
->gr_mem
[i
]))
1002 * Group doesn't exist or user not in group list, check the group ID
1003 * against the user's group ID...
1006 if (user
&& group
&& group
->gr_gid
== user
->pw_gid
)
1009 #ifdef HAVE_MBR_UID_TO_UUID
1011 * Check group membership through MacOS X membership API...
1015 if (!mbr_uid_to_uuid(user
->pw_uid
, useruuid
))
1016 if (!mbr_gid_to_uuid(group
->gr_gid
, groupuuid
))
1017 if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1020 #endif /* HAVE_MBR_UID_TO_UUID */
1023 * Username not found, group not found, or user is not part of the
1024 * system group... Check for a user and group in the MD5 password
1028 if (get_md5_password(username
, groupname
, junk
) != NULL
)
1032 * If we get this far, then the user isn't part of the named group...
1040 * 'cupsdCopyLocation()' - Make a copy of a location...
1043 cupsd_location_t
* /* O - New location */
1045 cupsd_location_t
**loc
) /* IO - Original location */
1047 int i
; /* Looping var */
1048 cupsd_location_t
*temp
; /* New location */
1049 char location
[HTTP_MAX_URI
];
1050 /* Location of resource */
1054 * Use a local copy of location because cupsdAddLocation may cause
1055 * this memory to be moved...
1058 strlcpy(location
, (*loc
)->location
, sizeof(location
));
1060 if ((temp
= cupsdAddLocation(location
)) == NULL
)
1064 * Copy the information from the original location to the new one.
1067 temp
->limit
= (*loc
)->limit
;
1068 temp
->order_type
= (*loc
)->order_type
;
1069 temp
->type
= (*loc
)->type
;
1070 temp
->level
= (*loc
)->level
;
1071 temp
->satisfy
= (*loc
)->satisfy
;
1072 temp
->encryption
= (*loc
)->encryption
;
1074 if ((temp
->num_names
= (*loc
)->num_names
) > 0)
1077 * Copy the names array...
1080 if ((temp
->names
= calloc(temp
->num_names
, sizeof(char *))) == NULL
)
1082 cupsdLogMessage(CUPSD_LOG_ERROR
,
1083 "cupsdCopyLocation: Unable to allocate memory for %d names: %s",
1084 temp
->num_names
, strerror(errno
));
1086 cupsdDeleteLocation(temp
);
1090 for (i
= 0; i
< temp
->num_names
; i
++)
1091 if ((temp
->names
[i
] = strdup((*loc
)->names
[i
])) == NULL
)
1093 cupsdLogMessage(CUPSD_LOG_ERROR
,
1094 "cupsdCopyLocation: Unable to copy name \"%s\": %s",
1095 (*loc
)->names
[i
], strerror(errno
));
1097 cupsdDeleteLocation(temp
);
1102 if ((temp
->num_allow
= (*loc
)->num_allow
) > 0)
1105 * Copy allow rules...
1108 if ((temp
->allow
= calloc(temp
->num_allow
, sizeof(cupsd_authmask_t
))) == NULL
)
1110 cupsdLogMessage(CUPSD_LOG_ERROR
,
1111 "cupsdCopyLocation: Unable to allocate memory for %d allow rules: %s",
1112 temp
->num_allow
, strerror(errno
));
1113 cupsdDeleteLocation(temp
);
1117 for (i
= 0; i
< temp
->num_allow
; i
++)
1118 switch (temp
->allow
[i
].type
= (*loc
)->allow
[i
].type
)
1121 temp
->allow
[i
].mask
.name
.length
= (*loc
)->allow
[i
].mask
.name
.length
;
1122 temp
->allow
[i
].mask
.name
.name
= strdup((*loc
)->allow
[i
].mask
.name
.name
);
1124 if (temp
->allow
[i
].mask
.name
.name
== NULL
)
1126 cupsdLogMessage(CUPSD_LOG_ERROR
,
1127 "cupsdCopyLocation: Unable to copy allow name \"%s\": %s",
1128 (*loc
)->allow
[i
].mask
.name
.name
, strerror(errno
));
1129 cupsdDeleteLocation(temp
);
1134 memcpy(&(temp
->allow
[i
].mask
.ip
), &((*loc
)->allow
[i
].mask
.ip
),
1135 sizeof(cupsd_ipmask_t
));
1140 if ((temp
->num_deny
= (*loc
)->num_deny
) > 0)
1143 * Copy deny rules...
1146 if ((temp
->deny
= calloc(temp
->num_deny
, sizeof(cupsd_authmask_t
))) == NULL
)
1148 cupsdLogMessage(CUPSD_LOG_ERROR
,
1149 "cupsdCopyLocation: Unable to allocate memory for %d deny rules: %s",
1150 temp
->num_deny
, strerror(errno
));
1151 cupsdDeleteLocation(temp
);
1155 for (i
= 0; i
< temp
->num_deny
; i
++)
1156 switch (temp
->deny
[i
].type
= (*loc
)->deny
[i
].type
)
1159 temp
->deny
[i
].mask
.name
.length
= (*loc
)->deny
[i
].mask
.name
.length
;
1160 temp
->deny
[i
].mask
.name
.name
= strdup((*loc
)->deny
[i
].mask
.name
.name
);
1162 if (temp
->deny
[i
].mask
.name
.name
== NULL
)
1164 cupsdLogMessage(CUPSD_LOG_ERROR
,
1165 "cupsdCopyLocation: Unable to copy deny name \"%s\": %s",
1166 (*loc
)->deny
[i
].mask
.name
.name
, strerror(errno
));
1167 cupsdDeleteLocation(temp
);
1172 memcpy(&(temp
->deny
[i
].mask
.ip
), &((*loc
)->deny
[i
].mask
.ip
),
1173 sizeof(cupsd_ipmask_t
));
1183 * 'cupsdDeleteAllLocations()' - Free all memory used for location authorization.
1187 cupsdDeleteAllLocations(void)
1189 cupsd_location_t
*loc
; /* Current location */
1193 * Free all of the allow/deny records first...
1196 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1198 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1199 cupsdDeleteLocation(loc
);
1202 * Then free the location array...
1205 cupsArrayDelete(Locations
);
1211 * 'cupsdDeleteLocation()' - Free all memory used by a location.
1215 cupsdDeleteLocation(
1216 cupsd_location_t
*loc
) /* I - Location to delete */
1218 int i
; /* Looping var */
1219 cupsd_authmask_t
*mask
; /* Current mask */
1222 cupsArrayRemove(Locations
, loc
);
1224 for (i
= loc
->num_names
- 1; i
>= 0; i
--)
1225 free(loc
->names
[i
]);
1227 if (loc
->num_names
> 0)
1230 for (i
= loc
->num_allow
, mask
= loc
->allow
; i
> 0; i
--, mask
++)
1231 if (mask
->type
== AUTH_NAME
|| mask
->type
== AUTH_INTERFACE
)
1232 free(mask
->mask
.name
.name
);
1234 if (loc
->num_allow
> 0)
1237 for (i
= loc
->num_deny
, mask
= loc
->deny
; i
> 0; i
--, mask
++)
1238 if (mask
->type
== AUTH_NAME
|| mask
->type
== AUTH_INTERFACE
)
1239 free(mask
->mask
.name
.name
);
1241 if (loc
->num_deny
> 0)
1244 free(loc
->location
);
1250 * 'cupsdDenyHost()' - Add a host name that is not allowed to access the
1255 cupsdDenyHost(cupsd_location_t
*loc
, /* I - Location to add to */
1256 char *name
) /* I - Name of host or domain to add */
1258 cupsd_authmask_t
*temp
; /* New host/domain mask */
1259 char ifname
[32], /* Interface name */
1260 *ifptr
; /* Pointer to end of name */
1263 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDenyHost(loc=%p(%s), name=\"%s\")",
1264 loc
, loc
->location
? loc
->location
: "nil", name
);
1266 if ((temp
= add_deny(loc
)) == NULL
)
1269 if (!strcasecmp(name
, "@LOCAL"))
1272 * Deny *interface*...
1275 temp
->type
= AUTH_INTERFACE
;
1276 temp
->mask
.name
.name
= strdup("*");
1277 temp
->mask
.name
.length
= 1;
1279 else if (!strncasecmp(name
, "@IF(", 4))
1282 * Deny *interface*...
1285 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
1287 ifptr
= ifname
+ strlen(ifname
);
1289 if (ifptr
[-1] == ')')
1295 temp
->type
= AUTH_INTERFACE
;
1296 temp
->mask
.name
.name
= strdup(ifname
);
1297 temp
->mask
.name
.length
= ifptr
- ifname
;
1305 temp
->type
= AUTH_NAME
;
1306 temp
->mask
.name
.name
= strdup(name
);
1307 temp
->mask
.name
.length
= strlen(name
);
1313 * 'cupsdDenyIP()' - Add an IP address or network that is not allowed to
1314 * access the location.
1318 cupsdDenyIP(cupsd_location_t
*loc
, /* I - Location to add to */
1319 unsigned address
[4],/* I - IP address to add */
1320 unsigned netmask
[4])/* I - Netmask of address */
1322 cupsd_authmask_t
*temp
; /* New host/domain mask */
1325 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1326 "cupsdDenyIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
1327 loc
, loc
->location
? loc
->location
: "nil",
1328 address
[0], address
[1], address
[2], address
[3],
1329 netmask
[0], netmask
[1], netmask
[2], netmask
[3]);
1331 if ((temp
= add_deny(loc
)) == NULL
)
1334 temp
->type
= AUTH_IP
;
1335 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
1336 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
1341 * 'cupsdFindBest()' - Find the location entry that best matches the resource.
1344 cupsd_location_t
* /* O - Location that matches */
1345 cupsdFindBest(const char *path
, /* I - Resource path */
1346 http_state_t state
) /* I - HTTP state/request */
1348 char uri
[HTTP_MAX_URI
],
1349 /* URI in request... */
1350 *uriptr
; /* Pointer into URI */
1351 cupsd_location_t
*loc
, /* Current location */
1352 *best
; /* Best match for location so far */
1353 int bestlen
; /* Length of best match */
1354 int limit
; /* Limit field */
1355 static const int limits
[] = /* Map http_status_t to AUTH_LIMIT_xyz */
1375 * First copy the connection URI to a local string so we have drop
1376 * any .ppd extension from the pathname in /printers or /classes
1380 strlcpy(uri
, path
, sizeof(uri
));
1382 if (!strncmp(uri
, "/printers/", 10) ||
1383 !strncmp(uri
, "/classes/", 9))
1386 * Check if the URI has .ppd on the end...
1389 uriptr
= uri
+ strlen(uri
) - 4; /* len > 4 if we get here... */
1391 if (!strcmp(uriptr
, ".ppd"))
1395 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: uri = \"%s\"...", uri
);
1398 * Loop through the list of locations to find a match...
1401 limit
= limits
[state
];
1405 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1407 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1409 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: Location %s Limit %x",
1410 loc
->location
? loc
->location
: "nil", loc
->limit
);
1412 if (!strncmp(uri
, "/printers/", 10) || !strncmp(uri
, "/classes/", 9))
1415 * Use case-insensitive comparison for queue names...
1418 if (loc
->length
> bestlen
&& loc
->location
&&
1419 !strncasecmp(uri
, loc
->location
, loc
->length
) &&
1420 loc
->location
[0] == '/' &&
1421 (limit
& loc
->limit
) != 0)
1424 bestlen
= loc
->length
;
1430 * Use case-sensitive comparison for other URIs...
1433 if (loc
->length
> bestlen
&& loc
->location
&&
1434 !strncmp(uri
, loc
->location
, loc
->length
) &&
1435 loc
->location
[0] == '/' &&
1436 (limit
& loc
->limit
) != 0)
1439 bestlen
= loc
->length
;
1445 * Return the match, if any...
1448 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: best = %s",
1449 best
? best
->location
: "NONE");
1456 * 'cupsdFindLocation()' - Find the named location.
1459 cupsd_location_t
* /* O - Location that matches */
1460 cupsdFindLocation(const char *location
) /* I - Connection */
1462 cupsd_location_t key
; /* Search key */
1465 key
.location
= (char *)location
;
1467 return ((cupsd_location_t
*)cupsArrayFind(Locations
, &key
));
1472 * 'cupsdIsAuthorized()' - Check to see if the user is authorized...
1475 http_status_t
/* O - HTTP_OK if authorized or error code */
1476 cupsdIsAuthorized(cupsd_client_t
*con
, /* I - Connection */
1477 const char *owner
)/* I - Owner of object */
1479 int i
, j
, /* Looping vars */
1480 auth
; /* Authorization status */
1481 unsigned address
[4]; /* Authorization address */
1482 cupsd_location_t
*best
; /* Best match for location so far */
1483 int hostlen
; /* Length of hostname */
1484 const char *username
; /* Username to authorize */
1485 struct passwd
*pw
; /* User password data */
1486 static const char * const levels
[] = /* Auth levels */
1492 static const char * const types
[] = /* Auth types */
1501 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1502 "cupsdIsAuthorized: con->uri=\"%s\", con->best=%p(%s)",
1503 con
->uri
, con
->best
, con
->best
? con
->best
->location
?
1504 con
->best
->location
: "(null)" : "");
1506 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1507 "cupsdIsAuthorized: owner=\"%s\"", owner
);
1510 * If there is no "best" authentication rule for this request, then
1511 * access is allowed from the local system and denied from other
1517 if (!strcmp(con
->http
.hostname
, "localhost") ||
1518 !strcmp(con
->http
.hostname
, ServerName
))
1521 return (HTTP_FORBIDDEN
);
1526 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1527 "cupsdIsAuthorized: level=AUTH_%s, type=AUTH_%s, "
1528 "satisfy=AUTH_SATISFY_%s, num_names=%d",
1529 levels
[best
->level
], types
[best
->type
],
1530 best
->satisfy
? "ANY" : "ALL", best
->num_names
);
1532 if (best
->limit
== AUTH_LIMIT_IPP
)
1533 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: op=%x(%s)",
1534 best
->op
, ippOpString(best
->op
));
1537 * Check host/ip-based accesses...
1541 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
1544 * Copy IPv6 address...
1547 address
[0] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0]);
1548 address
[1] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1]);
1549 address
[2] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]);
1550 address
[3] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[3]);
1553 #endif /* AF_INET6 */
1554 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET
)
1557 * Copy IPv4 address...
1563 address
[3] = ntohl(con
->http
.hostaddr
->ipv4
.sin_addr
.s_addr
);
1566 memset(address
, 0, sizeof(address
));
1568 hostlen
= strlen(con
->http
.hostname
);
1570 if (!strcasecmp(con
->http
.hostname
, "localhost"))
1573 * Access from localhost (127.0.0.1 or ::1) is always allowed...
1581 * Do authorization checks on the domain/address...
1584 switch (best
->order_type
)
1587 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1590 case AUTH_ALLOW
: /* Order Deny,Allow */
1593 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1594 best
->num_deny
, best
->deny
))
1597 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1598 best
->num_allow
, best
->allow
))
1602 case AUTH_DENY
: /* Order Allow,Deny */
1605 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1606 best
->num_allow
, best
->allow
))
1609 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1610 best
->num_deny
, best
->deny
))
1616 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: auth=AUTH_%s...",
1617 auth
? "DENY" : "ALLOW");
1619 if (auth
== AUTH_DENY
&& best
->satisfy
== AUTH_SATISFY_ALL
)
1620 return (HTTP_FORBIDDEN
);
1624 * See if encryption is required...
1627 if (best
->encryption
>= HTTP_ENCRYPT_REQUIRED
&& !con
->http
.tls
&&
1628 strcasecmp(con
->http
.hostname
, "localhost") &&
1629 best
->satisfy
== AUTH_SATISFY_ALL
)
1631 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1632 "cupsdIsAuthorized: Need upgrade to TLS...");
1633 return (HTTP_UPGRADE_REQUIRED
);
1635 #endif /* HAVE_SSL */
1638 * Now see what access level is required...
1641 if (best
->level
== AUTH_ANON
|| /* Anonymous access - allow it */
1642 (best
->type
== AUTH_NONE
&& best
->num_names
== 0))
1645 if (!con
->username
[0] && best
->type
== AUTH_NONE
&&
1646 best
->limit
== AUTH_LIMIT_IPP
)
1649 * Check for unauthenticated username...
1652 ipp_attribute_t
*attr
; /* requesting-user-name attribute */
1655 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
1658 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1659 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
1660 attr
->values
[0].string
.text
);
1661 username
= attr
->values
[0].string
.text
;
1663 else if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
1664 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1666 return (HTTP_OK
); /* unless overridden with Satisfy */
1670 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: username=\"%s\"",
1673 if (!con
->username
[0])
1675 if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
1676 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1678 return (HTTP_OK
); /* unless overridden with Satisfy */
1681 username
= con
->username
;
1685 * OK, got a username. See if we need normal user access, or group
1686 * access... (root always matches)
1689 if (!strcmp(username
, "root"))
1693 * Get the user info...
1696 pw
= getpwnam(username
);
1699 if (best
->level
== AUTH_USER
)
1702 * If there are no names associated with this location, then
1703 * any valid user is OK...
1706 if (best
->num_names
== 0)
1710 * Otherwise check the user list and return OK if this user is
1714 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1715 "cupsdIsAuthorized: Checking user membership...");
1717 for (i
= 0; i
< best
->num_names
; i
++)
1719 if (!strcasecmp(best
->names
[i
], "@OWNER") && owner
&&
1720 !strcasecmp(username
, owner
))
1722 else if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
1724 for (j
= 0; j
< NumSystemGroups
; j
++)
1725 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
1728 else if (best
->names
[i
][0] == '@')
1730 if (cupsdCheckGroup(username
, pw
, best
->names
[i
] + 1))
1733 else if (!strcasecmp(username
, best
->names
[i
]))
1737 return (HTTP_UNAUTHORIZED
);
1741 * Check to see if this user is in any of the named groups...
1744 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1745 "cupsdIsAuthorized: Checking group membership...");
1748 * Check to see if this user is in any of the named groups...
1751 for (i
= 0; i
< best
->num_names
; i
++)
1753 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1754 "cupsdIsAuthorized: Checking group \"%s\" membership...",
1757 if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
1759 for (j
= 0; j
< NumSystemGroups
; j
++)
1760 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
1763 else if (cupsdCheckGroup(username
, pw
, best
->names
[i
]))
1768 * The user isn't part of the specified group, so deny access...
1771 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1772 "cupsdIsAuthorized: User not in group(s)!");
1774 return (HTTP_UNAUTHORIZED
);
1779 * 'add_allow()' - Add an allow mask to the location.
1782 static cupsd_authmask_t
* /* O - New mask record */
1783 add_allow(cupsd_location_t
*loc
) /* I - Location to add to */
1785 cupsd_authmask_t
*temp
; /* New mask record */
1796 * Try to allocate memory for the record...
1799 if (loc
->num_allow
== 0)
1800 temp
= malloc(sizeof(cupsd_authmask_t
));
1802 temp
= realloc(loc
->allow
, sizeof(cupsd_authmask_t
) * (loc
->num_allow
+ 1));
1808 temp
+= loc
->num_allow
;
1812 * Clear the mask record and return...
1815 memset(temp
, 0, sizeof(cupsd_authmask_t
));
1821 * 'add_deny()' - Add a deny mask to the location.
1824 static cupsd_authmask_t
* /* O - New mask record */
1825 add_deny(cupsd_location_t
*loc
) /* I - Location to add to */
1827 cupsd_authmask_t
*temp
; /* New mask record */
1838 * Try to allocate memory for the record...
1841 if (loc
->num_deny
== 0)
1842 temp
= malloc(sizeof(cupsd_authmask_t
));
1844 temp
= realloc(loc
->deny
, sizeof(cupsd_authmask_t
) * (loc
->num_deny
+ 1));
1850 temp
+= loc
->num_deny
;
1854 * Clear the mask record and return...
1857 memset(temp
, 0, sizeof(cupsd_authmask_t
));
1863 * 'compare_locations()' - Compare two locations.
1866 static int /* O - Result of comparison */
1867 compare_locations(cupsd_location_t
*a
, /* I - First location */
1868 cupsd_location_t
*b
) /* I - Second location */
1870 return (strcmp(b
->location
, a
->location
));
1876 * 'cups_crypt()' - Encrypt the password using the DES or MD5 algorithms,
1880 static char * /* O - Encrypted password */
1881 cups_crypt(const char *pw
, /* I - Password string */
1882 const char *salt
) /* I - Salt (key) string */
1884 if (!strncmp(salt
, "$1$", 3))
1887 * Use MD5 passwords without the benefit of PAM; this is for
1888 * Slackware Linux, and the algorithm was taken from the
1889 * old shadow-19990827/lib/md5crypt.c source code... :(
1892 int i
; /* Looping var */
1893 unsigned long n
; /* Output number */
1894 int pwlen
; /* Length of password string */
1895 const char *salt_end
; /* End of "salt" data for MD5 */
1896 char *ptr
; /* Pointer into result string */
1897 _cups_md5_state_t state
; /* Primary MD5 state info */
1898 _cups_md5_state_t state2
; /* Secondary MD5 state info */
1899 unsigned char digest
[16]; /* MD5 digest result */
1900 static char result
[120]; /* Final password string */
1904 * Get the salt data between dollar signs, e.g. $1$saltdata$md5.
1905 * Get a maximum of 8 characters of salt data after $1$...
1908 for (salt_end
= salt
+ 3; *salt_end
&& (salt_end
- salt
) < 11; salt_end
++)
1909 if (*salt_end
== '$')
1913 * Compute the MD5 sum we need...
1918 _cupsMD5Init(&state
);
1919 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
1920 _cupsMD5Append(&state
, (unsigned char *)salt
, salt_end
- salt
);
1922 _cupsMD5Init(&state2
);
1923 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
1924 _cupsMD5Append(&state2
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
1925 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
1926 _cupsMD5Finish(&state2
, digest
);
1928 for (i
= pwlen
; i
> 0; i
-= 16)
1929 _cupsMD5Append(&state
, digest
, i
> 16 ? 16 : i
);
1931 for (i
= pwlen
; i
> 0; i
>>= 1)
1932 _cupsMD5Append(&state
, (unsigned char *)((i
& 1) ? "" : pw
), 1);
1934 _cupsMD5Finish(&state
, digest
);
1936 for (i
= 0; i
< 1000; i
++)
1938 _cupsMD5Init(&state
);
1941 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
1943 _cupsMD5Append(&state
, digest
, 16);
1946 _cupsMD5Append(&state
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
1949 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
1952 _cupsMD5Append(&state
, digest
, 16);
1954 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
1956 _cupsMD5Finish(&state
, digest
);
1960 * Copy the final sum to the result string and return...
1963 memcpy(result
, salt
, salt_end
- salt
);
1964 ptr
= result
+ (salt_end
- salt
);
1967 for (i
= 0; i
< 5; i
++, ptr
+= 4)
1969 n
= (((digest
[i
] << 8) | digest
[i
+ 6]) << 8);
1972 n
|= digest
[i
+ 12];
1979 to64(ptr
, digest
[11], 2);
1988 * Use the standard crypt() function...
1991 return (crypt(pw
, salt
));
1994 #endif /* !HAVE_LIBPAM */
1998 * 'get_md5_password()' - Get an MD5 password.
2001 static char * /* O - MD5 password string */
2002 get_md5_password(const char *username
, /* I - Username */
2003 const char *group
, /* I - Group */
2004 char passwd
[33]) /* O - MD5 password string */
2006 cups_file_t
*fp
; /* passwd.md5 file */
2007 char filename
[1024], /* passwd.md5 filename */
2008 line
[256], /* Line from file */
2009 tempuser
[33], /* User from file */
2010 tempgroup
[33]; /* Group from file */
2013 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2014 "get_md5_password(username=\"%s\", group=\"%s\", passwd=%p)",
2015 username
, group
? group
: "(null)", passwd
);
2017 snprintf(filename
, sizeof(filename
), "%s/passwd.md5", ServerRoot
);
2018 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2020 if (errno
!= ENOENT
)
2021 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", filename
,
2027 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
2029 if (sscanf(line
, "%32[^:]:%32[^:]:%32s", tempuser
, tempgroup
, passwd
) != 3)
2031 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad MD5 password line: %s", line
);
2035 if (!strcmp(username
, tempuser
) &&
2036 (group
== NULL
|| !strcmp(group
, tempgroup
)))
2039 * Found the password entry!
2042 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Found MD5 user %s, group %s...",
2043 username
, tempgroup
);
2051 * Didn't find a password entry - return NULL!
2061 * 'pam_func()' - PAM conversation function.
2064 static int /* O - Success or failure */
2066 int num_msg
, /* I - Number of messages */
2067 const struct pam_message
**msg
, /* I - Messages */
2068 struct pam_response
**resp
, /* O - Responses */
2070 /* I - Pointer to connection */
2072 int i
; /* Looping var */
2073 struct pam_response
*replies
; /* Replies */
2074 cupsd_authdata_t
*data
; /* Pointer to auth data */
2078 * Allocate memory for the responses...
2081 if ((replies
= malloc(sizeof(struct pam_response
) * num_msg
)) == NULL
)
2082 return (PAM_CONV_ERR
);
2085 * Answer all of the messages...
2088 DEBUG_printf(("pam_func: appdata_ptr = %p\n", appdata_ptr
));
2092 * Apparently some versions of HP-UX 11 have a broken pam_unix security
2093 * module. This is a workaround...
2099 data
= (cupsd_authdata_t
*)appdata_ptr
;
2102 for (i
= 0; i
< num_msg
; i
++)
2104 DEBUG_printf(("pam_func: Message = \"%s\"\n", msg
[i
]->msg
));
2106 switch (msg
[i
]->msg_style
)
2108 case PAM_PROMPT_ECHO_ON
:
2109 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_ON, returning \"%s\"...\n",
2111 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2112 replies
[i
].resp
= strdup(data
->username
);
2115 case PAM_PROMPT_ECHO_OFF
:
2116 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_OFF, returning \"%s\"...\n",
2118 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2119 replies
[i
].resp
= strdup(data
->password
);
2123 DEBUG_puts("pam_func: PAM_TEXT_INFO...");
2124 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2125 replies
[i
].resp
= NULL
;
2129 DEBUG_puts("pam_func: PAM_ERROR_MSG...");
2130 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2131 replies
[i
].resp
= NULL
;
2135 DEBUG_printf(("pam_func: Unknown PAM message %d...\n",
2136 msg
[i
]->msg_style
));
2138 return (PAM_CONV_ERR
);
2143 * Return the responses back to PAM...
2148 return (PAM_SUCCESS
);
2154 * 'to64()' - Base64-encode an integer value...
2158 to64(char *s
, /* O - Output string */
2159 unsigned long v
, /* I - Value to encode */
2160 int n
) /* I - Number of digits */
2162 const char *itoa64
= "./0123456789"
2163 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2164 "abcdefghijklmnopqrstuvwxyz";
2167 for (; n
> 0; n
--, v
>>= 6)
2168 *s
++ = itoa64
[v
& 0x3f];
2170 #endif /* HAVE_LIBPAM */
2174 * End of "$Id: auth.c 181 2006-06-22 20:01:18Z jlovell $".